-
-
Notifications
You must be signed in to change notification settings - Fork 412
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
138 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
58 changes: 58 additions & 0 deletions
58
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,58 @@ | ||
<?= "<?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()); | ||
|
||
$client->request('GET', '/register'); | ||
self::assertResponseIsSuccessful(); | ||
|
||
$client->submitForm('Register', [ | ||
'registration_form[email]' => '[email protected]', | ||
'registration_form[plainPassword]' => 'password', | ||
'registration_form[agreeTerms]' => true, | ||
]); | ||
|
||
// self::assertResponseRedirects('/'); | ||
self::assertCount(1, $userRepository->findAll()); | ||
self::assertFalse(($user = $userRepository->findAll()[0])->isVerified()); | ||
|
||
// Use either assertQueuedEmailCount() || assertEmailCount() depending on your mailer setup | ||
// self::assertQueuedEmailCount(1); | ||
self::assertEmailCount(1); | ||
|
||
self::assertCount(1, $messages = $this->getMailerMessages()); | ||
self::assertEmailTextBodyContains($messages[0], 'This link will expire in 1 hour.'); | ||
|
||
$client->followRedirect(); | ||
$client->loginUser($user); | ||
|
||
/** @var TemplatedEmail $templatedEmail */ | ||
$templatedEmail = $messages[0]; | ||
$messageBody = $templatedEmail->getHtmlBody(); | ||
|
||
preg_match('#(http://localhost/verify/email.+)">#', $messageBody, $resetLink); | ||
|
||
$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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,6 +225,50 @@ public function getTestDetails(): \Generator | |
$this->runRegistrationTest($runner, 'it_generates_registration_form_with_verification.php'); | ||
}), | ||
]; | ||
|
||
yield 'it_generates_registration_form_with_verification_and_with_tests' => [$this->createRegistrationFormTest() | ||
->addExtraDependencies('symfonycasts/verify-email-bundle') | ||
// needed for internal functional test | ||
->addExtraDependencies('symfony/web-profiler-bundle', 'mailer') | ||
->run(function (MakerTestRunner $runner) { | ||
$runner->writeFile( | ||
'config/packages/mailer.yaml', | ||
Yaml::dump(['framework' => [ | ||
'mailer' => ['dsn' => 'null://null'], | ||
]]) | ||
); | ||
|
||
$this->makeUser($runner); | ||
|
||
$output = $runner->runMaker([ | ||
'n', // add UniqueEntity | ||
'y', // verify user | ||
'y', // require authentication to verify user email | ||
'[email protected]', // from email address | ||
'SymfonyCasts', // From Name | ||
'n', // no authenticate after | ||
'app_anonymous', // route number to redirect to | ||
'y', // Generate tests | ||
]); | ||
|
||
$this->assertStringContainsString('Success', $output); | ||
|
||
$generatedFiles = [ | ||
'src/Security/EmailVerifier.php', | ||
'templates/registration/confirmation_email.html.twig', | ||
'tests/RegistrationControllerTest.php', | ||
]; | ||
|
||
foreach ($generatedFiles as $file) { | ||
$this->assertFileExists($runner->getPath($file)); | ||
} | ||
|
||
$runner->runConsole('cache:clear', [], '--env=test'); | ||
|
||
$runner->configureDatabase(); | ||
$runner->runTests(); | ||
}), | ||
]; | ||
} | ||
|
||
private function makeUser(MakerTestRunner $runner, string $identifier = 'email'): void | ||
|