-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathFailureEnvelopeTest.php
More file actions
74 lines (55 loc) · 2.62 KB
/
Copy pathFailureEnvelopeTest.php
File metadata and controls
74 lines (55 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Tests\Unit\Middleware\FailureHandling;
use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Message\GenericMessage;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\FailureHandling\FailureEnvelope;
final class FailureEnvelopeTest extends TestCase
{
public function testConstructor(): void
{
$message = $this->createMessage();
$metadata = ['attempt' => 1, 'error' => 'Test error'];
$envelope = new FailureEnvelope($message, $metadata);
$this->assertSame($message, $envelope->getMessage());
$this->assertArrayHasKey(FailureEnvelope::META_FAILURE, $envelope->getMetadata());
$this->assertSame($metadata, $envelope->getMetadata()[FailureEnvelope::META_FAILURE]);
}
public function testGetFailureMetadata(): void
{
$message = $this->createMessage();
$metadata = ['attempt' => 1, 'error' => 'Test error'];
$envelope = new FailureEnvelope($message, $metadata);
$this->assertSame($metadata, $envelope->getFailureMetadata());
}
public function testFromMessageWithExistingMetadata(): void
{
$existingMetadata = ['attempt' => 1];
$message = $this->createMessage([FailureEnvelope::META_FAILURE => $existingMetadata]);
$envelope = FailureEnvelope::fromMessage($message);
$this->assertSame($existingMetadata, $envelope->getMetadata()[FailureEnvelope::META_FAILURE]);
}
public function testFromMessageWithoutMetadata(): void
{
$message = $this->createMessage();
$envelope = FailureEnvelope::fromMessage($message);
$this->assertArrayHasKey(FailureEnvelope::META_FAILURE, $envelope->getMetadata());
$this->assertSame([], $envelope->getMetadata()[FailureEnvelope::META_FAILURE]);
}
public function testMetadataMerging(): void
{
$existingMetadata = ['attempt' => 1, 'firstError' => 'First error'];
$message = $this->createMessage([FailureEnvelope::META_FAILURE => $existingMetadata]);
$newMetadata = ['attempt' => 2, 'lastError' => 'Last error'];
$envelope = new FailureEnvelope($message, $newMetadata);
$mergedMetadata = $envelope->getMetadata()[FailureEnvelope::META_FAILURE];
$this->assertSame(2, $mergedMetadata['attempt']);
$this->assertSame('First error', $mergedMetadata['firstError']);
$this->assertSame('Last error', $mergedMetadata['lastError']);
}
private function createMessage(array $metadata = []): MessageInterface
{
return (new GenericMessage('test-handler', ['test-data']))->withMetadata($metadata);
}
}