Skip to content

Commit

Permalink
Support new event assertions (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
xEdelweiss authored and TavoNiievez committed Jan 2, 2024
1 parent fc6b934 commit 64c06fb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
14 changes: 12 additions & 2 deletions src/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -38,6 +46,8 @@ public function __invoke(Request $request): Response

$this->mailer->sendConfirmationEmail($user);

$this->eventDispatcher->dispatch(new UserRegisteredEvent());

return $this->redirectToRoute('app_login');
}

Expand Down
7 changes: 7 additions & 0 deletions src/Event/UserRegisteredEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Event;

class UserRegisteredEvent
{
}
65 changes: 60 additions & 5 deletions tests/Functional/EventsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

namespace App\Tests\Functional;

use App\Event\UserRegisteredEvent;
use App\Tests\FunctionalTester;
use Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener;
use Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\EventListener\ErrorListener;
use Symfony\Component\HttpKernel\EventListener\LocaleListener;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\HttpKernel\KernelEvents;

final class EventsCest
{
/**
* @deprecated in favor of dontSeeEventListenerIsCalled
*/
public function dontSeeEventTriggered(FunctionalTester $I)
{
$I->amOnPage('/');
Expand All @@ -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');
Expand All @@ -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]' => '[email protected]',
'[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]' => '[email protected]',
'[plainPassword]' => '123456',
'[agreeTerms]' => true
]);
$I->seeOrphanEvent('security.authentication.success');
$I->seeEvent(UserRegisteredEvent::class);
$I->seeEvent(KernelEvents::REQUEST, KernelEvents::FINISH_REQUEST);
}
}

0 comments on commit 64c06fb

Please sign in to comment.