Skip to content

Commit

Permalink
feature #51276 [Notifier] Transport possible to have null (StaffNowa)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.4 branch.

Discussion
----------

[Notifier] Transport possible to have null

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #51236
| License       | MIT

My test example of why I need to have properly transport null

```php
public function testSmsSending(): void
{
    $date = (new \DateTimeImmutable())->format('Y-m-d');

    $stateta = new Stateta();
    $stateta->setDate(new \DateTimeImmutable($date));
    $stateta->setA95(0);
    $stateta->setDk(0);

    $this->entityManager->persist($stateta);
    $this->entityManager->flush();

    $this->commandTester->execute([], [
        'verbosity' => OutputInterface::VERBOSITY_DEBUG
    ]);

    $this->commandTester->assertCommandIsSuccessful();

    $notifierEvent = $this->getNotifierEvent();

    self::assertEquals(null, $notifierEvent->getMessage()->getTransport());
    self::assertEquals("+37061234567", $notifierEvent->getMessage()->getRecipientId());

    self::assertNotificationIsQueued($notifierEvent);
    self::assertNotificationSubjectContains($notifierEvent->getMessage(), 'Company name');
    self::assertNotificationTransportIsEqual($notifierEvent->getMessage(), null);
}
```

Commits
-------

6120c4b [Notifier] Transport possible to have null
  • Loading branch information
fabpot committed Oct 2, 2023
2 parents 693276e + 6120c4b commit 96687e3
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
Expand Up @@ -52,12 +52,12 @@ public static function assertNotificationSubjectNotContains(MessageInterface $no
self::assertThat($notification, new LogicalNot(new NotifierConstraint\NotificationSubjectContains($text)), $message);
}

public static function assertNotificationTransportIsEqual(MessageInterface $notification, string $transportName, string $message = ''): void
public static function assertNotificationTransportIsEqual(MessageInterface $notification, string $transportName = null, string $message = ''): void
{
self::assertThat($notification, new NotifierConstraint\NotificationTransportIsEqual($transportName), $message);
}

public static function assertNotificationTransportIsNotEqual(MessageInterface $notification, string $transportName, string $message = ''): void
public static function assertNotificationTransportIsNotEqual(MessageInterface $notification, string $transportName = null, string $message = ''): void
{
self::assertThat($notification, new LogicalNot(new NotifierConstraint\NotificationTransportIsEqual($transportName)), $message);
}
Expand Down
Expand Up @@ -14,21 +14,25 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;

final class NotificationController
{
public function indexAction(NotifierInterface $notifier)
{
$firstNotification = new Notification('Hello World!', ['chat/slack']);
$firstNotification->content('Symfony is awesome!');

$notifier->send($firstNotification);

$secondNotification = (new Notification('New urgent notification'))
->importance(Notification::IMPORTANCE_URGENT)
;
$notifier->send($secondNotification);

$thirdNotification = new Notification('Hello World!', ['sms']);
$thirdNotification->content('Symfony is awesome!');
$notifier->send($thirdNotification, new Recipient('', '112'));

return new Response();
}
}
Expand Up @@ -21,9 +21,10 @@ public function testNotifierAssertion()
$client = $this->createClient(['test_case' => 'Notifier', 'root_config' => 'config.yml', 'debug' => true]);
$client->request('GET', '/send_notification');

$this->assertNotificationCount(2);
$this->assertNotificationCount(3);
$first = 0;
$second = 1;
$third = 2;
$this->assertNotificationIsNotQueued($this->getNotifierEvent($first));
$this->assertNotificationIsNotQueued($this->getNotifierEvent($second));

Expand All @@ -38,5 +39,9 @@ public function testNotifierAssertion()
$this->assertNotificationSubjectNotContains($notification, 'Hello World!');
$this->assertNotificationTransportIsEqual($notification, 'mercure');
$this->assertNotificationTransportIsNotEqual($notification, 'slack');

$notification = $this->getNotifierMessage($third);
$this->assertNotificationSubjectContains($notification, 'Hello World!');
$this->assertNotificationTransportIsEqual($notification, null);
}
}
Expand Up @@ -13,6 +13,8 @@ framework:
urgent: ['chat/mercure']
admin_recipients:
- { email: admin@example.com }
texter_transports:
smsbiuras: 'null://null'
profiler: ~

mercure:
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Notifier/Message/NullMessage.php
Expand Up @@ -40,6 +40,6 @@ public function getOptions(): ?MessageOptionsInterface

public function getTransport(): ?string
{
return $this->decoratedMessage->getTransport() ?? 'null';
return $this->decoratedMessage->getTransport() ?? null;
}
}
Expand Up @@ -19,9 +19,9 @@
*/
final class NotificationTransportIsEqual extends Constraint
{
private string $expectedText;
private ?string $expectedText;

public function __construct(string $expectedText)
public function __construct(?string $expectedText)
{
$this->expectedText = $expectedText;
}
Expand Down

0 comments on commit 96687e3

Please sign in to comment.