[Notifier] Fix return SentMessage then Messenger not used

This commit is contained in:
Pavel Kirpitsov 2021-04-29 11:13:57 +03:00
parent 2ad08d5a02
commit 1245114257
5 changed files with 129 additions and 7 deletions

View File

@ -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) {

View File

@ -0,0 +1,63 @@
<?php
namespace Symfony\Component\Notifier\Tests;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Notifier\Chatter;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
use Symfony\Component\Notifier\Transport\TransportInterface;
class ChatterTest extends TestCase
{
/** @var MockObject&TransportInterface */
private $transport;
/** @var MockObject&MessageBusInterface */
private $bus;
protected function setUp(): void
{
$this->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));
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace Symfony\Component\Notifier\Tests;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
use Symfony\Component\Notifier\Texter;
use Symfony\Component\Notifier\Transport\TransportInterface;
class TexterTest extends TestCase
{
/** @var MockObject&TransportInterface */
private $transport;
/** @var MockObject&MessageBusInterface */
private $bus;
protected function setUp(): void
{
$this->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));
}
}

View File

@ -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) {

View File

@ -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",