在我的最新项目中,我一直在使用 Symfony Messenger 实现 AMQP。虽然代码让我非常高兴,但我无法为发送消息的方法编写单元测试。
我所有涉及发送消息的代码的测试都会导致以下警告:
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()));
}
}
测试文件:
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);
}
}