diff --git a/tests/Functional/BrowserCest.php b/tests/Functional/BrowserCest.php index d27aea5..7167bac 100644 --- a/tests/Functional/BrowserCest.php +++ b/tests/Functional/BrowserCest.php @@ -135,12 +135,7 @@ public function seePageRedirectsTo(FunctionalTester $I) public function submitSymfonyForm(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'jane_doe@gmail.com', - '[password]' => '123456', - '[agreeTerms]' => true, - ]); + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: true); $I->seeInRepository(User::class, [ 'email' => 'jane_doe@gmail.com', ]); diff --git a/tests/Functional/EventsCest.php b/tests/Functional/EventsCest.php index a9f1dd7..c12a523 100644 --- a/tests/Functional/EventsCest.php +++ b/tests/Functional/EventsCest.php @@ -80,25 +80,13 @@ public function seeEventListenerIsCalled(FunctionalTester $I) public function seeOrphanEvent(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->stopFollowingRedirects(); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'jane_doe@gmail.com', - '[password]' => '123456', - '[agreeTerms]' => true, - ]); + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); $I->seeOrphanEvent(UserRegisteredEvent::class); } public function seeEvent(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->stopFollowingRedirects(); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'jane_doe@gmail.com', - '[password]' => '123456', - '[agreeTerms]' => true, - ]); + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); $I->seeEvent(UserRegisteredEvent::class); $I->seeEvent(KernelEvents::REQUEST, KernelEvents::FINISH_REQUEST); try { diff --git a/tests/Functional/FormCest.php b/tests/Functional/FormCest.php index 76cb010..31c91bb 100644 --- a/tests/Functional/FormCest.php +++ b/tests/Functional/FormCest.php @@ -22,35 +22,20 @@ public function assertNoFormValue(FunctionalTester $I) public function dontSeeFormErrors(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'jane_doe@gmail.com', - '[password]' => '123456', - '[agreeTerms]' => true, - ]); + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: true); $I->dontSeeFormErrors(); } public function seeFormErrorMessage(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'john_doe@gmail.com', - '[password]' => '123456', - '[agreeTerms]' => true, - ]); + $I->registerUser('john_doe@gmail.com', '123456', followRedirects: true); $I->seeFormErrorMessage('email'); $I->seeFormErrorMessage('email', 'There is already an account with this email'); } public function seeFormErrorMessages(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'john_doe@gmail.com', - '[password]' => '123', - '[agreeTerms]' => true, - ]); + $I->registerUser('john_doe@gmail.com', '123', followRedirects: true); // Only with the names of the fields $I->seeFormErrorMessages(['email', 'password']); @@ -66,12 +51,7 @@ public function seeFormErrorMessages(FunctionalTester $I) public function seeFormHasErrors(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'john_doe@gmail.com', - '[password]' => '123456', - '[agreeTerms]' => true, - ]); + $I->registerUser('john_doe@gmail.com', '123456', followRedirects: true); // There is already an account with this email $I->seeFormHasErrors(); } diff --git a/tests/Functional/MailerCest.php b/tests/Functional/MailerCest.php index 96a76e7..82870f4 100644 --- a/tests/Functional/MailerCest.php +++ b/tests/Functional/MailerCest.php @@ -10,26 +10,14 @@ final class MailerCest { public function dontSeeEmailIsSent(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->stopFollowingRedirects(); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'john_doe@gmail.com', - '[password]' => '123456', - '[agreeTerms]' => true, - ]); + $I->registerUser('john_doe@gmail.com', '123456', followRedirects: false); // There is already an account with this email $I->dontSeeEmailIsSent(); } public function grabLastSentEmail(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->stopFollowingRedirects(); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'jane_doe@gmail.com', - '[password]' => '123456', - '[agreeTerms]' => true, - ]); + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); $email = $I->grabLastSentEmail(); $address = $email->getTo()[0]; $I->assertSame('jane_doe@gmail.com', $address->getAddress()); @@ -37,13 +25,7 @@ public function grabLastSentEmail(FunctionalTester $I) public function grabSentEmails(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->stopFollowingRedirects(); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'jane_doe@gmail.com', - '[password]' => '123456', - '[agreeTerms]' => true, - ]); + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); $emails = $I->grabSentEmails(); $address = $emails[0]->getTo()[0]; $I->assertSame('jane_doe@gmail.com', $address->getAddress()); @@ -51,13 +33,7 @@ public function grabSentEmails(FunctionalTester $I) public function seeEmailIsSent(FunctionalTester $I) { - $I->amOnPage('/register'); - $I->stopFollowingRedirects(); - $I->submitSymfonyForm('registration_form', [ - '[email]' => 'jane_doe@gmail.com', - '[password]' => '123456', - '[agreeTerms]' => true, - ]); + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); $I->seeEmailIsSent(); } } diff --git a/tests/Support/FunctionalTester.php b/tests/Support/FunctionalTester.php index 6015508..3e74da4 100644 --- a/tests/Support/FunctionalTester.php +++ b/tests/Support/FunctionalTester.php @@ -9,4 +9,17 @@ class FunctionalTester extends Actor { use _generated\FunctionalTesterActions; + + public function registerUser(string $email, string $password, bool $followRedirects): void + { + $this->amOnPage('/register'); + if (!$followRedirects) { + $this->stopFollowingRedirects(); + } + $this->submitSymfonyForm('registration_form', [ + '[email]' => $email, + '[password]' => $password, + '[agreeTerms]' => true, + ]); + } }