-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSerializerTest.php
128 lines (103 loc) · 4.19 KB
/
SerializerTest.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
declare(strict_types=1);
namespace Tests\Happyr\MessageSerializer;
use Happyr\MessageSerializer\Hydrator\ArrayToMessageInterface;
use Happyr\MessageSerializer\Serializer;
use Happyr\MessageSerializer\Transformer\MessageToArrayInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
/**
* @internal
*/
final class SerializerTest extends TestCase
{
public function testDecode()
{
$transformer = $this->getMockBuilder(MessageToArrayInterface::class)->getMock();
$hydrator = $this->getMockBuilder(ArrayToMessageInterface::class)
->setMethods(['toMessage'])
->getMock();
$payload = ['a' => 'b'];
$data = [
'body' => \json_encode($payload),
];
$hydrator->expects(self::once())
->method('toMessage')
->with($payload)
->willReturn(new \stdClass());
$serializer = new Serializer($transformer, $hydrator);
$output = $serializer->decode($data);
self::assertInstanceOf(Envelope::class, $output);
self::assertInstanceOf(\stdClass::class, $output->getMessage());
}
public function testDecodeWithRetryCount(): void
{
$transformer = $this->getMockBuilder(MessageToArrayInterface::class)->getMock();
$hydrator = $this->getMockBuilder(ArrayToMessageInterface::class)
->getMock();
$payload = [
'a' => 'b',
];
$data = [
'body' => \json_encode(
array_merge($payload, ['_meta' => ['retry-count' => 2]]),
JSON_THROW_ON_ERROR, 512),
];
$hydrator->expects(self::once())
->method('toMessage')
->with($payload)
->willReturn(new \stdClass());
$serializer = new Serializer($transformer, $hydrator);
$output = $serializer->decode($data);
self::assertInstanceOf(\stdClass::class, $output->getMessage());
/** @var RedeliveryStamp $redeliveryStamp */
$redeliveryStamp = $output->last(RedeliveryStamp::class);
self::assertEquals(2, $redeliveryStamp->getRetryCount());
}
public function testEncode()
{
$transformer = $this->getMockBuilder(MessageToArrayInterface::class)
->setMethods(['toArray'])
->getMock();
$hydrator = $this->getMockBuilder(ArrayToMessageInterface::class)->getMock();
$envelope = new Envelope(new \stdClass('foo'));
$transformer->expects(self::once())
->method('toArray')
->with($envelope)
->willReturn(['foo' => 'bar']);
$serializer = new Serializer($transformer, $hydrator);
$output = $serializer->encode($envelope);
self::assertArrayHasKey('headers', $output);
self::assertArrayHasKey('Content-Type', $output['headers']);
self::assertEquals('application/json', $output['headers']['Content-Type']);
self::assertArrayHasKey('body', $output);
self::assertEquals(\json_encode(['foo' => 'bar', '_meta' => []]), $output['body']);
}
public function testEncodeWithRedeliveryStamp()
{
$transformer = $this->getMockBuilder(MessageToArrayInterface::class)
->getMock();
$hydrator = $this->getMockBuilder(ArrayToMessageInterface::class)->getMock();
$envelope = new Envelope(new \stdClass('foo'), [new RedeliveryStamp(2)]);
$transformer->expects(self::once())
->method('toArray')
->with($envelope)
->willReturn(['foo' => 'bar']);
$serializer = new Serializer($transformer, $hydrator);
$output = $serializer->encode($envelope);
self::assertArrayHasKey('headers', $output);
self::assertArrayHasKey('Content-Type', $output['headers']);
self::assertEquals('application/json', $output['headers']['Content-Type']);
self::assertArrayHasKey('body', $output);
self::assertEquals(\json_encode(
[
'foo' => 'bar',
'_meta' => [
'retry-count' => 2,
],
], JSON_THROW_ON_ERROR, 512),
$output['body']
);
}
}