Skip to content

Support new event assertions #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
{
}
54 changes: 46 additions & 8 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 @@ -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)
Expand All @@ -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);
}
}