单元测试 Symfony Messenger

2022-08-31 00:10:21

我一直在我的最新项目中使用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);
    }
}

答案 1

已通过设置带有 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));

答案 2

推荐