Skip to content

Commit a0e7bf6

Browse files
authored
Support new event assertions (#25)
* Update seeOrphanEvent test, add tests for seeEvent/dontSeeEvent * Add tests for seeEventTriggered/dontSeeEventTriggered to check event filtering * mark as skipped method tests not yet added * Remove sensio/framework-extra-bundle usage * Update codeception/module-symfony dependency
1 parent 8dcea5f commit a0e7bf6

File tree

4 files changed

+69
-14
lines changed

4 files changed

+69
-14
lines changed

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 46 additions & 8 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('/');
@@ -25,6 +32,9 @@ public function dontSeeEventListenerIsCalled(FunctionalTester $I)
2532
$I->dontSeeEventListenerIsCalled(ErrorListener::class);
2633
$I->dontSeeEventListenerIsCalled(new ErrorListener());
2734
$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]);
2838
}
2939

3040
public function dontSeeOrphanEvent(FunctionalTester $I)
@@ -35,34 +45,62 @@ public function dontSeeOrphanEvent(FunctionalTester $I)
3545
'password' => '123456',
3646
'_remember_me' => false
3747
]);
38-
$I->dontseeOrphanEvent();
48+
$I->dontSeeOrphanEvent();
3949
}
4050

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+
*/
4162
public function seeEventTriggered(FunctionalTester $I)
4263
{
4364
$I->amOnPage('/');
44-
$I->seeEventTriggered(SecurityListener::class);
65+
$I->seeEventTriggered(RouterListener::class);
4566
$I->seeEventTriggered(new RouterDataCollector());
46-
$I->seeEventTriggered([SecurityListener::class, RouterDataCollector::class]);
67+
$I->seeEventTriggered([RouterListener::class, RouterDataCollector::class]);
4768
}
4869

4970
public function seeEventListenerIsCalled(FunctionalTester $I)
5071
{
5172
$I->amOnPage('/');
52-
$I->seeEventListenerIsCalled(SecurityListener::class);
73+
$I->seeEventListenerIsCalled(RouterListener::class);
5374
$I->seeEventListenerIsCalled(new RouterDataCollector());
54-
$I->seeEventListenerIsCalled([SecurityListener::class, RouterDataCollector::class]);
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]);
5579
}
5680

5781
public function seeOrphanEvent(FunctionalTester $I)
5882
{
59-
$I->markTestIncomplete('To do: use a new event for this assertion');
6083
$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();
6198
$I->submitSymfonyForm('registration_form', [
6299
'[email]' => 'jane_doe@gmail.com',
63100
'[plainPassword]' => '123456',
64101
'[agreeTerms]' => true
65102
]);
66-
$I->seeOrphanEvent('security.authentication.success');
103+
$I->seeEvent(UserRegisteredEvent::class);
104+
$I->seeEvent(KernelEvents::REQUEST, KernelEvents::FINISH_REQUEST);
67105
}
68106
}

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