Skip to content

Commit 67ce755

Browse files
Add notifier cest (#34)
* add notifier cest * fix alphabetical order of checks
1 parent 0481788 commit 67ce755

File tree

9 files changed

+205
-2
lines changed

9 files changed

+205
-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
@@ -22,6 +22,7 @@
2222
"symfony/framework-bundle": "7.3.*",
2323
"symfony/http-client": "7.3.*",
2424
"symfony/mailer": "7.3.*",
25+
"symfony/notifier": "7.3.*",
2526
"symfony/runtime": "7.3.*",
2627
"symfony/security-bundle": "7.3.*",
2728
"symfony/translation": "7.3.*",

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
@@ -30,6 +30,10 @@
3030
$framework->mailer()
3131
->dsn('%env(MAILER_DSN)%');
3232

33+
// Notifier
34+
$framework->notifier()
35+
->chatterTransport('slack', '%env(NOTIFIER_DSN)%');
36+
3337
// PropertyInfo
3438
$framework->propertyInfo()
3539
->withConstructorExtractor(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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@
184184
"ref": "fadbfe33303a76e25cb63401050439aa9b1a9c7f"
185185
}
186186
},
187+
"symfony/notifier": {
188+
"version": "7.3",
189+
"recipe": {
190+
"repo": "github.com/symfony/recipes",
191+
"branch": "main",
192+
"version": "5.0",
193+
"ref": "178877daf79d2dbd62129dd03612cb1a2cb407cc"
194+
},
195+
"files": [
196+
"config/packages/notifier.yaml"
197+
]
198+
},
187199
"symfony/property-info": {
188200
"version": "7.3",
189201
"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('jane_doe@gmail.com', '123456', followRedirects: false);
14+
$notification = $I->getNotifierMessage();
15+
$I->assertNotificationSubjectContains($notification, 'created!');
16+
}
17+
18+
public function assertNotificationSubjectNotContains(FunctionalTester $I)
19+
{
20+
$I->registerUser('jane_doe@gmail.com', '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('jane_doe@gmail.com', '123456', followRedirects: false);
28+
$notification = $I->getNotifierMessage();
29+
$I->assertNotificationTransportIsEqual($notification);
30+
}
31+
32+
public function assertNotificationTransportIsNotEqual(FunctionalTester $I)
33+
{
34+
$I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false);
35+
$notification = $I->getNotifierMessage();
36+
$I->assertNotificationTransportIsNotEqual($notification, 'chat');
37+
}
38+
39+
public function dontSeeNotificationIsSent(FunctionalTester $I)
40+
{
41+
$I->registerUser('john_doe@gmail.com', '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('jane_doe@gmail.com', '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('jane_doe@gmail.com', '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('jane_doe@gmail.com', '123456', followRedirects: false);
64+
$I->seeNotificationIsSent();
65+
}
66+
}

0 commit comments

Comments
 (0)