testJsonSerializeKeepsNonEmptyArrays()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 15
rs 9.9
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Test\Response\Directives\APL\Document;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\AVGItem\AVGItem;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Graphic;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\LayoutDirection;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Parameter;
11
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\ScaleType;
12
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Style;
13
use PHPUnit\Framework\TestCase;
14
15
class GraphicTest extends TestCase
16
{
17
    public function testConstructorWithAllParameters(): void
18
    {
19
        $height = 200;
20
        $width = 300;
21
        $data = ['key' => 'value'];
22
        $description = 'Test graphic';
23
        $item = $this->createMock(AVGItem::class);
24
        $items = [$this->createMock(AVGItem::class)];
25
        $lang = 'en-US';
26
        $layoutDirection = LayoutDirection::RTL;
27
        $parameters = [$this->createMock(Parameter::class)];
28
        $resources = ['resource1'];
29
        $scaleTypeHeight = ScaleType::STRETCH;
30
        $scaleTypeWidth = ScaleType::GROW;
31
        $styles = [$this->createMock(Style::class)];
32
        $type = 'AVG';
33
        $version = '1.3';
34
        $viewportHeight = 400;
35
        $viewportWidth = 500;
36
37
        $graphic = new Graphic(
38
            $height,
39
            $width,
40
            $data,
41
            $description,
42
            $item,
43
            $items,
44
            $lang,
45
            $layoutDirection,
46
            $parameters,
47
            $resources,
48
            $scaleTypeHeight,
49
            $scaleTypeWidth,
50
            $styles,
51
            $type,
52
            $version,
53
            $viewportHeight,
54
            $viewportWidth
55
        );
56
57
        $this->assertSame($height, $graphic->height);
58
        $this->assertSame($width, $graphic->width);
59
        $this->assertSame($data, $graphic->data);
60
        $this->assertSame($description, $graphic->description);
61
        $this->assertSame($item, $graphic->item);
62
        $this->assertSame($items, $graphic->items);
63
        $this->assertSame($lang, $graphic->lang);
64
        $this->assertSame($layoutDirection, $graphic->layoutDirection);
65
        $this->assertSame($parameters, $graphic->parameters);
66
        $this->assertSame($resources, $graphic->resources);
67
        $this->assertSame($scaleTypeHeight, $graphic->scaleTypeHeight);
68
        $this->assertSame($scaleTypeWidth, $graphic->scaleTypeWidth);
69
        $this->assertSame($styles, $graphic->styles);
70
        $this->assertSame($type, $graphic->type);
71
        $this->assertSame($version, $graphic->version);
72
        $this->assertSame($viewportHeight, $graphic->viewportHeight);
73
        $this->assertSame($viewportWidth, $graphic->viewportWidth);
74
    }
75
76
    public function testConstructorWithDefaultParameters(): void
77
    {
78
        $height = 100;
79
        $width = 150;
80
81
        $graphic = new Graphic($height, $width);
82
83
        $this->assertSame($height, $graphic->height);
84
        $this->assertSame($width, $graphic->width);
85
        $this->assertNull($graphic->data);
86
        $this->assertSame('', $graphic->description);
87
        $this->assertNull($graphic->item);
88
        $this->assertNull($graphic->items);
89
        $this->assertNull($graphic->lang);
90
        $this->assertSame(LayoutDirection::LTR, $graphic->layoutDirection);
91
        $this->assertSame([], $graphic->parameters);
92
        $this->assertSame([], $graphic->resources);
93
        $this->assertSame(ScaleType::NONE, $graphic->scaleTypeHeight);
94
        $this->assertSame(ScaleType::NONE, $graphic->scaleTypeWidth);
95
        $this->assertSame([], $graphic->styles);
96
        $this->assertSame('AVG', $graphic->type);
97
        $this->assertSame('1.2', $graphic->version);
98
        $this->assertNull($graphic->viewportHeight);
99
        $this->assertNull($graphic->viewportWidth);
100
    }
101
102
    public function testJsonSerializeWithAllProperties(): void
103
    {
104
        $data = ['test' => 'data'];
105
        $item = $this->createMock(AVGItem::class);
106
        $items = [$this->createMock(AVGItem::class)];
107
        $parameters = [$this->createMock(Parameter::class)];
108
        $resources = ['resource'];
109
        $styles = [$this->createMock(Style::class)];
110
111
        $graphic = new Graphic(
112
            height: 200,
113
            width: 300,
114
            data: $data,
115
            description: 'Test',
116
            item: $item,
117
            items: $items,
118
            lang: 'fr-FR',
119
            layoutDirection: LayoutDirection::RTL,
120
            parameters: $parameters,
121
            resources: $resources,
122
            scaleTypeHeight: ScaleType::GROW,
123
            scaleTypeWidth: ScaleType::STRETCH,
124
            styles: $styles,
125
            type: 'AVG',
126
            version: '1.4',
127
            viewportHeight: 400,
128
            viewportWidth: 500
129
        );
130
131
        $result = $graphic->jsonSerialize();
132
133
        $this->assertSame($data, $result['data']);
134
        $this->assertSame('Test', $result['description']);
135
        $this->assertSame(200, $result['height']);
136
        $this->assertSame($item, $result['item']);
137
        $this->assertSame($items, $result['items']);
138
        $this->assertSame('fr-FR', $result['lang']);
139
        $this->assertSame(LayoutDirection::RTL, $result['layoutDirection']);
140
        $this->assertSame($parameters, $result['parameters']);
141
        $this->assertSame($resources, $result['resources']);
142
        $this->assertSame(ScaleType::GROW, $result['scaleTypeHeight']);
143
        $this->assertSame(ScaleType::STRETCH, $result['scaleTypeWidth']);
144
        $this->assertIsArray($result['styles']);
145
        $this->assertSame('AVG', $result['type']);
146
        $this->assertSame('1.4', $result['version']);
147
        $this->assertSame(400, $result['viewportHeight']);
148
        $this->assertSame(500, $result['viewportWidth']);
149
        $this->assertSame(300, $result['width']);
150
    }
151
152
    public function testJsonSerializeFiltersNullValues(): void
153
    {
154
        $graphic = new Graphic(100, 200);
155
        $result = $graphic->jsonSerialize();
156
157
        $this->assertArrayNotHasKey('data', $result);
158
        $this->assertArrayNotHasKey('item', $result);
159
        $this->assertArrayNotHasKey('items', $result);
160
        $this->assertArrayNotHasKey('lang', $result);
161
        $this->assertArrayNotHasKey('viewportHeight', $result);
162
        $this->assertArrayNotHasKey('viewportWidth', $result);
163
    }
164
165
    public function testJsonSerializeFiltersEmptyArrays(): void
166
    {
167
        $graphic = new Graphic(100, 200, [], '', null, [], null, LayoutDirection::LTR, [], [], ScaleType::NONE, ScaleType::NONE, []);
168
        $result = $graphic->jsonSerialize();
169
170
        $this->assertArrayNotHasKey('data', $result);
171
        $this->assertArrayNotHasKey('items', $result);
172
        $this->assertArrayNotHasKey('parameters', $result);
173
        $this->assertArrayNotHasKey('resources', $result);
174
        $this->assertArrayNotHasKey('styles', $result);
175
    }
176
177
    public function testJsonSerializeKeepsNonEmptyArrays(): void
178
    {
179
        $parameters = [$this->createMock(Parameter::class)];
180
        $resources = ['test'];
181
        $styles = [$this->createMock(Style::class)];
182
183
        $graphic = new Graphic(100, 200, null, '', null, null, null, LayoutDirection::LTR, $parameters, $resources, ScaleType::NONE, ScaleType::NONE, $styles);
184
        $result = $graphic->jsonSerialize();
185
186
        $this->assertArrayHasKey('parameters', $result);
187
        $this->assertArrayHasKey('resources', $result);
188
        $this->assertArrayHasKey('styles', $result);
189
        $this->assertSame($parameters, $result['parameters']);
190
        $this->assertSame($resources, $result['resources']);
191
        $this->assertIsArray($result['styles']);
192
    }
193
194
    public function testJsonSerializeStylesAsArray(): void
195
    {
196
        $styles = [$this->createMock(Style::class)];
197
        $graphic = new Graphic(100, 200, styles: $styles);
198
        $result = $graphic->jsonSerialize();
199
200
        $this->assertIsArray($result['styles']);
201
    }
202
203
    public function testJsonSerializeWithDifferentScaleTypes(): void
204
    {
205
        $scaleTypes = [ScaleType::NONE, ScaleType::GROW, ScaleType::STRETCH];
206
207
        foreach ($scaleTypes as $scaleType) {
208
            $graphic = new Graphic(100, 200, scaleTypeHeight: $scaleType, scaleTypeWidth: $scaleType);
209
            $result = $graphic->jsonSerialize();
210
211
            $this->assertSame($scaleType, $result['scaleTypeHeight']);
212
            $this->assertSame($scaleType, $result['scaleTypeWidth']);
213
        }
214
    }
215
216
    public function testJsonSerializeWithDifferentLayoutDirections(): void
217
    {
218
        $directions = [LayoutDirection::LTR, LayoutDirection::RTL];
219
220
        foreach ($directions as $direction) {
221
            $graphic = new Graphic(100, 200, layoutDirection: $direction);
222
            $result = $graphic->jsonSerialize();
223
224
            $this->assertSame($direction, $result['layoutDirection']);
225
        }
226
    }
227
228
    public function testImplementsJsonSerializable(): void
229
    {
230
        $graphic = new Graphic(100, 200);
231
232
        $this->assertInstanceOf(\JsonSerializable::class, $graphic);
233
    }
234
}
235