ComradeDeserializerTest::testDecodeIntoArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace ComradeReader\Test\Service;
4
5
use ComradeReader\Model\Entity\PaginatedResults;
6
use ComradeReader\Service\ComradeDeserializer;
7
use ComradeReader\Test\Helpers\SimpleTestEntity;
8
use GuzzleHttp\Psr7\Response;
9
use Symfony\Component\Serializer\Encoder\JsonDecode;
10
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
11
use Symfony\Component\Serializer\Serializer;
12
13
/**
14
 * @see ComradeDeserializer
15
 * @package ComradeReader\Test\Service
16
 */
17
class ComradeDeserializerTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @param array $response
21
     * @return ComradeDeserializer
22
     */
23
    private function constructComrade($response)
24
    {
25
        return new ComradeDeserializer(new Response(200, [], json_encode($response)), new Serializer([
26
            new PropertyNormalizer()
27
        ], [
28
            new JsonDecode()
29
        ]));
30
    }
31
32
    /**
33
     * @throws \ComradeReader\Exception\Deserializer\DeserializerException
34
     * @expectedException \ComradeReader\Exception\Deserializer\DeserializerException
35
     */
36
    public function testInvalidResponse()
37
    {
38
        $comrade = $this->constructComrade(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
        $comrade->getDecodedResponse();
40
    }
41
42
    /**
43
     * @see ComradeDeserializer::decodeIntoObject()
44
     */
45
    public function testDecodeIntoObject()
46
    {
47
        $comrade = $this->constructComrade([
48
            'success' => true,
49
            'data'    => [
50
                'id'        => 1,
51
                'colorName' => 'red',
52
            ]
53
        ]);
54
55
        /** @var SimpleTestEntity $decoded */
56
        $decoded = $comrade->decodeIntoObject(SimpleTestEntity::class);
57
58
        $this->assertSame(1, $decoded->getId());
59
        $this->assertSame('red', $decoded->getColorName());
60
    }
61
62
    /**
63
     * @see ComradeDeserializer::decodeMultiple()
64
     */
65
    public function testDecodeIntoMultipleObjects()
66
    {
67
        $comrade = $this->constructComrade([
68
            'success' => true,
69
            'data'    => [
70
                [
71
                    'id'        => 1,
72
                    'colorName' => 'red',
73
                ],
74
                [
75
                    'id'        => 2,
76
                    'colorName' => 'black',
77
                ]
78
            ]
79
        ]);
80
81
        /** @var SimpleTestEntity[] $decoded */
82
        $decoded = $comrade->decodeIntoMultipleObjects(SimpleTestEntity::class);
83
84
        $this->assertCount(2, $decoded);
85
    }
86
87
    /**
88
     * @see ComradeDeserializer::getData()
89
     */
90
    public function testDecodeIntoArray()
91
    {
92
        $comrade = $this->constructComrade([
93
            'success' => true,
94
            'data'    => [
95
                'id'        => 1,
96
                'colorName' => 'Black & Red',
97
            ]
98
        ]);
99
100
        /** @var array $decoded */
101
        $decoded = $comrade->getData();
102
103
        $this->assertInternalType('array', $decoded);
104
        $this->assertSame('Black & Red', $decoded['colorName']);
105
    }
106
107
    /**
108
     * @see ComradeDeserializer::getPlainResponse()
109
     */
110
    public function testGetPlainResponse()
111
    {
112
        $request = [
113
            'success' => true,
114
            'data'    => [
115
                'id'        => 1,
116
                'colorName' => 'Black & Red',
117
            ]
118
        ];
119
        $comrade = $this->constructComrade($request);
120
121
        $this->assertSame(json_encode($request), $comrade->getPlainResponse());
122
    }
123
124
    /**
125
     * @see ComradeDeserializer::decodeMultiple()
126
     */
127
    public function testPaginatedResults()
128
    {
129
        $comrade = $this->constructComrade([
130
            'success' => true,
131
            'data'    => [
132
                'results' => [
133
                    [
134
                        'id'        => 1,
135
                        'colorName' => 'Black & Red',
136
                    ],
137
                ],
138
139
                'current_page' => 1,
140
                'max_pages'    => 1,
141
            ]
142
        ]);
143
144
        /** @var PaginatedResults $results */
145
        $results =  $comrade->decodeIntoMultipleObjects(SimpleTestEntity::class);
146
147
        $this->assertInstanceOf(
148
            PaginatedResults::class,
149
            $results
150
        );
151
152
        $this->assertInstanceOf(SimpleTestEntity::class, current($results->getResults()));
153
    }
154
}