From 1226c4c6bd89ec08c0a1719c70e47079c25eba28 Mon Sep 17 00:00:00 2001 From: d-mitrofanov-v Date: Sun, 9 Nov 2025 17:17:51 +0200 Subject: [PATCH 1/2] add notifier cest --- .env | 4 ++ .env.test | 3 +- composer.json | 1 + composer.lock | 84 ++++++++++++++++++++++- config/packages/framework.php | 4 ++ src/Controller/RegistrationController.php | 4 ++ src/Utils/Notifier.php | 29 ++++++++ symfony.lock | 12 ++++ tests/Functional/NotifierCest.php | 66 ++++++++++++++++++ 9 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 src/Utils/Notifier.php create mode 100644 tests/Functional/NotifierCest.php diff --git a/.env b/.env index 46b026c..726c7e5 100644 --- a/.env +++ b/.env @@ -14,3 +14,7 @@ DATABASE_URL=sqlite:///%kernel.project_dir%/var/test.db3 ###> symfony/mailer ### MAILER_DSN=null://null ###< symfony/mailer ### + +###> symfony/notifier ### +NOTIFIER_DSN=null://null +###< symfony/notifier ### diff --git a/.env.test b/.env.test index 3f898bf..07c06b7 100644 --- a/.env.test +++ b/.env.test @@ -3,4 +3,5 @@ APP_SECRET='$ecretf0rt3st' DATABASE_URL=sqlite:///%kernel.project_dir%/var/test.db3 KERNEL_CLASS='App\Kernel' MAILER_DSN=null://null -SYMFONY_DEPRECATIONS_HELPER=999999 \ No newline at end of file +NOTIFIER_DSN=null://null +SYMFONY_DEPRECATIONS_HELPER=999999 diff --git a/composer.json b/composer.json index 4631cb0..9ce6e72 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "symfony/framework-bundle": "7.3.*", "symfony/http-client": "7.3.*", "symfony/mailer": "7.3.*", + "symfony/notifier": "7.3.*", "symfony/runtime": "7.3.*", "symfony/security-bundle": "7.3.*", "symfony/translation": "7.3.*", diff --git a/composer.lock b/composer.lock index 8a47128..576f1b6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e6f1d4d752e5fe26c01e3d72a447eeb5", + "content-hash": "828dd793a79b92d47f14551e7b557616", "packages": [ { "name": "doctrine/dbal", @@ -2973,6 +2973,88 @@ ], "time": "2025-07-15T13:41:35+00:00" }, + { + "name": "symfony/notifier", + "version": "v7.3.3", + "source": { + "type": "git", + "url": "https://github.com/symfony/notifier.git", + "reference": "33e91495d9674b6ba5e2a1de810902ba976156f5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/notifier/zipball/33e91495d9674b6ba5e2a1de810902ba976156f5", + "reference": "33e91495d9674b6ba5e2a1de810902ba976156f5", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "psr/log": "^1|^2|^3" + }, + "conflict": { + "symfony/event-dispatcher": "<6.4", + "symfony/event-dispatcher-contracts": "<2.5", + "symfony/http-client-contracts": "<2.5", + "symfony/http-kernel": "<6.4" + }, + "require-dev": { + "symfony/event-dispatcher-contracts": "^2.5|^3", + "symfony/http-client-contracts": "^2.5|^3", + "symfony/http-foundation": "^6.4|^7.0", + "symfony/messenger": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Notifier\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Sends notifications via one or more channels (email, SMS, ...)", + "homepage": "https://symfony.com", + "keywords": [ + "notification", + "notifier" + ], + "support": { + "source": "https://github.com/symfony/notifier/tree/v7.3.3" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2025-08-13T11:49:31+00:00" + }, { "name": "symfony/options-resolver", "version": "v7.3.3", diff --git a/config/packages/framework.php b/config/packages/framework.php index 61b342a..b5f6d7d 100644 --- a/config/packages/framework.php +++ b/config/packages/framework.php @@ -30,6 +30,10 @@ $framework->mailer() ->dsn('%env(MAILER_DSN)%'); + // Notifier + $framework->notifier() + ->chatterTransport('slack', '%env(NOTIFIER_DSN)%'); + // PropertyInfo $framework->propertyInfo() ->withConstructorExtractor(true); diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index edb54d4..4e90395 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -9,6 +9,7 @@ use App\Form\RegistrationFormType; use App\Repository\Model\UserRepositoryInterface; use App\Utils\Mailer; +use App\Utils\Notifier; use Psr\EventDispatcher\EventDispatcherInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -18,6 +19,7 @@ final class RegistrationController extends AbstractController { public function __construct( private readonly Mailer $mailer, + private readonly Notifier $notifier, private readonly UserRepositoryInterface $userRepository, private readonly EventDispatcherInterface $eventDispatcher, ) { @@ -34,6 +36,8 @@ public function __invoke(Request $request): Response $this->mailer->sendConfirmationEmail($user); + $this->notifier->sendConfirmationNotification($user); + $this->eventDispatcher->dispatch(new UserRegisteredEvent()); return $this->redirectToRoute('app_login'); diff --git a/src/Utils/Notifier.php b/src/Utils/Notifier.php new file mode 100644 index 0000000..8c72e66 --- /dev/null +++ b/src/Utils/Notifier.php @@ -0,0 +1,29 @@ +content("Account for {$user->getEmail()} created successfully"); + + $recipient = new Recipient($user->getEmail()); + + $this->notifier->send($notification, $recipient); + + return $notification; + } +} diff --git a/symfony.lock b/symfony.lock index 99244d3..f010384 100644 --- a/symfony.lock +++ b/symfony.lock @@ -184,6 +184,18 @@ "ref": "fadbfe33303a76e25cb63401050439aa9b1a9c7f" } }, + "symfony/notifier": { + "version": "7.3", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "main", + "version": "5.0", + "ref": "178877daf79d2dbd62129dd03612cb1a2cb407cc" + }, + "files": [ + "config/packages/notifier.yaml" + ] + }, "symfony/property-info": { "version": "7.3", "recipe": { diff --git a/tests/Functional/NotifierCest.php b/tests/Functional/NotifierCest.php new file mode 100644 index 0000000..20ace9e --- /dev/null +++ b/tests/Functional/NotifierCest.php @@ -0,0 +1,66 @@ +registerUser('john_doe@gmail.com', '123456', followRedirects: false); + // There is already an account with this notification + $I->dontSeeNotificationIsSent(); + } + + public function grabLastSentNotification(FunctionalTester $I) + { + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); + $notification = $I->grabLastSentNotification(); + $I->assertSame('Account created!', $notification->getSubject()); + } + + public function grabSentNotifications(FunctionalTester $I) + { + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); + $notifications = $I->grabSentNotifications(); + $subject = $notifications[0]->getSubject(); + $I->assertSame('Account created!', $subject); + } + + public function seeNotificationIsSent(FunctionalTester $I) + { + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); + $I->seeNotificationIsSent(); + } + + public function assertNotificationSubjectContains(FunctionalTester $I) + { + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); + $notification = $I->getNotifierMessage(); + $I->assertNotificationSubjectContains($notification, 'created!'); + } + + public function assertNotificationSubjectNotContains(FunctionalTester $I) + { + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); + $notification = $I->getNotifierMessage(); + $I->assertNotificationSubjectNotContains($notification, 'Account not created!'); + } + + public function assertNotificationTransportIsEqual(FunctionalTester $I) + { + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); + $notification = $I->getNotifierMessage(); + $I->assertNotificationTransportIsEqual($notification); + } + + public function assertNotificationTransportIsNotEqual(FunctionalTester $I) + { + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); + $notification = $I->getNotifierMessage(); + $I->assertNotificationTransportIsNotEqual($notification, 'chat'); + } +} From 6e7d7689c8853f103020a860e603180c8272a8ec Mon Sep 17 00:00:00 2001 From: d-mitrofanov-v Date: Sun, 9 Nov 2025 17:37:42 +0200 Subject: [PATCH 2/2] fix alphabetical order of checks --- tests/Functional/NotifierCest.php | 52 +++++++++++++++---------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/tests/Functional/NotifierCest.php b/tests/Functional/NotifierCest.php index 20ace9e..0f3e250 100644 --- a/tests/Functional/NotifierCest.php +++ b/tests/Functional/NotifierCest.php @@ -8,59 +8,59 @@ final class NotifierCest { - public function dontSeeNotificationIsSent(FunctionalTester $I) + public function assertNotificationSubjectContains(FunctionalTester $I) { - $I->registerUser('john_doe@gmail.com', '123456', followRedirects: false); - // There is already an account with this notification - $I->dontSeeNotificationIsSent(); + $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); + $notification = $I->getNotifierMessage(); + $I->assertNotificationSubjectContains($notification, 'created!'); } - public function grabLastSentNotification(FunctionalTester $I) + public function assertNotificationSubjectNotContains(FunctionalTester $I) { $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); - $notification = $I->grabLastSentNotification(); - $I->assertSame('Account created!', $notification->getSubject()); + $notification = $I->getNotifierMessage(); + $I->assertNotificationSubjectNotContains($notification, 'Account not created!'); } - public function grabSentNotifications(FunctionalTester $I) + public function assertNotificationTransportIsEqual(FunctionalTester $I) { $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); - $notifications = $I->grabSentNotifications(); - $subject = $notifications[0]->getSubject(); - $I->assertSame('Account created!', $subject); + $notification = $I->getNotifierMessage(); + $I->assertNotificationTransportIsEqual($notification); } - public function seeNotificationIsSent(FunctionalTester $I) + public function assertNotificationTransportIsNotEqual(FunctionalTester $I) { $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); - $I->seeNotificationIsSent(); + $notification = $I->getNotifierMessage(); + $I->assertNotificationTransportIsNotEqual($notification, 'chat'); } - public function assertNotificationSubjectContains(FunctionalTester $I) + public function dontSeeNotificationIsSent(FunctionalTester $I) { - $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); - $notification = $I->getNotifierMessage(); - $I->assertNotificationSubjectContains($notification, 'created!'); + $I->registerUser('john_doe@gmail.com', '123456', followRedirects: false); + // There is already an account with this notification + $I->dontSeeNotificationIsSent(); } - public function assertNotificationSubjectNotContains(FunctionalTester $I) + public function grabLastSentNotification(FunctionalTester $I) { $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); - $notification = $I->getNotifierMessage(); - $I->assertNotificationSubjectNotContains($notification, 'Account not created!'); + $notification = $I->grabLastSentNotification(); + $I->assertSame('Account created!', $notification->getSubject()); } - public function assertNotificationTransportIsEqual(FunctionalTester $I) + public function grabSentNotifications(FunctionalTester $I) { $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); - $notification = $I->getNotifierMessage(); - $I->assertNotificationTransportIsEqual($notification); + $notifications = $I->grabSentNotifications(); + $subject = $notifications[0]->getSubject(); + $I->assertSame('Account created!', $subject); } - public function assertNotificationTransportIsNotEqual(FunctionalTester $I) + public function seeNotificationIsSent(FunctionalTester $I) { $I->registerUser('jane_doe@gmail.com', '123456', followRedirects: false); - $notification = $I->getNotifierMessage(); - $I->assertNotificationTransportIsNotEqual($notification, 'chat'); + $I->seeNotificationIsSent(); } }