Skip to content

Commit 6398d45

Browse files
xEdelweissTavoNiievez
authored andcommitted
Support new event assertions (#25)
1 parent e90a41a commit 6398d45

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

src/Controller/RegistrationController.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
namespace App\Controller;
66

77
use App\Entity\User;
8+
use App\Event\UserRegisteredEvent;
89
use App\Form\RegistrationFormType;
910
use App\Repository\Model\UserRepositoryInterface;
1011
use App\Utils\Mailer;
12+
use Psr\EventDispatcher\EventDispatcherInterface;
1113
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1214
use Symfony\Component\HttpFoundation\Request;
1315
use Symfony\Component\HttpFoundation\Response;
@@ -18,10 +20,16 @@ final class RegistrationController extends AbstractController
1820

1921
private UserRepositoryInterface $userRepository;
2022

21-
public function __construct(Mailer $mailer, UserRepositoryInterface $userRepository)
22-
{
23+
private EventDispatcherInterface $eventDispatcher;
24+
25+
public function __construct(
26+
Mailer $mailer,
27+
UserRepositoryInterface $userRepository,
28+
EventDispatcherInterface $eventDispatcher
29+
) {
2330
$this->mailer = $mailer;
2431
$this->userRepository = $userRepository;
32+
$this->eventDispatcher = $eventDispatcher;
2533
}
2634

2735
public function __invoke(Request $request): Response
@@ -38,6 +46,8 @@ public function __invoke(Request $request): Response
3846

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

49+
$this->eventDispatcher->dispatch(new UserRegisteredEvent());
50+
4151
return $this->redirectToRoute('app_login');
4252
}
4353

src/Event/UserRegisteredEvent.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace App\Event;
4+
5+
class UserRegisteredEvent
6+
{
7+
}

tests/Functional/EventsCest.php

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44

55
namespace App\Tests\Functional;
66

7+
use App\Event\UserRegisteredEvent;
78
use App\Tests\FunctionalTester;
8-
use Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener;
99
use Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector;
10+
use Symfony\Component\Console\ConsoleEvents;
1011
use Symfony\Component\Console\EventListener\ErrorListener;
12+
use Symfony\Component\HttpKernel\EventListener\LocaleListener;
13+
use Symfony\Component\HttpKernel\EventListener\RouterListener;
14+
use Symfony\Component\HttpKernel\KernelEvents;
1115

1216
final class EventsCest
1317
{
18+
/**
19+
* @deprecated in favor of dontSeeEventListenerIsCalled
20+
*/
1421
public function dontSeeEventTriggered(FunctionalTester $I)
1522
{
1623
$I->amOnPage('/');
@@ -19,6 +26,17 @@ public function dontSeeEventTriggered(FunctionalTester $I)
1926
$I->dontSeeEventTriggered([ErrorListener::class, ErrorListener::class]);
2027
}
2128

29+
public function dontSeeEventListenerIsCalled(FunctionalTester $I)
30+
{
31+
$I->amOnPage('/');
32+
$I->dontSeeEventListenerIsCalled(ErrorListener::class);
33+
$I->dontSeeEventListenerIsCalled(new ErrorListener());
34+
$I->dontSeeEventListenerIsCalled([ErrorListener::class, ErrorListener::class]);
35+
// with events
36+
$I->dontSeeEventListenerIsCalled(RouterListener::class, KernelEvents::EXCEPTION);
37+
$I->dontSeeEventListenerIsCalled(RouterListener::class, [KernelEvents::RESPONSE, KernelEvents::EXCEPTION]);
38+
}
39+
2240
public function dontSeeOrphanEvent(FunctionalTester $I)
2341
{
2442
$I->amOnPage('/login');
@@ -27,26 +45,62 @@ public function dontSeeOrphanEvent(FunctionalTester $I)
2745
'password' => '123456',
2846
'_remember_me' => false
2947
]);
30-
$I->dontseeOrphanEvent();
31-
$I->dontSeeOrphanEvent('security.authentication.success');
48+
$I->dontSeeOrphanEvent();
3249
}
3350

51+
public function dontSeeEvent(FunctionalTester $I)
52+
{
53+
$I->markTestSkipped();
54+
$I->amOnPage('/');
55+
$I->dontSeeEvent(KernelEvents::EXCEPTION);
56+
$I->dontSeeEvent([new UserRegisteredEvent(), ConsoleEvents::COMMAND]);
57+
}
58+
59+
/**
60+
* @deprecated in favor of seeEventListenerIsCalled
61+
*/
3462
public function seeEventTriggered(FunctionalTester $I)
3563
{
3664
$I->amOnPage('/');
37-
$I->seeEventTriggered(SecurityListener::class);
65+
$I->seeEventTriggered(RouterListener::class);
3866
$I->seeEventTriggered(new RouterDataCollector());
39-
$I->seeEventTriggered([SecurityListener::class, RouterDataCollector::class]);
67+
$I->seeEventTriggered([RouterListener::class, RouterDataCollector::class]);
68+
}
69+
70+
public function seeEventListenerIsCalled(FunctionalTester $I)
71+
{
72+
$I->amOnPage('/');
73+
$I->seeEventListenerIsCalled(RouterListener::class);
74+
$I->seeEventListenerIsCalled(new RouterDataCollector());
75+
$I->seeEventListenerIsCalled([RouterListener::class, RouterDataCollector::class]);
76+
// with events
77+
$I->seeEventListenerIsCalled(RouterListener::class, KernelEvents::REQUEST);
78+
$I->seeEventListenerIsCalled(LocaleListener::class, [KernelEvents::REQUEST, KernelEvents::FINISH_REQUEST]);
4079
}
4180

4281
public function seeOrphanEvent(FunctionalTester $I)
4382
{
4483
$I->amOnPage('/register');
84+
$I->stopFollowingRedirects();
85+
$I->submitSymfonyForm('registration_form', [
86+
'[email]' => 'jane_doe@gmail.com',
87+
'[plainPassword]' => '123456',
88+
'[agreeTerms]' => true
89+
]);
90+
$I->seeOrphanEvent(UserRegisteredEvent::class);
91+
}
92+
93+
public function seeEvent(FunctionalTester $I)
94+
{
95+
$I->markTestSkipped();
96+
$I->amOnPage('/register');
97+
$I->stopFollowingRedirects();
4598
$I->submitSymfonyForm('registration_form', [
4699
'[email]' => 'jane_doe@gmail.com',
47100
'[plainPassword]' => '123456',
48101
'[agreeTerms]' => true
49102
]);
50-
$I->seeOrphanEvent('security.authentication.success');
103+
$I->seeEvent(UserRegisteredEvent::class);
104+
$I->seeEvent(KernelEvents::REQUEST, KernelEvents::FINISH_REQUEST);
51105
}
52106
}

0 commit comments

Comments
 (0)