Skip to content

Commit 54561bb

Browse files
committed
add NotifierAssertionTrait
1 parent f5f40bc commit 54561bb

File tree

10 files changed

+233
-0
lines changed

10 files changed

+233
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.2
55
---
66

7+
* Add `NotificationAssertionsTrait`
78
* Add option `framework.catch_all_throwables` to allow `Symfony\Component\HttpKernel\HttpKernel` to catch all kinds of `Throwable`
89
* Make `AbstractController::render()` able to deal with forms and deprecate `renderForm()`
910
* Deprecate the `Symfony\Component\Serializer\Normalizer\ObjectNormalizer` and

Test/KernelTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
abstract class KernelTestCase extends TestCase
2727
{
2828
use MailerAssertionsTrait;
29+
use NotificationAssertionsTrait;
2930

3031
protected static $class;
3132

Test/NotificationAssertionsTrait.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Test;
13+
14+
use PHPUnit\Framework\Constraint\LogicalNot;
15+
use Symfony\Component\Notifier\Event\MessageEvent;
16+
use Symfony\Component\Notifier\Event\NotificationEvents;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Test\Constraint as NotifierConstraint;
19+
20+
/*
21+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
22+
*/
23+
trait NotificationAssertionsTrait
24+
{
25+
public static function assertNotificationCount(int $count, string $transport = null, string $message = ''): void
26+
{
27+
self::assertThat(self::getNotificationEvents(), new NotifierConstraint\NotificationCount($count, $transport), $message);
28+
}
29+
30+
public static function assertQueuedNotificationCount(int $count, string $transport = null, string $message = ''): void
31+
{
32+
self::assertThat(self::getMessageMailerEvents(), new NotifierConstraint\NotificationCount($count, $transport, true), $message);
33+
}
34+
35+
public static function assertNotificationIsQueued(MessageEvent $event, string $message = ''): void
36+
{
37+
self::assertThat($event, new NotifierConstraint\NotificationIsQueued(), $message);
38+
}
39+
40+
public static function assertNotificationIsNotQueued(MessageEvent $event, string $message = ''): void
41+
{
42+
self::assertThat($event, new LogicalNot(new NotifierConstraint\NotificationIsQueued()), $message);
43+
}
44+
45+
public static function assertNotificationSubjectContains(MessageInterface $messageObject, string $text, string $message = ''): void
46+
{
47+
self::assertThat($messageObject, new NotifierConstraint\NotificationSubjectContains($text), $message);
48+
}
49+
50+
public static function assertNotificationSubjectNotContains(MessageInterface $messageObject, string $text, string $message = ''): void
51+
{
52+
self::assertThat($messageObject, new LogicalNot(new NotifierConstraint\NotificationSubjectContains($text)), $message);
53+
}
54+
55+
public static function assertNotificationTransportIsEqual(MessageInterface $messageObject, string $text, string $message = ''): void
56+
{
57+
self::assertThat($messageObject, new NotifierConstraint\NotificationTransportIsEqual($text), $message);
58+
}
59+
60+
public static function assertNotificationTransportIsNotEqual(MessageInterface $messageObject, string $text, string $message = ''): void
61+
{
62+
self::assertThat($messageObject, new LogicalNot(new NotifierConstraint\NotificationTransportIsEqual($text)), $message);
63+
}
64+
65+
/**
66+
* @return MessageEvent[]
67+
*/
68+
public static function getNotifierEvents(string $transport = null): array
69+
{
70+
return self::getNotificationEvents()->getEvents($transport);
71+
}
72+
73+
public static function getNotifierEvent(int $index = 0, string $transport = null): ?MessageEvent
74+
{
75+
return self::getNotifierEvents($transport)[$index] ?? null;
76+
}
77+
78+
/**
79+
* @return MessageInterface[]
80+
*/
81+
public static function getNotifierMessages(string $transport = null): array
82+
{
83+
return self::getNotificationEvents()->getMessages($transport);
84+
}
85+
86+
public static function getNotifierMessage(int $index = 0, string $transport = null): ?MessageInterface
87+
{
88+
return self::getNotifierMessages($transport)[$index] ?? null;
89+
}
90+
91+
public static function getNotificationEvents(): NotificationEvents
92+
{
93+
$container = static::getContainer();
94+
if ($container->has('notifier.logger_notification_listener')) {
95+
return $container->get('notifier.logger_notification_listener')->getEvents();
96+
}
97+
98+
static::fail('A client must have Notifier enabled to make notifications assertions. Did you forget to require symfony/notifier?');
99+
}
100+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\Notifier\Notification\Notification;
16+
use Symfony\Component\Notifier\NotifierInterface;
17+
18+
final class NotificationController
19+
{
20+
public function indexAction(NotifierInterface $notifier)
21+
{
22+
$firstNotification = new Notification('Hello World!', ['chat/slack']);
23+
$firstNotification->content('Symfony is awesome!');
24+
25+
$notifier->send($firstNotification);
26+
27+
$secondNotification = (new Notification('New urgent notification'))
28+
->importance(Notification::IMPORTANCE_URGENT)
29+
;
30+
$notifier->send($secondNotification);
31+
32+
return new Response();
33+
}
34+
}

Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ send_email:
6464
uid:
6565
resource: "../../Controller/UidController.php"
6666
type: "annotation"
67+
68+
send_notification:
69+
path: /send_notification
70+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\NotificationController::indexAction }

Tests/Functional/NotificationTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
final class NotificationTest extends AbstractWebTestCase
15+
{
16+
public function testNotifierAssertion()
17+
{
18+
$client = $this->createClient(['test_case' => 'Notifier', 'root_config' => 'config.yml', 'debug' => true]);
19+
$client->request('GET', '/send_notification');
20+
21+
$this->assertNotificationCount(2);
22+
$first = 0;
23+
$second = 1;
24+
$this->assertNotificationIsNotQueued($this->getNotifierEvent($first));
25+
$this->assertNotificationIsNotQueued($this->getNotifierEvent($second));
26+
27+
$notification = $this->getNotifierMessage($first);
28+
$this->assertNotificationSubjectContains($notification, 'Hello World!');
29+
$this->assertNotificationSubjectNotContains($notification, 'New urgent notification');
30+
$this->assertNotificationTransportIsEqual($notification, 'slack');
31+
$this->assertNotificationTransportIsNotEqual($notification, 'mercure');
32+
33+
$notification = $this->getNotifierMessage($second);
34+
$this->assertNotificationSubjectContains($notification, 'New urgent notification');
35+
$this->assertNotificationSubjectNotContains($notification, 'Hello World!');
36+
$this->assertNotificationTransportIsEqual($notification, 'mercure');
37+
$this->assertNotificationTransportIsNotEqual($notification, 'slack');
38+
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
14+
use Symfony\Bundle\MercureBundle\MercureBundle;
15+
16+
return [
17+
new FrameworkBundle(),
18+
new TestBundle(),
19+
new MercureBundle(),
20+
];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
- { resource: services.yml }
4+
5+
framework:
6+
mailer:
7+
dsn: 'null://null'
8+
notifier:
9+
chatter_transports:
10+
slack: 'null://null'
11+
mercure: 'null://null'
12+
channel_policy:
13+
urgent: ['chat/mercure']
14+
admin_recipients:
15+
- { email: admin@example.com }
16+
profiler: ~
17+
18+
mercure:
19+
hubs:
20+
default:
21+
url: 'null://null'
22+
jwt:
23+
secret: '!ChangeMe!'
24+
publish: [ 'foo', 'https://example.com/foo' ]
25+
subscribe: [ 'bar', 'https://example.com/bar' ]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_notifiertest_bundle:
2+
resource: '@TestBundle/Resources/config/routing.yml'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
_defaults:
3+
public: true
4+
5+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\NotificationController:
6+
tags: ['controller.service_arguments']
7+

0 commit comments

Comments
 (0)