Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add unit test for plugin
  • Loading branch information
vladyslav-sikailo-run-as-root committed Apr 11, 2023
commit f38c541bf8fa259d4ac7ba94e82cfe94b71a7162
1 change: 1 addition & 0 deletions src/Plugin/HandleQueueMessageRejectPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function aroundReject(
): void {
if (!$error) {
$proceed($envelope, $requeue, $error);
return;
}

$shouldBeSavedForRetry = $this->isMessageShouldBeSavedForRetryService->execute($envelope);
Expand Down
93 changes: 93 additions & 0 deletions src/Test/Unit/Plugin/HandleQueueMessageRejectPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace RunAsRoot\MessageQueueRetry\Test\Unit\Plugin;

use Magento\Framework\MessageQueue\Envelope;
use Magento\Framework\MessageQueue\QueueInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RunAsRoot\MessageQueueRetry\Plugin\HandleQueueMessageRejectPlugin;
use RunAsRoot\MessageQueueRetry\Service\IsMessageShouldBeSavedForRetryService;
use RunAsRoot\MessageQueueRetry\Service\SaveFailedMessageService;

final class HandleQueueMessageRejectPluginTest extends TestCase
{
private IsMessageShouldBeSavedForRetryService|MockObject $isMessageShouldBeSavedForRetryServiceMock;
private SaveFailedMessageService|MockObject $saveFailedMessageServiceMock;
private QueueInterface|MockObject $queueMock;
private HandleQueueMessageRejectPlugin $sut;
private bool $isProceedCalled;
private \Closure $testProceedFn;

public function setUp(): void
{
$this->isMessageShouldBeSavedForRetryServiceMock = $this->createMock(IsMessageShouldBeSavedForRetryService::class);
$this->saveFailedMessageServiceMock = $this->createMock(SaveFailedMessageService::class);
$this->queueMock = $this->createMock(QueueInterface::class);

$this->isProceedCalled = false;
$this->testProceedFn = fn () => $this->isProceedCalled = true;

$this->sut = new HandleQueueMessageRejectPlugin(
$this->isMessageShouldBeSavedForRetryServiceMock,
$this->saveFailedMessageServiceMock
);
}

public function testItShouldSaveFailedMessageAndNotProceed(): void
{
$testError = 'test error';

$this->isMessageShouldBeSavedForRetryServiceMock->expects($this->once())->method('execute')->willReturn(true);
$this->saveFailedMessageServiceMock->expects($this->once())->method('execute');
$this->queueMock->expects($this->once())->method('acknowledge');

$this->sut->aroundReject(
$this->queueMock,
$this->testProceedFn,
new Envelope('body'),
false,
$testError
);

self::assertFalse($this->isProceedCalled);
}

public function testItProceedIfMessageShouldNotBeSavedForRetry(): void
{
$testError = 'test error';

$this->isMessageShouldBeSavedForRetryServiceMock->expects($this->once())->method('execute')->willReturn(false);
$this->saveFailedMessageServiceMock->expects($this->never())->method('execute');

$this->sut->aroundReject(
$this->queueMock,
$this->testProceedFn,
new Envelope('body'),
false,
$testError
);

self::assertTrue($this->isProceedCalled);
}

public function testItProceedIfThereIsNoError(): void
{
$testEmptyError = '';

$this->saveFailedMessageServiceMock->expects($this->never())->method('execute');
$this->isMessageShouldBeSavedForRetryServiceMock->expects($this->never())->method('execute');

$this->sut->aroundReject(
$this->queueMock,
$this->testProceedFn,
new Envelope('body'),
false,
$testEmptyError
);

self::assertTrue($this->isProceedCalled);
}
}