diff --git a/composer.lock b/composer.lock index e55ef8c..4402f34 100644 --- a/composer.lock +++ b/composer.lock @@ -5933,12 +5933,12 @@ "source": { "type": "git", "url": "https://github.com/Codeception/module-symfony.git", - "reference": "cb1334090161aecc94bdb78c7ea3a52417cd728b" + "reference": "19c86fc1041fc29b151ec9a783426089731dbae7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Codeception/module-symfony/zipball/cb1334090161aecc94bdb78c7ea3a52417cd728b", - "reference": "cb1334090161aecc94bdb78c7ea3a52417cd728b", + "url": "https://api.github.com/repos/Codeception/module-symfony/zipball/19c86fc1041fc29b151ec9a783426089731dbae7", + "reference": "19c86fc1041fc29b151ec9a783426089731dbae7", "shasum": "" }, "require": { @@ -5994,7 +5994,7 @@ "issues": "https://github.com/Codeception/module-symfony/issues", "source": "https://github.com/Codeception/module-symfony/tree/main" }, - "time": "2023-10-23T18:28:50+00:00" + "time": "2023-12-11T21:10:21+00:00" }, { "name": "codeception/stub", diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index a552c93..a0b3264 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -5,9 +5,11 @@ namespace App\Controller; use App\Entity\User; +use App\Event\UserRegisteredEvent; use App\Form\RegistrationFormType; use App\Repository\Model\UserRepositoryInterface; use App\Utils\Mailer; +use Psr\EventDispatcher\EventDispatcherInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -18,10 +20,16 @@ final class RegistrationController extends AbstractController private UserRepositoryInterface $userRepository; - public function __construct(Mailer $mailer, UserRepositoryInterface $userRepository) - { + private EventDispatcherInterface $eventDispatcher; + + public function __construct( + Mailer $mailer, + UserRepositoryInterface $userRepository, + EventDispatcherInterface $eventDispatcher + ) { $this->mailer = $mailer; $this->userRepository = $userRepository; + $this->eventDispatcher = $eventDispatcher; } public function __invoke(Request $request): Response @@ -38,6 +46,8 @@ public function __invoke(Request $request): Response $this->mailer->sendConfirmationEmail($user); + $this->eventDispatcher->dispatch(new UserRegisteredEvent()); + return $this->redirectToRoute('app_login'); } diff --git a/src/Event/UserRegisteredEvent.php b/src/Event/UserRegisteredEvent.php new file mode 100644 index 0000000..e968232 --- /dev/null +++ b/src/Event/UserRegisteredEvent.php @@ -0,0 +1,7 @@ +amOnPage('/'); @@ -25,6 +32,9 @@ public function dontSeeEventListenerIsCalled(FunctionalTester $I) $I->dontSeeEventListenerIsCalled(ErrorListener::class); $I->dontSeeEventListenerIsCalled(new ErrorListener()); $I->dontSeeEventListenerIsCalled([ErrorListener::class, ErrorListener::class]); + // with events + $I->dontSeeEventListenerIsCalled(RouterListener::class, KernelEvents::EXCEPTION); + $I->dontSeeEventListenerIsCalled(RouterListener::class, [KernelEvents::RESPONSE, KernelEvents::EXCEPTION]); } public function dontSeeOrphanEvent(FunctionalTester $I) @@ -35,34 +45,62 @@ public function dontSeeOrphanEvent(FunctionalTester $I) 'password' => '123456', '_remember_me' => false ]); - $I->dontseeOrphanEvent(); + $I->dontSeeOrphanEvent(); } + public function dontSeeEvent(FunctionalTester $I) + { + $I->markTestSkipped(); + $I->amOnPage('/'); + $I->dontSeeEvent(KernelEvents::EXCEPTION); + $I->dontSeeEvent([new UserRegisteredEvent(), ConsoleEvents::COMMAND]); + } + + /** + * @deprecated in favor of seeEventListenerIsCalled + */ public function seeEventTriggered(FunctionalTester $I) { $I->amOnPage('/'); - $I->seeEventTriggered(SecurityListener::class); + $I->seeEventTriggered(RouterListener::class); $I->seeEventTriggered(new RouterDataCollector()); - $I->seeEventTriggered([SecurityListener::class, RouterDataCollector::class]); + $I->seeEventTriggered([RouterListener::class, RouterDataCollector::class]); } public function seeEventListenerIsCalled(FunctionalTester $I) { $I->amOnPage('/'); - $I->seeEventListenerIsCalled(SecurityListener::class); + $I->seeEventListenerIsCalled(RouterListener::class); $I->seeEventListenerIsCalled(new RouterDataCollector()); - $I->seeEventListenerIsCalled([SecurityListener::class, RouterDataCollector::class]); + $I->seeEventListenerIsCalled([RouterListener::class, RouterDataCollector::class]); + // with events + $I->seeEventListenerIsCalled(RouterListener::class, KernelEvents::REQUEST); + $I->seeEventListenerIsCalled(LocaleListener::class, [KernelEvents::REQUEST, KernelEvents::FINISH_REQUEST]); } public function seeOrphanEvent(FunctionalTester $I) { - $I->markTestIncomplete('To do: use a new event for this assertion'); $I->amOnPage('/register'); + $I->stopFollowingRedirects(); + $I->submitSymfonyForm('registration_form', [ + '[email]' => 'jane_doe@gmail.com', + '[plainPassword]' => '123456', + '[agreeTerms]' => true + ]); + $I->seeOrphanEvent(UserRegisteredEvent::class); + } + + public function seeEvent(FunctionalTester $I) + { + $I->markTestSkipped(); + $I->amOnPage('/register'); + $I->stopFollowingRedirects(); $I->submitSymfonyForm('registration_form', [ '[email]' => 'jane_doe@gmail.com', '[plainPassword]' => '123456', '[agreeTerms]' => true ]); - $I->seeOrphanEvent('security.authentication.success'); + $I->seeEvent(UserRegisteredEvent::class); + $I->seeEvent(KernelEvents::REQUEST, KernelEvents::FINISH_REQUEST); } }