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('/'); @@ -19,6 +26,17 @@ public function dontSeeEventTriggered(FunctionalTester $I) $I->dontSeeEventTriggered([ErrorListener::class, ErrorListener::class]); } + public function dontSeeEventListenerIsCalled(FunctionalTester $I) + { + $I->amOnPage('/'); + $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) { $I->amOnPage('/login'); @@ -27,25 +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(RouterListener::class); $I->seeEventTriggered(new RouterDataCollector()); - $I->seeEventTriggered([RouterDataCollector::class]); + $I->seeEventTriggered([RouterListener::class, RouterDataCollector::class]); + } + + public function seeEventListenerIsCalled(FunctionalTester $I) + { + $I->amOnPage('/'); + $I->seeEventListenerIsCalled(RouterListener::class); + $I->seeEventListenerIsCalled(new RouterDataCollector()); + $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); } }