Skip to content

Commit 2113f49

Browse files
Add notifier cest (#35)
Co-authored-by: dmitrii <[email protected]>
1 parent 12cb123 commit 2113f49

File tree

9 files changed

+214
-2
lines changed

9 files changed

+214
-2
lines changed

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ DATABASE_URL=sqlite:///%kernel.project_dir%/var/test.db3
1414
###> symfony/mailer ###
1515
MAILER_DSN=null://null
1616
###< symfony/mailer ###
17+
18+
###> symfony/notifier ###
19+
NOTIFIER_DSN=null://null
20+
###< symfony/notifier ###

.env.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ APP_SECRET='$ecretf0rt3st'
33
DATABASE_URL=sqlite:///%kernel.project_dir%/var/test.db3
44
KERNEL_CLASS='App\Kernel'
55
MAILER_DSN=null://null
6-
SYMFONY_DEPRECATIONS_HELPER=999999
6+
NOTIFIER_DSN=null://null
7+
SYMFONY_DEPRECATIONS_HELPER=999999

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"symfony/framework-bundle": "6.4.*",
2424
"symfony/http-client": "6.4.*",
2525
"symfony/mailer": "6.4.*",
26+
"symfony/notifier": "6.4.*",
2627
"symfony/runtime": "6.4.*",
2728
"symfony/security-bundle": "6.4.*",
2829
"symfony/translation": "6.4.*",

composer.lock

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

config/packages/framework.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
$framework->mailer()
2525
->dsn('%env(MAILER_DSN)%');
2626

27+
// Notifier
28+
$framework->notifier()
29+
->chatterTransport('slack', '%env(NOTIFIER_DSN)%');
30+
2731
// Routing
2832
$framework->router()
2933
->utf8(true);

src/Controller/RegistrationController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Form\RegistrationFormType;
1010
use App\Repository\Model\UserRepositoryInterface;
1111
use App\Utils\Mailer;
12+
use App\Utils\Notifier;
1213
use Psr\EventDispatcher\EventDispatcherInterface;
1314
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1415
use Symfony\Component\HttpFoundation\Request;
@@ -18,6 +19,7 @@ final class RegistrationController extends AbstractController
1819
{
1920
public function __construct(
2021
private readonly Mailer $mailer,
22+
private readonly Notifier $notifier,
2123
private readonly UserRepositoryInterface $userRepository,
2224
private readonly EventDispatcherInterface $eventDispatcher,
2325
) {
@@ -34,6 +36,8 @@ public function __invoke(Request $request): Response
3436

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

39+
$this->notifier->sendConfirmationNotification($user);
40+
3741
$this->eventDispatcher->dispatch(new UserRegisteredEvent());
3842

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

src/Utils/Notifier.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Utils;
6+
7+
use App\Entity\User;
8+
use Symfony\Component\Notifier\Notification\Notification;
9+
use Symfony\Component\Notifier\NotifierInterface;
10+
use Symfony\Component\Notifier\Recipient\Recipient;
11+
12+
final readonly class Notifier
13+
{
14+
public function __construct(private NotifierInterface $notifier)
15+
{
16+
}
17+
18+
public function sendConfirmationNotification(User $user): Notification
19+
{
20+
$notification = (new Notification('Account created!', ['chat']))
21+
->content("Account for {$user->getEmail()} created successfully");
22+
23+
$recipient = new Recipient($user->getEmail());
24+
25+
$this->notifier->send($notification, $recipient);
26+
27+
return $notification;
28+
}
29+
}

symfony.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
"ref": "89689e24507d8cb2c4a7937ece549db3836b608c"
99
}
1010
},
11+
"doctrine/deprecations": {
12+
"version": "1.1",
13+
"recipe": {
14+
"repo": "github.com/symfony/recipes",
15+
"branch": "main",
16+
"version": "1.0",
17+
"ref": "87424683adc81d7dc305eefec1fced883084aab9"
18+
}
19+
},
1120
"doctrine/doctrine-bundle": {
1221
"version": "2.11",
1322
"recipe": {
@@ -163,6 +172,18 @@
163172
"ref": "fadbfe33303a76e25cb63401050439aa9b1a9c7f"
164173
}
165174
},
175+
"symfony/notifier": {
176+
"version": "6.4",
177+
"recipe": {
178+
"repo": "github.com/symfony/recipes",
179+
"branch": "main",
180+
"version": "5.0",
181+
"ref": "178877daf79d2dbd62129dd03612cb1a2cb407cc"
182+
},
183+
"files": [
184+
"./config/packages/notifier.yaml"
185+
]
186+
},
166187
"symfony/routing": {
167188
"version": "7.0",
168189
"recipe": {

tests/Functional/NotifierCest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Functional;
6+
7+
use App\Tests\Support\FunctionalTester;
8+
9+
final class NotifierCest
10+
{
11+
public function assertNotificationSubjectContains(FunctionalTester $I)
12+
{
13+
$I->registerUser('[email protected]', '123456', followRedirects: false);
14+
$notification = $I->getNotifierMessage();
15+
$I->assertNotificationSubjectContains($notification, 'created!');
16+
}
17+
18+
public function assertNotificationSubjectNotContains(FunctionalTester $I)
19+
{
20+
$I->registerUser('[email protected]', '123456', followRedirects: false);
21+
$notification = $I->getNotifierMessage();
22+
$I->assertNotificationSubjectNotContains($notification, 'Account not created!');
23+
}
24+
25+
public function assertNotificationTransportIsEqual(FunctionalTester $I)
26+
{
27+
$I->registerUser('[email protected]', '123456', followRedirects: false);
28+
$notification = $I->getNotifierMessage();
29+
$I->assertNotificationTransportIsEqual($notification);
30+
}
31+
32+
public function assertNotificationTransportIsNotEqual(FunctionalTester $I)
33+
{
34+
$I->registerUser('[email protected]', '123456', followRedirects: false);
35+
$notification = $I->getNotifierMessage();
36+
$I->assertNotificationTransportIsNotEqual($notification, 'chat');
37+
}
38+
39+
public function dontSeeNotificationIsSent(FunctionalTester $I)
40+
{
41+
$I->registerUser('[email protected]', '123456', followRedirects: false);
42+
// There is already an account with this notification
43+
$I->dontSeeNotificationIsSent();
44+
}
45+
46+
public function grabLastSentNotification(FunctionalTester $I)
47+
{
48+
$I->registerUser('[email protected]', '123456', followRedirects: false);
49+
$notification = $I->grabLastSentNotification();
50+
$I->assertSame('Account created!', $notification->getSubject());
51+
}
52+
53+
public function grabSentNotifications(FunctionalTester $I)
54+
{
55+
$I->registerUser('[email protected]', '123456', followRedirects: false);
56+
$notifications = $I->grabSentNotifications();
57+
$subject = $notifications[0]->getSubject();
58+
$I->assertSame('Account created!', $subject);
59+
}
60+
61+
public function seeNotificationIsSent(FunctionalTester $I)
62+
{
63+
$I->registerUser('[email protected]', '123456', followRedirects: false);
64+
$I->seeNotificationIsSent();
65+
}
66+
}

0 commit comments

Comments
 (0)