From 12451142576e7c3bd7c97e9ac93bbb1c07b90420 Mon Sep 17 00:00:00 2001 From: Pavel Kirpitsov Date: Thu, 29 Apr 2021 11:13:57 +0300 Subject: [PATCH] [Notifier] Fix return SentMessage then Messenger not used --- src/Symfony/Component/Notifier/Chatter.php | 4 +- .../Component/Notifier/Tests/ChatterTest.php | 63 +++++++++++++++++++ .../Component/Notifier/Tests/TexterTest.php | 62 ++++++++++++++++++ src/Symfony/Component/Notifier/Texter.php | 4 +- src/Symfony/Component/Notifier/composer.json | 3 +- 5 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 src/Symfony/Component/Notifier/Tests/ChatterTest.php create mode 100644 src/Symfony/Component/Notifier/Tests/TexterTest.php diff --git a/src/Symfony/Component/Notifier/Chatter.php b/src/Symfony/Component/Notifier/Chatter.php index 81e3984715..8db4ae5e49 100644 --- a/src/Symfony/Component/Notifier/Chatter.php +++ b/src/Symfony/Component/Notifier/Chatter.php @@ -51,9 +51,7 @@ final class Chatter implements ChatterInterface public function send(MessageInterface $message): ?SentMessage { if (null === $this->bus) { - $this->transport->send($message); - - return null; + return $this->transport->send($message); } if (null !== $this->dispatcher) { diff --git a/src/Symfony/Component/Notifier/Tests/ChatterTest.php b/src/Symfony/Component/Notifier/Tests/ChatterTest.php new file mode 100644 index 0000000000..91b0b435e5 --- /dev/null +++ b/src/Symfony/Component/Notifier/Tests/ChatterTest.php @@ -0,0 +1,63 @@ +transport = $this->createMock(TransportInterface::class); + $this->bus = $this->createMock(MessageBusInterface::class); + } + + public function testSendWithoutBus() + { + $message = new DummyMessage(); + + $sentMessage = new SentMessage($message, 'any'); + + $this->transport + ->expects($this->once()) + ->method('send') + ->with($message) + ->willReturn($sentMessage); + + $chatter = new Chatter($this->transport); + $this->assertSame($sentMessage, $chatter->send($message)); + $this->assertSame($message, $sentMessage->getOriginalMessage()); + } + + public function testSendWithBus() + { + $message = new DummyMessage(); + + $this->transport + ->expects($this->never()) + ->method('send') + ->with($message); + + $this->bus + ->expects($this->once()) + ->method('dispatch') + ->with($message) + ->willReturn(new Envelope(new \stdClass())); + + $chatter = new Chatter($this->transport, $this->bus); + $this->assertNull($chatter->send($message)); + } +} diff --git a/src/Symfony/Component/Notifier/Tests/TexterTest.php b/src/Symfony/Component/Notifier/Tests/TexterTest.php new file mode 100644 index 0000000000..1d695d65af --- /dev/null +++ b/src/Symfony/Component/Notifier/Tests/TexterTest.php @@ -0,0 +1,62 @@ +transport = $this->createMock(TransportInterface::class); + $this->bus = $this->createMock(MessageBusInterface::class); + } + + public function testSendWithoutBus() + { + $message = new DummyMessage(); + $sentMessage = new SentMessage($message, 'any'); + + $this->transport + ->expects($this->once()) + ->method('send') + ->with($message) + ->willReturn($sentMessage); + + $texter = new Texter($this->transport); + $this->assertSame($sentMessage, $texter->send($message)); + $this->assertSame($message, $sentMessage->getOriginalMessage()); + } + + public function testSendWithBus() + { + $message = new DummyMessage(); + + $this->transport + ->expects($this->never()) + ->method('send') + ->with($message); + + $this->bus + ->expects($this->once()) + ->method('dispatch') + ->with($message) + ->willReturn(new Envelope(new \stdClass())); + + $texter = new Texter($this->transport, $this->bus); + $this->assertNull($texter->send($message)); + } +} diff --git a/src/Symfony/Component/Notifier/Texter.php b/src/Symfony/Component/Notifier/Texter.php index 4414b248d8..190feec88f 100644 --- a/src/Symfony/Component/Notifier/Texter.php +++ b/src/Symfony/Component/Notifier/Texter.php @@ -51,9 +51,7 @@ final class Texter implements TexterInterface public function send(MessageInterface $message): ?SentMessage { if (null === $this->bus) { - $this->transport->send($message); - - return null; + return $this->transport->send($message); } if (null !== $this->dispatcher) { diff --git a/src/Symfony/Component/Notifier/composer.json b/src/Symfony/Component/Notifier/composer.json index 02178970b0..3e04a0479a 100644 --- a/src/Symfony/Component/Notifier/composer.json +++ b/src/Symfony/Component/Notifier/composer.json @@ -22,7 +22,8 @@ }, "require-dev": { "symfony/event-dispatcher-contracts": "^2", - "symfony/http-client-contracts": "^2" + "symfony/http-client-contracts": "^2", + "symfony/messenger": "^4.4 || ^5.0" }, "conflict": { "symfony/http-kernel": "<4.4",