Test Failed
Pull Request — master (#96)
by Maximilian
17:24
created

AudioTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 14
eloc 80
c 4
b 0
f 0
dl 0
loc 146
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testStream() 0 13 1
A testPlaybackNearlyFinishedDirective() 0 5 1
A testAudioItem() 0 6 1
A testPlaybackFailedDirective() 0 9 1
A testPlaybackStartedDirective() 0 5 1
A getPlaybackFailed() 0 8 1
A testPlaybackStoppedDirective() 0 5 1
A testAudioItemWithMetadata() 0 14 1
A testClearDirective() 0 6 1
A testPlayDirectiveBehavior() 0 8 1
A testStopDirective() 0 5 1
A testPlayDirective() 0 9 1
A testStreamAll() 0 17 1
A testPlaybackFinishedDirective() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Test\Response\Directives;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\AudioItem;
8
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\ClearDirective;
9
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\CurrentPlaybackState;
10
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\Metadata;
11
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\PlaybackFailed;
12
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\PlaybackFinished;
13
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\PlaybackNearlyFinished;
14
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\PlaybackStarted;
15
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\PlaybackStopped;
16
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\PlayDirective;
17
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\StopDirective;
18
use MaxBeckers\AmazonAlexa\Response\Directives\AudioPlayer\Stream;
19
use MaxBeckers\AmazonAlexa\Response\Directives\Display\Image;
20
use MaxBeckers\AmazonAlexa\Response\Directives\Display\ImageSource;
21
use MaxBeckers\AmazonAlexa\Response\Directives\System\Error;
22
use PHPUnit\Framework\TestCase;
23
24
class AudioTest extends TestCase
25
{
26
    public function testStream(): void
27
    {
28
        $stream = Stream::create('testurl', 'token');
29
        $this->assertInstanceOf(Stream::class, $stream);
30
        $this->assertSame('testurl', $stream->url);
31
        $this->assertSame('token', $stream->token);
32
33
        $json = new \ArrayObject([
34
            'url' => 'testurl',
35
            'token' => 'token',
36
        ]);
37
38
        $this->assertEquals($json, $stream->jsonSerialize());
39
    }
40
41
    public function testStreamAll(): void
42
    {
43
        $stream = Stream::create('testurl', 'token', 'prevToken', 10);
44
        $this->assertInstanceOf(Stream::class, $stream);
45
        $this->assertSame('testurl', $stream->url);
46
        $this->assertSame('token', $stream->token);
47
        $this->assertSame('prevToken', $stream->expectedPreviousToken);
48
        $this->assertSame(10, $stream->offsetInMilliseconds);
49
50
        $json = new \ArrayObject([
51
            'url' => 'testurl',
52
            'token' => 'token',
53
            'expectedPreviousToken' => 'prevToken',
54
            'offsetInMilliseconds' => 10,
55
        ]);
56
57
        $this->assertEquals($json, $stream->jsonSerialize());
58
    }
59
60
    public function testAudioItem(): void
61
    {
62
        $stream = Stream::create('testurl', 'token');
63
        $audioItem = AudioItem::create($stream);
64
        $this->assertInstanceOf(AudioItem::class, $audioItem);
65
        $this->assertSame($stream, $audioItem->stream);
66
    }
67
68
    public function testAudioItemWithMetadata(): void
69
    {
70
        $art = Image::create(null, [ImageSource::create('https://url-of-the-album-art-image.png')]);
71
        $backgroundImage = Image::create(null, [ImageSource::create('https://url-of-the-background-image.png')]);
72
73
        $stream = Stream::create('https://url-of-the-stream-to-play', 'opaque token representing this stream', 'opaque token representing the previous stream', 0);
74
        $metadata = Metadata::create('title of the track to display', 'subtitle of the track to display', $art, $backgroundImage);
75
76
        $audioItem = AudioItem::create($stream, $metadata);
77
        $this->assertEquals([
78
            'stream' => $stream,
79
            'metadata' => $metadata,
80
        ], $audioItem->jsonSerialize());
81
        $this->assertSame('{"stream":{"url":"https:\/\/url-of-the-stream-to-play","token":"opaque token representing this stream","expectedPreviousToken":"opaque token representing the previous stream","offsetInMilliseconds":0},"metadata":{"title":"title of the track to display","subtitle":"subtitle of the track to display","art":{"contentDescription":null,"sources":[{"url":"https:\/\/url-of-the-album-art-image.png"}]},"backgroundImage":{"contentDescription":null,"sources":[{"url":"https:\/\/url-of-the-background-image.png"}]}}}', json_encode($audioItem));
82
    }
83
84
    public function testPlayDirective(): void
85
    {
86
        $stream = Stream::create('testurl', 'token');
87
        $audioItem = AudioItem::create($stream);
88
        $playDirective = PlayDirective::create($audioItem);
89
        $this->assertInstanceOf(PlayDirective::class, $playDirective);
90
        $this->assertSame(PlayDirective::TYPE, $playDirective->type);
91
        $this->assertSame($audioItem, $playDirective->audioItem);
92
        $this->assertSame(PlayDirective::PLAY_BEHAVIOR_REPLACE_ALL, $playDirective->playBehavior);
93
    }
94
95
    public function testPlayDirectiveBehavior(): void
96
    {
97
        $stream = Stream::create('testurl', 'token');
98
        $audioItem = AudioItem::create($stream);
99
        $playDirective = PlayDirective::create($audioItem, PlayDirective::PLAY_BEHAVIOR_REPLACE_ENQUEUED);
100
        $this->assertInstanceOf(PlayDirective::class, $playDirective);
101
        $this->assertSame($audioItem, $playDirective->audioItem);
102
        $this->assertSame(PlayDirective::PLAY_BEHAVIOR_REPLACE_ENQUEUED, $playDirective->playBehavior);
103
    }
104
105
    public function testClearDirective(): void
106
    {
107
        $clearDirective = ClearDirective::create(ClearDirective::CLEAR_BEHAVIOR_CLEAR_ALL);
108
        $this->assertInstanceOf(ClearDirective::class, $clearDirective);
109
        $this->assertSame(ClearDirective::TYPE, $clearDirective->type);
110
        $this->assertSame(ClearDirective::CLEAR_BEHAVIOR_CLEAR_ALL, $clearDirective->clearBehavior);
111
    }
112
113
    public function testStopDirective(): void
114
    {
115
        $stopDirective = StopDirective::create();
116
        $this->assertInstanceOf(StopDirective::class, $stopDirective);
117
        $this->assertSame(StopDirective::TYPE, $stopDirective->type);
118
    }
119
120
    public function testPlaybackStartedDirective(): void
121
    {
122
        $playbackStarted = PlaybackStarted::create('requestId', 'timestamp', 'token', 0, 'en-US');
123
        $this->assertInstanceOf(PlaybackStarted::class, $playbackStarted);
124
        $this->assertSame(PlaybackStarted::TYPE, $playbackStarted->type);
125
    }
126
127
    public function testPlaybackFinishedDirective(): void
128
    {
129
        $playbackFinished = PlaybackFinished::create('requestId', 'timestamp', 'token', 0, 'en-US');
130
        $this->assertInstanceOf(PlaybackFinished::class, $playbackFinished);
131
        $this->assertSame(PlaybackFinished::TYPE, $playbackFinished->type);
132
    }
133
134
    public function testPlaybackStoppedDirective(): void
135
    {
136
        $playbackStopped = PlaybackStopped::create('requestId', 'timestamp', 'token', 0, 'en-US');
137
        $this->assertInstanceOf(PlaybackStopped::class, $playbackStopped);
138
        $this->assertSame(PlaybackStopped::TYPE, $playbackStopped->type);
139
    }
140
141
    public function testPlaybackNearlyFinishedDirective(): void
142
    {
143
        $playbackNearlyFinished = PlaybackNearlyFinished::create('requestId', 'timestamp', 'token', 0, 'en-US');
144
        $this->assertInstanceOf(PlaybackNearlyFinished::class, $playbackNearlyFinished);
145
        $this->assertSame(PlaybackNearlyFinished::TYPE, $playbackNearlyFinished->type);
146
    }
147
148
    /**
149
     * @dataProvider getPlaybackFailed
150
     */
151
    public function testPlaybackFailedDirective(string $errorReason, string $playerActivity): void
152
    {
153
        $error = Error::create($errorReason, 'message');
154
        $currentPlaybackState = CurrentPlaybackState::create('token', 0, $playerActivity);
155
        $playbackFailed = PlaybackFailed::create('requestId', 'timestamp', 'token', 0, 'en-US', $error, $currentPlaybackState);
156
        $this->assertInstanceOf(PlaybackFailed::class, $playbackFailed);
157
        $this->assertSame(PlaybackFailed::TYPE, $playbackFailed->type);
158
        $this->assertSame($errorReason, $playbackFailed->error->type);
159
        $this->assertSame($playerActivity, $playbackFailed->currentPlaybackState->playerActivity);
160
    }
161
162
    public static function getPlaybackFailed(): array
163
    {
164
        return [
165
            [Error::MEDIA_ERROR_UNKNOWN, CurrentPlaybackState::PLAYER_ACTIVITY_PLAYING],
166
            [Error::MEDIA_ERROR_SERVICE_UNAVAILABLE, CurrentPlaybackState::PLAYER_ACTIVITY_FINISHED],
167
            [Error::MEDIA_ERROR_INVALID_REQUEST, CurrentPlaybackState::PLAYER_ACTIVITY_IDLE],
168
            [Error::MEDIA_ERROR_INTERNAL_SERVER_ERROR, CurrentPlaybackState::PLAYER_ACTIVITY_PAUSED],
169
            [Error::MEDIA_ERROR_INTERNAL_DEVICE_ERROR, CurrentPlaybackState::PLAYER_ACTIVITY_BUFFER_UNDERRUN],
170
        ];
171
    }
172
}
173