Created
March 25, 2021 23:40
-
-
Save TristanOrta/24b6f209cf9e5d4462a9713cf550b991 to your computer and use it in GitHub Desktop.
this is an example of a php unit test for the login of your application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use Illuminate\Foundation\Testing\WithFaker; | |
use Tests\TestCase; | |
use App\User; | |
use Illuminate\Support\Facades\DB; | |
class LoginModuleTest extends TestCase | |
{ | |
use RefreshDatabase; | |
//el usuario puede ver el formulario login | |
public function test_user_can_view_a_login_form() | |
{ | |
$response = $this->get('/login'); | |
$response->assertSuccessful(); | |
$response->assertViewIs('auth.login'); | |
} | |
//el usuario no puede ver el formulario de login cuando esta autenticado | |
public function test_user_cannot_view_a_login_form_when_authenticated() | |
{ | |
$user = factory(User::class)->make(); | |
$response = $this->actingAs($user)->get('/login'); | |
$response->assertRedirect('/home'); | |
} | |
// el usuario puede logearse con las credenciales correctas | |
public function test_user_can_login_with_correct_credentials() | |
{ | |
// Creamos un factory de user para que lo utilice en la prueba | |
$user = factory(User::class)->create([ | |
'password' => bcrypt($password = '123456'), | |
]); | |
$response = $this->post('/login', [ | |
'email' => $user->email, | |
'password' => $password, | |
]); | |
$response->assertRedirect('/home'); | |
$this->assertAuthenticatedAs($user); | |
} | |
// El usuario no puede logearse con credenciales incorectas | |
public function test_user_cannot_login_with_incorrect_password() | |
{ | |
$user = factory(User::class)->create([ | |
'password' => bcrypt('123456'), | |
]); | |
$response = $this->from('/login')->post('/login', [ | |
'email' => $user->email, | |
'password' => 'invalid-password', | |
]); | |
$response->assertRedirect('/login'); | |
$response->assertSessionHasErrors('email'); | |
$this->assertTrue(session()->hasOldInput('email')); | |
$this->assertFalse(session()->hasOldInput('password')); | |
$this->assertGuest(); | |
} | |
public function test_remember_me_functionality() | |
{ | |
$user = factory(User::class)->create([ | |
'id' => random_int(1, 100), | |
'password' => bcrypt($password = '123456'), | |
]); | |
$response = $this->post('/login', [ | |
'email' => $user->email, | |
'password' => $password, | |
'remember' => 'on', | |
]); | |
$response->assertRedirect('/home'); | |
// cookie assertion goes here | |
$this->assertAuthenticatedAs($user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment