2017-09-30 13:16:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Messenger\Tests;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
|
2018-04-16 12:07:19 +01:00
|
|
|
use Symfony\Component\Messenger\Envelope;
|
2017-09-30 13:16:30 +01:00
|
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
2018-04-18 14:40:19 +01:00
|
|
|
use Symfony\Component\Messenger\Tests\Fixtures\CallbackReceiver;
|
2017-09-30 13:16:30 +01:00
|
|
|
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
|
|
|
|
use Symfony\Component\Messenger\Worker;
|
|
|
|
|
|
|
|
class WorkerTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testWorkerDispatchTheReceivedMessage()
|
|
|
|
{
|
2018-04-16 12:07:19 +01:00
|
|
|
$apiMessage = new DummyMessage('API');
|
|
|
|
$ipaMessage = new DummyMessage('IPA');
|
|
|
|
|
|
|
|
$receiver = new CallbackReceiver(function ($handler) use ($apiMessage, $ipaMessage) {
|
|
|
|
$handler(Envelope::wrap($apiMessage));
|
|
|
|
$handler(Envelope::wrap($ipaMessage));
|
2017-09-30 13:16:30 +01:00
|
|
|
});
|
|
|
|
|
2018-03-13 16:34:51 +00:00
|
|
|
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
|
2017-09-30 13:16:30 +01:00
|
|
|
|
2018-04-16 12:07:19 +01:00
|
|
|
$bus->expects($this->at(0))->method('dispatch')->with(Envelope::wrap($apiMessage)->with(new ReceivedMessage()));
|
|
|
|
$bus->expects($this->at(1))->method('dispatch')->with(Envelope::wrap($ipaMessage)->with(new ReceivedMessage()));
|
2017-09-30 13:16:30 +01:00
|
|
|
|
|
|
|
$worker = new Worker($receiver, $bus);
|
|
|
|
$worker->run();
|
|
|
|
}
|
|
|
|
|
2018-04-16 12:07:19 +01:00
|
|
|
public function testWorkerDoesNotWrapMessagesAlreadyWrappedWithReceivedMessage()
|
2017-09-30 13:16:30 +01:00
|
|
|
{
|
2018-04-16 12:07:19 +01:00
|
|
|
$envelop = Envelope::wrap(new DummyMessage('API'))->with(new ReceivedMessage());
|
|
|
|
$receiver = new CallbackReceiver(function ($handler) use ($envelop) {
|
|
|
|
$handler($envelop);
|
2017-09-30 13:16:30 +01:00
|
|
|
});
|
|
|
|
|
2018-03-13 16:34:51 +00:00
|
|
|
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
|
2017-09-30 13:16:30 +01:00
|
|
|
|
2018-04-16 12:07:19 +01:00
|
|
|
$bus->expects($this->at(0))->method('dispatch')->with($envelop);
|
2017-09-30 13:16:30 +01:00
|
|
|
|
|
|
|
$worker = new Worker($receiver, $bus);
|
|
|
|
$worker->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkerIsThrowingExceptionsBackToGenerators()
|
|
|
|
{
|
2018-03-13 16:34:51 +00:00
|
|
|
$receiver = new CallbackReceiver(function ($handler) {
|
2017-09-30 13:16:30 +01:00
|
|
|
try {
|
2018-04-16 12:07:19 +01:00
|
|
|
$handler(Envelope::wrap(new DummyMessage('Hello')));
|
2017-09-30 13:16:30 +01:00
|
|
|
|
|
|
|
$this->assertTrue(false, 'This should not be called because the exception is sent back to the generator.');
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// This should be called because of the exception sent back to the generator.
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-13 16:34:51 +00:00
|
|
|
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
|
2017-09-30 13:16:30 +01:00
|
|
|
$bus->method('dispatch')->willThrowException(new \InvalidArgumentException('Why not'));
|
|
|
|
|
|
|
|
$worker = new Worker($receiver, $bus);
|
|
|
|
$worker->run();
|
|
|
|
}
|
2018-03-13 16:34:51 +00:00
|
|
|
|
|
|
|
public function testWorkerDoesNotSendNullMessagesToTheBus()
|
|
|
|
{
|
|
|
|
$receiver = new CallbackReceiver(function ($handler) {
|
|
|
|
$handler(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
|
|
|
|
$bus->expects($this->never())->method('dispatch');
|
|
|
|
|
|
|
|
$worker = new Worker($receiver, $bus);
|
|
|
|
$worker->run();
|
|
|
|
}
|
2017-09-30 13:16:30 +01:00
|
|
|
}
|