Я возился с реализацией AMQP с использованием Symfony Messenger в моем последнем проекте. Хотя код работает к моей большой радости, я не могу написать модульные тесты для методов, которые отправляют сообщения.
Все мои тесты с кодом, который отправляет сообщение, приводят к следующим предупреждениям:
Class "Symfony\Component\Messenger\Envelope" is declared "final" and cannot be mocked.
Я не могу найти документацию на сайте Symfony, посвященную тестам Symfony Messenger.
Как я могу проверить свой код с помощью модульных тестов с сообщениями?
Учебный класс:
namespace App\MessageHandler;
use App\Entity\File;
use App\Message\CheckVideoMessage;
use App\Message\GenerateThumbnailMessage;
use App\Message\GetVideoMetadataMessage;
use App\Message\ProcessFileMessage;
use App\Service\MediaProcessorService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class ProcessFileMessageHandler
{
/**
* @var MediaProcessorService
*/
private $mediaProcessorService;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var MessageBusInterface
*/
private $messageBus;
public function __construct(
MediaProcessorService $mediaProcessorService,
EntityManagerInterface $entityManager,
LoggerInterface $logger,
MessageBusInterface $messageBus
) {
$this->mediaProcessorService = $mediaProcessorService;
$this->entityManager = $entityManager;
$this->logger = $logger;
$this->messageBus = $messageBus;
}
public function __invoke(ProcessFileMessage $message)
{
/** @var File $file */
$file = $this->entityManager->find(File::class, $message->getFileId());
if (!$file->getInode()->getMeta()) {
$this->messageBus->dispatch(new GetVideoMetadataMessage($file->getId()));
}
// This event will also dispatch message to generate thumbnails if the video is valid
$this->messageBus->dispatch(new CheckVideoMessage($file->getId()));
$this->messageBus->dispatch(new GenerateThumbnailMessage($file->getId()));
}
}
TestFile:
namespace App\Tests\MessageHandler;
use App\Entity\Inode;
use App\MessageHandler\ProcessFileMessageHandler;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use App\Entity\File;
use App\Message\CheckVideoMessage;
use App\Message\GenerateThumbnailMessage;
use App\Message\GetVideoMetadataMessage;
use App\Message\ProcessFileMessage;
use App\Service\MediaProcessorService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class ProcessFileMessageHandlerTest extends TestCase
{
/**
* @var MediaProcessorService|MockObject
*/
private $mediaProcessorService;
/**
* @var EntityManagerInterface|MockObject
*/
private $entityManager;
/**
* @var LoggerInterface|MockObject
*/
private $logger;
/**
* @var MessageBusInterface|MockObject
*/
private $messageBus;
/**
* @var ProcessFileMessageHandler
*/
private $handler;
protected function setUp()
{
$this->mediaProcessorService = $this->getMockBuilder(MediaProcessorService::class)
->disableOriginalConstructor()
->getMock();
$this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->logger = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->messageBus = $this->getMockBuilder(MessageBusInterface::class)
->disableOriginalConstructor()
->getMock();
$this->handler = new ProcessFileMessageHandler(
$this->mediaProcessorService,
$this->entityManager,
$this->logger,
$this->messageBus
);
}
/**
* @test
*/
public function willDispatchAllMessagesOnANewFile()
{
$message = new ProcessFileMessage(1);
$inode = (new Inode())->setMeta(false);
$file = (new File())->setId(1)->setInode($inode);
$this->entityManager->expects($this->once())
->method('find')
->willReturn($file);
$this->messageBus->expects(self::exactly(3))
->method('dispatch')
->withConsecutive(
self::isInstanceOf(GetVideoMetadataMessage::class),
self::isInstanceOf(CheckVideoMessage::class),
self::isInstanceOf(GenerateThumbnailMessage::class)
);
$handler = $this->handler;
$handler($message);
}
}
Проблема была решена путем установки возврата с объектом Envelope.
$this->messageBus->expects(self::exactly(3))
->method('dispatch')
->withConsecutive(
self::isInstanceOf(GetVideoMetadataMessage::class),
self::isInstanceOf(CheckVideoMessage::class),
self::isInstanceOf(GenerateThumbnailMessage::class)
)
->willReturn(new Symfony\Component\Messenger\Envelope($message));
Других решений пока нет …