Completed
Push — master ( eefbf1...ab6c28 )
by Tobias
03:54 queued 01:15
created

SerializerTest::testEncode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
13
/**
14
 * @internal
15
 */
16
final class SerializerTest extends TestCase
17
{
18
    public function testDecode()
19
    {
20
        $transformer = $this->getMockBuilder(MessageToArrayInterface::class)->getMock();
21
        $hydrator = $this->getMockBuilder(ArrayToMessageInterface::class)
22
            ->setMethods(['toMessage'])
23
            ->getMock();
24
25
        $payload = ['a' => 'b'];
26
        $data = [
27
            'body' => \json_encode($payload),
28
        ];
29
30
        $hydrator->expects(self::once())
31
            ->method('toMessage')
32
            ->with($payload)
33
            ->willReturn(new \stdClass());
34
35
        $serializer = new Serializer($transformer, $hydrator);
36
        $output = $serializer->decode($data);
37
38
        self::assertInstanceOf(Envelope::class, $output);
39
        self::assertInstanceOf(\stdClass::class, $output->getMessage());
40
    }
41
42
    public function testEncode()
43
    {
44
        $transformer = $this->getMockBuilder(MessageToArrayInterface::class)
45
            ->setMethods(['toArray'])
46
            ->getMock();
47
        $hydrator = $this->getMockBuilder(ArrayToMessageInterface::class)->getMock();
48
49
        $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...
50
51
        $transformer->expects(self::once())
52
            ->method('toArray')
53
            ->with($envelope)
54
            ->willReturn(['foo' => 'bar']);
55
56
        $serializer = new Serializer($transformer, $hydrator);
57
        $output = $serializer->encode($envelope);
58
59
        self::assertArrayHasKey('headers', $output);
60
        self::assertArrayHasKey('Content-Type', $output['headers']);
61
        self::assertEquals('application/json', $output['headers']['Content-Type']);
62
63
        self::assertArrayHasKey('body', $output);
64
        self::assertEquals(\json_encode(['foo' => 'bar']), $output['body']);
65
    }
66
}
67