Skip to content

Commit 64c06fb

Browse files
xEdelweissTavoNiievez
authored andcommitted
Support new event assertions (#25)
1 parent fc6b934 commit 64c06fb

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
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 & 5 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,25 +45,62 @@ public function dontSeeOrphanEvent(FunctionalTester $I)
2745
'password' => '123456',
2846
'_remember_me' => false
2947
]);
30-
$I->dontseeOrphanEvent();
48+
$I->dontSeeOrphanEvent();
3149
}
3250

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+
*/
3362
public function seeEventTriggered(FunctionalTester $I)
3463
{
3564
$I->amOnPage('/');
65+
$I->seeEventTriggered(RouterListener::class);
3666
$I->seeEventTriggered(new RouterDataCollector());
37-
$I->seeEventTriggered([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]);
3879
}
3980

4081
public function seeOrphanEvent(FunctionalTester $I)
4182
{
42-
$I->markTestIncomplete('To do: use a new event for this assertion');
4383
$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();
4498
$I->submitSymfonyForm('registration_form', [
4599
'[email]' => 'jane_doe@gmail.com',
46100
'[plainPassword]' => '123456',
47101
'[agreeTerms]' => true
48102
]);
49-
$I->seeOrphanEvent('security.authentication.success');
103+
$I->seeEvent(UserRegisteredEvent::class);
104+
$I->seeEvent(KernelEvents::REQUEST, KernelEvents::FINISH_REQUEST);
50105
}
51106
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy