Passed
Pull Request — master (#97)
by Maximilian
04:02
created

VideoComponentTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 171
dl 0
loc 245
rs 10
c 1
b 0
f 1
wmc 15

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testJsonSerializeWithArraySource() 0 7 1
A testJsonSerializeWithBooleanProperties() 0 12 1
A testConstructorWithAllParameters() 0 51 1
A testJsonSerializeWithDefaultValues() 0 10 1
A testJsonSerializeWithEmptyArrays() 0 26 1
A testConstructorWithDefaultParameters() 0 20 1
A testTypeConstant() 0 3 1
A testJsonSerializeWithStringSource() 0 6 1
A testJsonSerializeWithDifferentScaleValues() 0 9 2
A testImplementsJsonSerializable() 0 5 1
A testJsonSerializeWithDifferentAudioTracks() 0 9 2
A testJsonSerializeWithAllProperties() 0 54 1
A testExtendsAPLBaseComponent() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Test\Response\Directives\APL\Component;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\VideoComponent;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\APLComponentType;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\AudioTrack;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Scale;
11
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
12
use PHPUnit\Framework\TestCase;
13
14
class VideoComponentTest extends TestCase
15
{
16
    public function testConstructorWithAllParameters(): void
17
    {
18
        $audioTrack = AudioTrack::FOREGROUND;
19
        $onEnd = [$this->createMock(AbstractStandardCommand::class)];
20
        $onPause = [$this->createMock(AbstractStandardCommand::class)];
21
        $onPlay = [$this->createMock(AbstractStandardCommand::class)];
22
        $onTimeUpdate = [$this->createMock(AbstractStandardCommand::class)];
23
        $onTrackUpdate = [$this->createMock(AbstractStandardCommand::class)];
24
        $onTrackReady = [$this->createMock(AbstractStandardCommand::class)];
25
        $onTrackFail = [$this->createMock(AbstractStandardCommand::class)];
26
        $preserve = ['playbackPosition', 'volume'];
27
        $scale = Scale::FILL;
28
        $source = 'https://example.com/video.mp4';
29
        $sources = ['https://example.com/video1.mp4', 'https://example.com/video2.mp4'];
30
        $trackChanges = ['position', 'state'];
31
32
        $component = new VideoComponent(
33
            $audioTrack,
34
            true, // autoplay
35
            true, // muted
36
            $onEnd,
37
            $onPause,
38
            $onPlay,
39
            $onTimeUpdate,
40
            $onTrackUpdate,
41
            $onTrackReady,
42
            $onTrackFail,
43
            $preserve,
44
            $scale,
45
            false, // screenLock
46
            $source,
47
            $sources,
48
            $trackChanges
49
        );
50
51
        $this->assertSame($audioTrack, $component->audioTrack);
52
        $this->assertTrue($component->autoplay);
53
        $this->assertTrue($component->muted);
54
        $this->assertSame($onEnd, $component->onEnd);
55
        $this->assertSame($onPause, $component->onPause);
56
        $this->assertSame($onPlay, $component->onPlay);
57
        $this->assertSame($onTimeUpdate, $component->onTimeUpdate);
58
        $this->assertSame($onTrackUpdate, $component->onTrackUpdate);
59
        $this->assertSame($onTrackReady, $component->onTrackReady);
60
        $this->assertSame($onTrackFail, $component->onTrackFail);
61
        $this->assertSame($preserve, $component->preserve);
62
        $this->assertSame($scale, $component->scale);
63
        $this->assertFalse($component->screenLock);
64
        $this->assertSame($source, $component->source);
65
        $this->assertSame($sources, $component->sources);
66
        $this->assertSame($trackChanges, $component->trackChanges);
67
    }
68
69
    public function testConstructorWithDefaultParameters(): void
70
    {
71
        $component = new VideoComponent();
72
73
        $this->assertNull($component->audioTrack);
74
        $this->assertFalse($component->autoplay);
75
        $this->assertFalse($component->muted);
76
        $this->assertNull($component->onEnd);
77
        $this->assertNull($component->onPause);
78
        $this->assertNull($component->onPlay);
79
        $this->assertNull($component->onTimeUpdate);
80
        $this->assertNull($component->onTrackUpdate);
81
        $this->assertNull($component->onTrackReady);
82
        $this->assertNull($component->onTrackFail);
83
        $this->assertNull($component->preserve);
84
        $this->assertNull($component->scale);
85
        $this->assertTrue($component->screenLock);
86
        $this->assertNull($component->source);
87
        $this->assertNull($component->sources);
88
        $this->assertNull($component->trackChanges);
89
    }
90
91
    public function testJsonSerializeWithAllProperties(): void
92
    {
93
        $audioTrack = AudioTrack::BACKGROUND;
94
        $onEnd = [$this->createMock(AbstractStandardCommand::class)];
95
        $onPause = [$this->createMock(AbstractStandardCommand::class)];
96
        $onPlay = [$this->createMock(AbstractStandardCommand::class)];
97
        $onTimeUpdate = [$this->createMock(AbstractStandardCommand::class)];
98
        $onTrackUpdate = [$this->createMock(AbstractStandardCommand::class)];
99
        $onTrackReady = [$this->createMock(AbstractStandardCommand::class)];
100
        $onTrackFail = [$this->createMock(AbstractStandardCommand::class)];
101
        $preserve = ['state'];
102
        $scale = Scale::BEST_FIT;
103
        $source = ['url1.mp4', 'url2.mp4'];
104
        $sources = [['url' => 'video1.mp4'], ['url' => 'video2.mp4']];
105
        $trackChanges = ['playback'];
106
107
        $component = new VideoComponent(
108
            audioTrack: $audioTrack,
109
            autoplay: true,
110
            muted: true,
111
            onEnd: $onEnd,
112
            onPause: $onPause,
113
            onPlay: $onPlay,
114
            onTimeUpdate: $onTimeUpdate,
115
            onTrackUpdate: $onTrackUpdate,
116
            onTrackReady: $onTrackReady,
117
            onTrackFail: $onTrackFail,
118
            preserve: $preserve,
119
            scale: $scale,
120
            screenLock: false,
121
            source: $source,
122
            sources: $sources,
123
            trackChanges: $trackChanges
124
        );
125
126
        $result = $component->jsonSerialize();
127
128
        $this->assertSame(APLComponentType::VIDEO->value, $result['type']);
129
        $this->assertSame($audioTrack->value, $result['audioTrack']);
130
        $this->assertTrue($result['autoplay']);
131
        $this->assertTrue($result['muted']);
132
        $this->assertSame($onEnd, $result['onEnd']);
133
        $this->assertSame($onPause, $result['onPause']);
134
        $this->assertSame($onPlay, $result['onPlay']);
135
        $this->assertSame($onTimeUpdate, $result['onTimeUpdate']);
136
        $this->assertSame($onTrackUpdate, $result['onTrackUpdate']);
137
        $this->assertSame($onTrackReady, $result['onTrackReady']);
138
        $this->assertSame($onTrackFail, $result['onTrackFail']);
139
        $this->assertSame($preserve, $result['preserve']);
140
        $this->assertSame($scale->value, $result['scale']);
141
        $this->assertFalse($result['screenLock']);
142
        $this->assertSame($source, $result['source']);
143
        $this->assertSame($sources, $result['sources']);
144
        $this->assertSame($trackChanges, $result['trackChanges']);
145
    }
146
147
    public function testJsonSerializeWithDefaultValues(): void
148
    {
149
        $component = new VideoComponent();
150
        $result = $component->jsonSerialize();
151
152
        $this->assertSame(APLComponentType::VIDEO->value, $result['type']);
153
        $this->assertArrayNotHasKey('audioTrack', $result);
154
        $this->assertArrayNotHasKey('autoplay', $result);
155
        $this->assertArrayNotHasKey('muted', $result);
156
        $this->assertArrayNotHasKey('screenLock', $result); // true is default, so not included
157
    }
158
159
    public function testJsonSerializeWithBooleanProperties(): void
160
    {
161
        $component = new VideoComponent(
162
            autoplay: true,
163
            muted: true,
164
            screenLock: false
165
        );
166
        $result = $component->jsonSerialize();
167
168
        $this->assertTrue($result['autoplay']);
169
        $this->assertTrue($result['muted']);
170
        $this->assertFalse($result['screenLock']);
171
    }
172
173
    public function testJsonSerializeWithEmptyArrays(): void
174
    {
175
        $component = new VideoComponent(
176
            onEnd: [],
177
            onPause: [],
178
            onPlay: [],
179
            onTimeUpdate: [],
180
            onTrackUpdate: [],
181
            onTrackReady: [],
182
            onTrackFail: [],
183
            preserve: [],
184
            sources: [],
185
            trackChanges: []
186
        );
187
        $result = $component->jsonSerialize();
188
189
        $this->assertArrayNotHasKey('onEnd', $result);
190
        $this->assertArrayNotHasKey('onPause', $result);
191
        $this->assertArrayNotHasKey('onPlay', $result);
192
        $this->assertArrayNotHasKey('onTimeUpdate', $result);
193
        $this->assertArrayNotHasKey('onTrackUpdate', $result);
194
        $this->assertArrayNotHasKey('onTrackReady', $result);
195
        $this->assertArrayNotHasKey('onTrackFail', $result);
196
        $this->assertArrayNotHasKey('preserve', $result);
197
        $this->assertArrayNotHasKey('sources', $result);
198
        $this->assertArrayNotHasKey('trackChanges', $result);
199
    }
200
201
    public function testJsonSerializeWithStringSource(): void
202
    {
203
        $component = new VideoComponent(source: 'video.mp4');
204
        $result = $component->jsonSerialize();
205
206
        $this->assertSame('video.mp4', $result['source']);
207
    }
208
209
    public function testJsonSerializeWithArraySource(): void
210
    {
211
        $source = ['primary.mp4', 'fallback.mp4'];
212
        $component = new VideoComponent(source: $source);
213
        $result = $component->jsonSerialize();
214
215
        $this->assertSame($source, $result['source']);
216
    }
217
218
    public function testJsonSerializeWithDifferentAudioTracks(): void
219
    {
220
        $audioTracks = [AudioTrack::FOREGROUND, AudioTrack::BACKGROUND];
221
222
        foreach ($audioTracks as $track) {
223
            $component = new VideoComponent(audioTrack: $track);
224
            $result = $component->jsonSerialize();
225
226
            $this->assertSame($track->value, $result['audioTrack']);
227
        }
228
    }
229
230
    public function testJsonSerializeWithDifferentScaleValues(): void
231
    {
232
        $scaleValues = [Scale::FILL, Scale::BEST_FIT, Scale::NONE];
233
234
        foreach ($scaleValues as $scale) {
235
            $component = new VideoComponent(scale: $scale);
236
            $result = $component->jsonSerialize();
237
238
            $this->assertSame($scale->value, $result['scale']);
239
        }
240
    }
241
242
    public function testTypeConstant(): void
243
    {
244
        $this->assertSame(APLComponentType::VIDEO, VideoComponent::TYPE);
245
    }
246
247
    public function testExtendsAPLBaseComponent(): void
248
    {
249
        $component = new VideoComponent();
250
251
        $this->assertInstanceOf(\MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\APLBaseComponent::class, $component);
252
    }
253
254
    public function testImplementsJsonSerializable(): void
255
    {
256
        $component = new VideoComponent();
257
258
        $this->assertInstanceOf(\JsonSerializable::class, $component);
259
    }
260
}
261