Completed
Push — master ( 8f4390...add441 )
by Tobias
01:21
created

SerializerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 112
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testDecode() 0 23 1
A testDecodeWithRetryCount() 0 28 1
A testEncode() 0 24 1
A testEncodeWithRedeliveryStamp() 0 31 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\Happyr\MessageSerializer;
6
7
use Happyr\MessageSerializer\Hydrator\ArrayToMessageInterface;
8
use Happyr\MessageSerializer\Serializer;
9
use Happyr\MessageSerializer\Transformer\MessageToArrayInterface;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\Messenger\Envelope;
12
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
13
14
/**
15
 * @internal
16
 */
17
final class SerializerTest extends TestCase
18
{
19
    public function testDecode()
20
    {
21
        $transformer = $this->getMockBuilder(MessageToArrayInterface::class)->getMock();
22
        $hydrator = $this->getMockBuilder(ArrayToMessageInterface::class)
23
            ->setMethods(['toMessage'])
24
            ->getMock();
25
26
        $payload = ['a' => 'b'];
27
        $data = [
28
            'body' => \json_encode($payload),
29
        ];
30
31
        $hydrator->expects(self::once())
32
            ->method('toMessage')
33
            ->with($payload)
34
            ->willReturn(new \stdClass());
35
36
        $serializer = new Serializer($transformer, $hydrator);
37
        $output = $serializer->decode($data);
38
39
        self::assertInstanceOf(Envelope::class, $output);
40
        self::assertInstanceOf(\stdClass::class, $output->getMessage());
41
    }
42
43
    public function testDecodeWithRetryCount(): void
44
    {
45
        $transformer = $this->getMockBuilder(MessageToArrayInterface::class)->getMock();
46
        $hydrator = $this->getMockBuilder(ArrayToMessageInterface::class)
47
            ->getMock();
48
49
        $payload = [
50
            'a' => 'b',
51
        ];
52
        $data = [
53
            'body' => \json_encode(
54
                array_merge($payload, ['_meta' => ['retry-count' => 2]]),
55
                JSON_THROW_ON_ERROR, 512),
56
        ];
57
58
        $hydrator->expects(self::once())
59
            ->method('toMessage')
60
            ->with($payload)
61
            ->willReturn(new \stdClass());
62
63
        $serializer = new Serializer($transformer, $hydrator);
64
        $output = $serializer->decode($data);
65
66
        self::assertInstanceOf(\stdClass::class, $output->getMessage());
67
        /** @var RedeliveryStamp $redeliveryStamp */
68
        $redeliveryStamp = $output->last(RedeliveryStamp::class);
69
        self::assertEquals(2, $redeliveryStamp->getRetryCount());
70
    }
71
72
    public function testEncode()
73
    {
74
        $transformer = $this->getMockBuilder(MessageToArrayInterface::class)
75
            ->setMethods(['toArray'])
76
            ->getMock();
77
        $hydrator = $this->getMockBuilder(ArrayToMessageInterface::class)->getMock();
78
79
        $envelope = new Envelope(new \stdClass('foo'));
0 ignored issues
show
Unused Code introduced by
The call to stdClass::__construct() has too many arguments starting with 'foo'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
80
81
        $transformer->expects(self::once())
82
            ->method('toArray')
83
            ->with($envelope)
84
            ->willReturn(['foo' => 'bar']);
85
86
        $serializer = new Serializer($transformer, $hydrator);
87
        $output = $serializer->encode($envelope);
88
89
        self::assertArrayHasKey('headers', $output);
90
        self::assertArrayHasKey('Content-Type', $output['headers']);
91
        self::assertEquals('application/json', $output['headers']['Content-Type']);
92
93
        self::assertArrayHasKey('body', $output);
94
        self::assertEquals(\json_encode(['foo' => 'bar', '_meta' => []]), $output['body']);
95
    }
96
97
    public function testEncodeWithRedeliveryStamp()
98
    {
99
        $transformer = $this->getMockBuilder(MessageToArrayInterface::class)
100
            ->getMock();
101
        $hydrator = $this->getMockBuilder(ArrayToMessageInterface::class)->getMock();
102
103
        $envelope = new Envelope(new \stdClass('foo'), [new RedeliveryStamp(2)]);
0 ignored issues
show
Unused Code introduced by
The call to stdClass::__construct() has too many arguments starting with 'foo'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
104
105
        $transformer->expects(self::once())
106
            ->method('toArray')
107
            ->with($envelope)
108
            ->willReturn(['foo' => 'bar']);
109
110
        $serializer = new Serializer($transformer, $hydrator);
111
        $output = $serializer->encode($envelope);
112
113
        self::assertArrayHasKey('headers', $output);
114
        self::assertArrayHasKey('Content-Type', $output['headers']);
115
        self::assertEquals('application/json', $output['headers']['Content-Type']);
116
117
        self::assertArrayHasKey('body', $output);
118
        self::assertEquals(\json_encode(
119
            [
120
                'foo' => 'bar',
121
                '_meta' => [
122
                    'retry-count' => 2,
123
                ],
124
            ], JSON_THROW_ON_ERROR, 512),
125
            $output['body']
126
        );
127
    }
128
}
129