Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Notifier] Fix return SentMessage then Messenger not used #40982

Merged
merged 1 commit into from May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Symfony/Component/Notifier/Chatter.php
Expand Up @@ -51,9 +51,7 @@ public function supports(MessageInterface $message): bool
public function send(MessageInterface $message): ?SentMessage
{
if (null === $this->bus) {
$this->transport->send($message);

return null;
return $this->transport->send($message);
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
}

if (null !== $this->dispatcher) {
Expand Down
63 changes: 63 additions & 0 deletions src/Symfony/Component/Notifier/Tests/ChatterTest.php
@@ -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;
WaylandAce marked this conversation as resolved.
Show resolved Hide resolved
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 */
WaylandAce marked this conversation as resolved.
Show resolved Hide resolved
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));
}
}
62 changes: 62 additions & 0 deletions src/Symfony/Component/Notifier/Tests/TexterTest.php
@@ -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;
WaylandAce marked this conversation as resolved.
Show resolved Hide resolved

/** @var MockObject&MessageBusInterface */
WaylandAce marked this conversation as resolved.
Show resolved Hide resolved
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));
}
}
4 changes: 1 addition & 3 deletions src/Symfony/Component/Notifier/Texter.php
Expand Up @@ -51,9 +51,7 @@ public function supports(MessageInterface $message): bool
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) {
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Notifier/composer.json
Expand Up @@ -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",
Expand Down