Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###
3 changes: 2 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
NOTIFIER_DSN=null://null
SYMFONY_DEPRECATIONS_HELPER=999999
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down
84 changes: 83 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions config/packages/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
$framework->mailer()
->dsn('%env(MAILER_DSN)%');

// Notifier
$framework->notifier()
->chatterTransport('slack', '%env(NOTIFIER_DSN)%');

// PropertyInfo
$framework->propertyInfo()
->withConstructorExtractor(true);
Expand Down
4 changes: 4 additions & 0 deletions src/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
Expand All @@ -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');
Expand Down
29 changes: 29 additions & 0 deletions src/Utils/Notifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Utils;

use App\Entity\User;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;

final readonly class Notifier
{
public function __construct(private NotifierInterface $notifier)
{
}

public function sendConfirmationNotification(User $user): Notification
{
$notification = (new Notification('Account created!', ['chat']))
->content("Account for {$user->getEmail()} created successfully");

$recipient = new Recipient($user->getEmail());

$this->notifier->send($notification, $recipient);

return $notification;
}
}
12 changes: 12 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
66 changes: 66 additions & 0 deletions tests/Functional/NotifierCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace App\Tests\Functional;

use App\Tests\Support\FunctionalTester;

final class NotifierCest
{
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');
}

public function dontSeeNotificationIsSent(FunctionalTester $I)
{
$I->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();
}
}