Skip to content

Commit cc43153

Browse files
Unit tests
1 parent 5ab2f0c commit cc43153

7 files changed

+177
-84
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace RunAsRoot\MessageQueueRetry\Test\Unit\Converter;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use RunAsRoot\MessageQueueRetry\Converter\QueueRetryXmlToArrayConverter;
7+
8+
final class QueueRetryXmlToArrayConverterTest extends TestCase
9+
{
10+
private QueueRetryXmlToArrayConverter $sut;
11+
12+
protected function setUp(): void
13+
{
14+
$this->sut = new QueueRetryXmlToArrayConverter();
15+
}
16+
17+
public function testConvert(): void
18+
{
19+
$doc = new \DOMDocument();
20+
$doc->loadXML($this->getQueueRetryXmlFile());
21+
22+
$result = $this->sut->convert($doc);
23+
24+
$expected = [
25+
'queue_retry_topics' => [
26+
'sample_topic' => [
27+
'topic_name' => 'sample_topic',
28+
'retry_limit' => 3,
29+
],
30+
'another_topic' => [
31+
'topic_name' => 'another_topic',
32+
'retry_limit' => 10,
33+
],
34+
],
35+
];
36+
37+
$this->assertEquals($expected, $result);
38+
}
39+
40+
public function getQueueRetryXmlFile(): string
41+
{
42+
return file_get_contents(__DIR__ . '/_files/queue_retry.xml');
43+
}
44+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:RunAsRoot:module:RunAsRoot_MessageQueueRetry:/etc/queue_retry.xsd">
4+
<topic name="sample_topic" retryLimit="3"/>
5+
<topic name="another_topic" retryLimit="10"/>
6+
</config>

src/Test/Unit/Model/Config/Backend/QueuesConfigTest.php

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace RunAsRoot\MessageQueueRetry\Test\Unit\Repository\Query;
4+
5+
use Magento\Framework\Config\DataInterface;
6+
use PHPUnit\Framework\MockObject\MockObject;
7+
use PHPUnit\Framework\TestCase;
8+
use RunAsRoot\MessageQueueRetry\Config\QueueRetryConfigInterface;
9+
use RunAsRoot\MessageQueueRetry\Repository\Query\FindQueueRetryLimitByTopicNameQuery;
10+
11+
class FindQueueRetryLimitByTopicNameQueryTest extends TestCase
12+
{
13+
private FindQueueRetryLimitByTopicNameQuery $sut;
14+
private DataInterface|MockObject $configStorageMock;
15+
16+
protected function setUp(): void
17+
{
18+
$this->configStorageMock = $this->createMock(DataInterface::class);
19+
$this->sut = new FindQueueRetryLimitByTopicNameQuery($this->configStorageMock);
20+
}
21+
22+
/**
23+
* @dataProvider dataProvider
24+
*/
25+
public function testExecute($expected, $topicName, $config): void
26+
{
27+
$configKey = QueueRetryConfigInterface::CONFIG_KEY_NAME . '/' . $topicName;
28+
$this->configStorageMock->expects($this->once())->method('get')->with($configKey)->willReturn($config);
29+
30+
$result = $this->sut->execute($topicName);
31+
32+
$this->assertEquals($expected, $result);
33+
}
34+
35+
public function dataProvider(): array
36+
{
37+
return [
38+
[3, 'sample_topic', ['retry_limit' => '3']],
39+
[null, 'sample_topic', ['some_key' => '3']],
40+
[null, 'sample_topic', null],
41+
];
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace RunAsRoot\MessageQueueRetry\Test\Unit\SchemaLocator;
4+
5+
use Magento\Framework\Config\Dom\UrnResolver;
6+
use PHPUnit\Framework\MockObject\MockObject;
7+
use PHPUnit\Framework\TestCase;
8+
use RunAsRoot\MessageQueueRetry\Config\QueueRetryConfigInterface;
9+
use RunAsRoot\MessageQueueRetry\SchemaLocator\QueueRetrySchemaLocator;
10+
11+
class QueueRetrySchemaLocatorTest extends TestCase
12+
{
13+
private QueueRetrySchemaLocator $sut;
14+
private UrnResolver|MockObject $urnResolverMock;
15+
16+
protected function setUp(): void
17+
{
18+
$this->urnResolverMock = $this->createMock(UrnResolver::class);
19+
$this->sut = new QueueRetrySchemaLocator($this->urnResolverMock);
20+
}
21+
22+
public function testGetSchema(): void
23+
{
24+
$urn = QueueRetryConfigInterface::XSD_FILE_URN;
25+
$realPath = 'some-path';
26+
$this->urnResolverMock->expects($this->once())->method('getRealPath')->with($urn)->willReturn($realPath);
27+
28+
$result = $this->sut->getSchema();
29+
30+
$this->assertEquals($realPath, $result);
31+
}
32+
33+
public function testGetPerFileSchema(): void
34+
{
35+
$urn = QueueRetryConfigInterface::XSD_FILE_URN;
36+
$realPath = 'some-path';
37+
$this->urnResolverMock->expects($this->once())->method('getRealPath')->with($urn)->willReturn($realPath);
38+
39+
$result = $this->sut->getPerFileSchema();
40+
41+
$this->assertEquals($realPath, $result);
42+
}
43+
}

src/Test/Unit/Service/GetMessageRetriesCountServiceTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
final class GetMessageRetriesCountServiceTest extends TestCase
1313
{
14+
private GetMessageRetriesCountService $sut;
15+
16+
protected function setUp(): void
17+
{
18+
$this->sut = new GetMessageRetriesCountService();
19+
}
20+
1421
public function testItGetsMessageRetriesCount(): void
1522
{
1623
$testRetriesCount = 3;
@@ -21,10 +28,9 @@ public function testItGetsMessageRetriesCount(): void
2128
$messageProperties = ['application_headers' => $applicationHeaders, 'topic_name' => $topicName];
2229
$messageMock->expects($this->once())->method('getProperties')->willReturn($messageProperties);
2330

24-
$sut = new GetMessageRetriesCountService();
25-
$retriesCount = $sut->execute($messageMock);
31+
$retriesCount = $this->sut->execute($messageMock);
2632

27-
self::assertEquals($testRetriesCount, $retriesCount);
33+
$this->assertEquals($testRetriesCount, $retriesCount);
2834
}
2935

3036
public function testItReturnsZeroAfterFirstProcessingBecauseItIsNotRetry(): void
@@ -33,9 +39,21 @@ public function testItReturnsZeroAfterFirstProcessingBecauseItIsNotRetry(): void
3339
$messageProperties = [ ];
3440
$messageMock->expects($this->once())->method('getProperties')->willReturn($messageProperties);
3541

36-
$sut = new GetMessageRetriesCountService();
37-
$retriesCount = $sut->execute($messageMock);
42+
$retriesCount = $this->sut->execute($messageMock);
43+
44+
$this->assertEquals(0, $retriesCount);
45+
}
46+
47+
public function testItReturnsZeroAsFallback(): void
48+
{
49+
$messageMock = $this->createMock(Envelope::class);
50+
$applicationHeaders = new AMQPTable(['x-death' => [['count' => null]]]);
51+
$topicName = 'sample_topic';
52+
$messageProperties = ['application_headers' => $applicationHeaders, 'topic_name' => $topicName];
53+
$messageMock->expects($this->once())->method('getProperties')->willReturn($messageProperties);
54+
55+
$retriesCount = $this->sut->execute($messageMock);
3856

39-
self::assertEquals(0, $retriesCount);
57+
$this->assertEquals(0, $retriesCount);
4058
}
4159
}

src/Test/Unit/Service/IsMessageShouldBeSavedForRetryServiceTest.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Magento\Framework\MessageQueue\Envelope;
88
use PHPUnit\Framework\MockObject\MockObject;
99
use PHPUnit\Framework\TestCase;
10+
use RunAsRoot\MessageQueueRetry\Repository\Query\FindQueueRetryLimitByTopicNameQuery;
1011
use RunAsRoot\MessageQueueRetry\Service\GetMessageRetriesCountService;
1112
use RunAsRoot\MessageQueueRetry\Service\IsMessageShouldBeSavedForRetryService;
1213
use RunAsRoot\MessageQueueRetry\System\Config\MessageQueueRetryConfig;
@@ -16,27 +17,29 @@ final class IsMessageShouldBeSavedForRetryServiceTest extends TestCase
1617
private IsMessageShouldBeSavedForRetryService $sut;
1718
private MessageQueueRetryConfig|MockObject $messageQueueRetryConfigMock;
1819
private GetMessageRetriesCountService|MockObject $getMessageRetriesCountServiceMock;
20+
private FindQueueRetryLimitByTopicNameQuery|MockObject $findQueueRetryLimitByTopicNameQueryMock;
1921

2022
protected function setUp(): void
2123
{
2224
$this->messageQueueRetryConfigMock = $this->createMock(MessageQueueRetryConfig::class);
2325
$this->getMessageRetriesCountServiceMock = $this->createMock(GetMessageRetriesCountService::class);
24-
25-
$this->messageQueueRetryConfigMock->method('isDelayQueueEnabled')->willReturn(true);
26+
$this->findQueueRetryLimitByTopicNameQueryMock = $this->createMock(FindQueueRetryLimitByTopicNameQuery::class);
2627

2728
$this->sut = new IsMessageShouldBeSavedForRetryService(
2829
$this->messageQueueRetryConfigMock,
29-
$this->getMessageRetriesCountServiceMock
30+
$this->getMessageRetriesCountServiceMock,
31+
$this->findQueueRetryLimitByTopicNameQueryMock
3032
);
3133
}
3234

3335
public function testItReturnsTrueIfRetryLimitIsReached(): void
3436
{
3537
$testMessageProperties = ['topic_name' => 'sample_topic'];
36-
$testQueueConfiguration = ['sample_topic' => ['retry_limit' => 2]];
3738

39+
$this->messageQueueRetryConfigMock->method('isDelayQueueEnabled')->willReturn(true);
3840
$this->getMessageRetriesCountServiceMock->expects($this->once())->method('execute')->willReturn(2);
39-
$this->messageQueueRetryConfigMock->expects($this->once())->method('getDelayQueues')->willReturn($testQueueConfiguration);
41+
$this->findQueueRetryLimitByTopicNameQueryMock->expects($this->once())->method('execute')
42+
->with('sample_topic')->willReturn(2);
4043

4144
$result = $this->sut->execute(new Envelope('', $testMessageProperties));
4245
$this->assertTrue($result);
@@ -45,10 +48,11 @@ public function testItReturnsTrueIfRetryLimitIsReached(): void
4548
public function testItReturnsFalseIfRetryLimitIsNotReached(): void
4649
{
4750
$testMessageProperties = ['topic_name' => 'sample_topic'];
48-
$testQueueConfiguration = ['sample_topic' => ['retry_limit' => 2]];
4951

52+
$this->messageQueueRetryConfigMock->method('isDelayQueueEnabled')->willReturn(true);
5053
$this->getMessageRetriesCountServiceMock->expects($this->once())->method('execute')->willReturn(1);
51-
$this->messageQueueRetryConfigMock->expects($this->once())->method('getDelayQueues')->willReturn($testQueueConfiguration);
54+
$this->findQueueRetryLimitByTopicNameQueryMock->expects($this->once())->method('execute')
55+
->with('sample_topic')->willReturn(2);
5256

5357
$result = $this->sut->execute(new Envelope('', $testMessageProperties));
5458
$this->assertFalse($result);
@@ -57,29 +61,19 @@ public function testItReturnsFalseIfRetryLimitIsNotReached(): void
5761
public function testItReturnsFalseIfQueueConfigHasNoRetryLimit(): void
5862
{
5963
$testMessageProperties = ['topic_name' => 'sample_topic'];
60-
$testQueueConfiguration = ['sample_topic' => []];
61-
62-
$this->getMessageRetriesCountServiceMock->expects($this->once())->method('execute')->willReturn(1);
63-
$this->messageQueueRetryConfigMock->expects($this->once())->method('getDelayQueues')->willReturn($testQueueConfiguration);
64-
65-
$result = $this->sut->execute(new Envelope('', $testMessageProperties));
66-
$this->assertFalse($result);
67-
}
68-
69-
public function testItReturnsFalseIfQueueIsNotConfigured(): void
70-
{
71-
$testMessageProperties = ['topic_name' => 'sample_topic'];
72-
$testQueueConfiguration = ['another_topic' => ['retry_limit' => 1]];
7364

65+
$this->messageQueueRetryConfigMock->method('isDelayQueueEnabled')->willReturn(true);
7466
$this->getMessageRetriesCountServiceMock->expects($this->once())->method('execute')->willReturn(1);
75-
$this->messageQueueRetryConfigMock->expects($this->once())->method('getDelayQueues')->willReturn($testQueueConfiguration);
67+
$this->findQueueRetryLimitByTopicNameQueryMock->expects($this->once())->method('execute')
68+
->with('sample_topic')->willReturn(null);
7669

7770
$result = $this->sut->execute(new Envelope('', $testMessageProperties));
7871
$this->assertFalse($result);
7972
}
8073

8174
public function testItReturnsFalseIfMessageHasNoTopicName(): void
8275
{
76+
$this->messageQueueRetryConfigMock->method('isDelayQueueEnabled')->willReturn(true);
8377
$this->getMessageRetriesCountServiceMock->expects($this->once())->method('execute')->willReturn(1);
8478

8579
$result = $this->sut->execute(new Envelope('', []));
@@ -88,6 +82,7 @@ public function testItReturnsFalseIfMessageHasNoTopicName(): void
8882

8983
public function testItReturnsFalseIfItIsFirstTimeConsuming(): void
9084
{
85+
$this->messageQueueRetryConfigMock->method('isDelayQueueEnabled')->willReturn(true);
9186
$this->getMessageRetriesCountServiceMock->expects($this->once())->method('execute')->willReturn(0);
9287

9388
$result = $this->sut->execute(new Envelope('', []));
@@ -96,9 +91,7 @@ public function testItReturnsFalseIfItIsFirstTimeConsuming(): void
9691

9792
public function testItReturnsFalseIfConfigDisabled(): void
9893
{
99-
$this->messageQueueRetryConfigMock->expects($this->once())
100-
->method('isDelayQueueEnabled')
101-
->willReturn(false);
94+
$this->messageQueueRetryConfigMock->expects($this->once())->method('isDelayQueueEnabled')->willReturn(false);
10295

10396
$result = $this->sut->execute(new Envelope('', []));
10497
$this->assertFalse($result);

0 commit comments

Comments
 (0)