This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Messenger/Tests/WorkerTest.php

106 lines
3.2 KiB
PHP
Raw Normal View History

<?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;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Transport\ReceiverInterface;
use Symfony\Component\Messenger\Worker;
class WorkerTest extends TestCase
{
public function testWorkerDispatchTheReceivedMessage()
{
2018-03-13 16:34:51 +00:00
$receiver = new CallbackReceiver(function ($handler) {
$handler(new DummyMessage('API'));
$handler(new DummyMessage('IPA'));
});
2018-03-13 16:34:51 +00:00
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
$bus->expects($this->at(0))->method('dispatch')->with(new ReceivedMessage(new DummyMessage('API')));
$bus->expects($this->at(1))->method('dispatch')->with(new ReceivedMessage(new DummyMessage('IPA')));
$worker = new Worker($receiver, $bus);
$worker->run();
}
public function testWorkerDoesNotWrapMessagesAlreadyWrappedInReceivedMessages()
{
2018-03-13 16:34:51 +00:00
$receiver = new CallbackReceiver(function ($handler) {
$handler(new ReceivedMessage(new DummyMessage('API')));
});
2018-03-13 16:34:51 +00:00
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
$bus->expects($this->at(0))->method('dispatch')->with(new ReceivedMessage(new DummyMessage('API')));
$worker = new Worker($receiver, $bus);
$worker->run();
}
public function testWorkerIsThrowingExceptionsBackToGenerators()
{
2018-03-13 16:34:51 +00:00
$receiver = new CallbackReceiver(function ($handler) {
try {
2018-03-13 16:34:51 +00:00
$handler(new DummyMessage('Hello'));
$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();
$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();
}
}
class CallbackReceiver implements ReceiverInterface
{
private $callable;
public function __construct(callable $callable)
{
$this->callable = $callable;
}
2018-03-13 16:34:51 +00:00
public function receive(callable $handler): void
{
$callable = $this->callable;
2018-03-13 16:34:51 +00:00
$callable($handler);
}
2018-03-13 16:34:51 +00:00
public function stop(): void
{
}
}