-
-
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.
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 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
66 changes: 66 additions & 0 deletions
66
src/Resources/skeleton/registration/Test.WithVerify.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,66 @@ | ||
<?= "<?php\n" ?> | ||
namespace App\Tests; | ||
|
||
<?= $use_statements ?> | ||
|
||
class RegistrationControllerTest extends WebTestCase | ||
{ | ||
public function testRegister(): void | ||
{ | ||
$client = static::createClient(); | ||
|
||
$container = static::getContainer(); | ||
$em = $container->get('doctrine.orm.entity_manager'); | ||
$userRepository = $container->get(<?= $repository_class_name ?>::class); | ||
|
||
foreach ($userRepository->findAll() as $user) { | ||
$em->remove($user); | ||
} | ||
|
||
$em->flush(); | ||
|
||
self::assertCount(0, $userRepository->findAll()); | ||
|
||
// Register a new user | ||
$client->request('GET', '/register'); | ||
self::assertResponseIsSuccessful(); | ||
|
||
$client->submitForm('Register', [ | ||
'registration_form[email]' => 'me@example.com', | ||
'registration_form[plainPassword]' => 'password', | ||
'registration_form[agreeTerms]' => true, | ||
]); | ||
|
||
// Ensure the response redirects after submitting the form, the user exists, and is not verified | ||
// self::assertResponseRedirects('/'); | ||
self::assertCount(1, $userRepository->findAll()); | ||
self::assertFalse(($user = $userRepository->findAll()[0])->isVerified()); | ||
|
||
// Ensure the verification email was sent | ||
// Use either assertQueuedEmailCount() || assertEmailCount() depending on your mailer setup | ||
// self::assertQueuedEmailCount(1); | ||
self::assertEmailCount(1); | ||
|
||
self::assertCount(1, $messages = $this->getMailerMessages()); | ||
self::assertEmailAddressContains($messages[0], 'from', '<?= $from_email ?>'); | ||
self::assertEmailAddressContains($messages[0], 'to', 'me@example.com'); | ||
self::assertEmailTextBodyContains($messages[0], 'This link will expire in 1 hour.'); | ||
|
||
// Login the new user | ||
$client->followRedirect(); | ||
$client->loginUser($user); | ||
|
||
// Get the verification link from the email | ||
/** @var TemplatedEmail $templatedEmail */ | ||
$templatedEmail = $messages[0]; | ||
$messageBody = $templatedEmail->getHtmlBody(); | ||
|
||
preg_match('#(http://localhost/verify/email.+)">#', $messageBody, $resetLink); | ||
|
||
// "Click" the link, and see if the user is verified | ||
$client->request('GET', $resetLink[1]); | ||
$client->followRedirect(); | ||
|
||
self::assertTrue(static::getContainer()->get(UserRepository::class)->findAll()[0]->isVerified()); | ||
} | ||
} |
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