-
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[make:security:form-login] add ability to generate tests
- Loading branch information
Showing
3 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
src/Resources/skeleton/security/formLogin/Test.LoginController.tpl.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?= "<?php\n" ?> | ||
namespace App\Tests; | ||
|
||
<?= $use_statements ?> | ||
|
||
class LoginControllerTest extends WebTestCase | ||
{ | ||
public function testLogin(): void | ||
{ | ||
$client = static::createClient(); | ||
$container = static::getContainer(); | ||
$em = $container->get('doctrine.orm.entity_manager'); | ||
$userRepository = $em->getRepository(\<?= $user_class ?>::class); | ||
|
||
foreach ($userRepository->findAll() as $user) { | ||
$em->remove($user); | ||
} | ||
|
||
$em->flush(); | ||
|
||
self::assertCount(0, $userRepository->findAll()); | ||
|
||
// Ensure login with invalid credentials shows error message | ||
$client->request('GET', '/login'); | ||
self::assertResponseIsSuccessful(); | ||
|
||
$client->submitForm('Sign in', [ | ||
'_username' => 'email@example.com', | ||
'_password' => 'password', | ||
]); | ||
|
||
self::assertResponseRedirects('/login'); | ||
|
||
$client->followRedirect(); | ||
|
||
self::assertSelectorTextContains('.alert-danger', 'Invalid credentials.'); | ||
|
||
// Ensure login with valid credentials | ||
/** @var UserPasswordHasherInterface $passwordHasher */ | ||
$passwordHasher = (static::getContainer())->get('security.user_password_hasher'); | ||
|
||
$user = (new User())->setEmail('email@example.com'); | ||
$user->setPassword($passwordHasher->hashPassword($user, 'password')); | ||
|
||
$em->persist($user); | ||
$em->flush(); | ||
|
||
$client->submitForm('Sign in', [ | ||
'_username' => 'email@example.com', | ||
'_password' => 'password', | ||
]); | ||
|
||
self::assertResponseRedirects('/'); | ||
$client->followRedirect(); | ||
self::assertSelectorNotExists('.alert-danger'); | ||
self::assertResponseIsSuccessful(); | ||
} | ||
} |
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