scratch – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | namespace Tests\FFMpeg\Unit\Media; |
||
4 | |||
5 | use FFMpeg\Media\Audio; |
||
6 | use Alchemy\BinaryDriver\Exception\ExecutionFailureException; |
||
7 | use FFMpeg\Format\AudioInterface; |
||
8 | |||
9 | class AudioTest extends AbstractStreamableTestCase |
||
10 | { |
||
11 | public function testFiltersReturnsAudioFilters() |
||
12 | { |
||
13 | $driver = $this->getFFMpegDriverMock(); |
||
14 | $ffprobe = $this->getFFProbeMock(); |
||
15 | |||
16 | $audio = new Audio(__FILE__, $driver, $ffprobe); |
||
17 | $this->assertInstanceOf('FFMpeg\Filters\Audio\AudioFilters', $audio->filters()); |
||
18 | } |
||
19 | |||
20 | public function testAddFiltersAddsAFilter() |
||
21 | { |
||
22 | $driver = $this->getFFMpegDriverMock(); |
||
23 | $ffprobe = $this->getFFProbeMock(); |
||
24 | |||
25 | $filters = $this->getMockBuilder('FFMpeg\Filters\FiltersCollection') |
||
26 | ->disableOriginalConstructor() |
||
27 | ->getMock(); |
||
28 | |||
29 | $audio = new Audio(__FILE__, $driver, $ffprobe); |
||
30 | $audio->setFiltersCollection($filters); |
||
31 | |||
32 | $filter = $this->getMock('FFMpeg\Filters\Audio\AudioFilterInterface'); |
||
33 | |||
34 | $filters->expects($this->once()) |
||
35 | ->method('add') |
||
36 | ->with($filter); |
||
37 | |||
38 | $audio->addFilter($filter); |
||
39 | } |
||
40 | |||
41 | public function testAddAVideoFilterThrowsException() |
||
42 | { |
||
43 | $driver = $this->getFFMpegDriverMock(); |
||
44 | $ffprobe = $this->getFFProbeMock(); |
||
45 | |||
46 | $filters = $this->getMockBuilder('FFMpeg\Filters\FiltersCollection') |
||
47 | ->disableOriginalConstructor() |
||
48 | ->getMock(); |
||
49 | |||
50 | $audio = new Audio(__FILE__, $driver, $ffprobe); |
||
51 | $audio->setFiltersCollection($filters); |
||
52 | |||
53 | $filter = $this->getMock('FFMpeg\Filters\Video\VideoFilterInterface'); |
||
54 | |||
55 | $filters->expects($this->never()) |
||
56 | ->method('add'); |
||
57 | |||
58 | $this->setExpectedException('FFMpeg\Exception\InvalidArgumentException'); |
||
59 | $audio->addFilter($filter); |
||
60 | } |
||
61 | |||
62 | public function testSaveWithFailure() |
||
63 | { |
||
64 | $driver = $this->getFFMpegDriverMock(); |
||
65 | $ffprobe = $this->getFFProbeMock(); |
||
66 | $outputPathfile = '/target/file'; |
||
67 | |||
68 | $format = $this->getMock('FFMpeg\Format\AudioInterface'); |
||
69 | $format->expects($this->any()) |
||
70 | ->method('getExtraParams') |
||
71 | ->will($this->returnValue(array())); |
||
72 | |||
73 | $configuration = $this->getMock('Alchemy\BinaryDriver\ConfigurationInterface'); |
||
74 | |||
75 | $driver->expects($this->any()) |
||
76 | ->method('getConfiguration') |
||
77 | ->will($this->returnValue($configuration)); |
||
78 | |||
79 | $failure = new ExecutionFailureException('failed to encode'); |
||
80 | $driver->expects($this->once()) |
||
81 | ->method('command') |
||
82 | ->will($this->throwException($failure)); |
||
83 | |||
84 | $audio = new Audio(__FILE__, $driver, $ffprobe); |
||
85 | $this->setExpectedException('FFMpeg\Exception\RuntimeException'); |
||
86 | $audio->save($format, $outputPathfile); |
||
87 | } |
||
88 | |||
89 | public function testSaveAppliesFilters() |
||
90 | { |
||
91 | $driver = $this->getFFMpegDriverMock(); |
||
92 | $ffprobe = $this->getFFProbeMock(); |
||
93 | $outputPathfile = '/target/file'; |
||
94 | $format = $this->getMock('FFMpeg\Format\AudioInterface'); |
||
95 | $format->expects($this->any()) |
||
96 | ->method('getExtraParams') |
||
97 | ->will($this->returnValue(array())); |
||
98 | |||
99 | $configuration = $this->getMock('Alchemy\BinaryDriver\ConfigurationInterface'); |
||
100 | |||
101 | $driver->expects($this->any()) |
||
102 | ->method('getConfiguration') |
||
103 | ->will($this->returnValue($configuration)); |
||
104 | |||
105 | $audio = new Audio(__FILE__, $driver, $ffprobe); |
||
106 | |||
107 | $filter = $this->getMock('FFMpeg\Filters\Audio\AudioFilterInterface'); |
||
108 | $filter->expects($this->once()) |
||
109 | ->method('apply') |
||
110 | ->with($audio, $format) |
||
111 | ->will($this->returnValue(array('extra-filter-command'))); |
||
112 | |||
113 | $capturedCommands = array(); |
||
114 | |||
115 | $driver->expects($this->once()) |
||
116 | ->method('command') |
||
117 | ->with($this->isType('array'), false, $this->anything()) |
||
118 | ->will($this->returnCallback(function ($commands, $errors, $listeners) use (&$capturedCommands) { |
||
119 | $capturedCommands[] = $commands; |
||
120 | })); |
||
121 | |||
122 | $audio->addFilter($filter); |
||
123 | $audio->save($format, $outputPathfile); |
||
124 | |||
125 | foreach ($capturedCommands as $commands) { |
||
126 | $this->assertEquals('-y', $commands[0]); |
||
127 | $this->assertEquals('-i', $commands[1]); |
||
128 | $this->assertEquals(__FILE__, $commands[2]); |
||
129 | $this->assertEquals('extra-filter-command', $commands[3]); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @dataProvider provideSaveData |
||
135 | */ |
||
136 | public function testSaveShouldSave($threads, $expectedCommands, $expectedListeners, $format) |
||
137 | { |
||
138 | $driver = $this->getFFMpegDriverMock(); |
||
139 | $ffprobe = $this->getFFProbeMock(); |
||
140 | |||
141 | $configuration = $this->getMock('Alchemy\BinaryDriver\ConfigurationInterface'); |
||
142 | |||
143 | $driver->expects($this->any()) |
||
144 | ->method('getConfiguration') |
||
145 | ->will($this->returnValue($configuration)); |
||
146 | |||
147 | $configuration->expects($this->once()) |
||
148 | ->method('has') |
||
149 | ->with($this->equalTo('ffmpeg.threads')) |
||
150 | ->will($this->returnValue($threads)); |
||
151 | |||
152 | if ($threads) { |
||
153 | $configuration->expects($this->once()) |
||
154 | ->method('get') |
||
155 | ->with($this->equalTo('ffmpeg.threads')) |
||
156 | ->will($this->returnValue(24)); |
||
157 | } else { |
||
158 | $configuration->expects($this->never()) |
||
159 | ->method('get'); |
||
160 | } |
||
161 | |||
162 | $capturedCommand = $capturedListeners = null; |
||
163 | |||
164 | $driver->expects($this->once()) |
||
165 | ->method('command') |
||
166 | ->with($this->isType('array'), false, $this->anything()) |
||
167 | ->will($this->returnCallback(function ($commands, $errors, $listeners) use (&$capturedCommand, &$capturedListeners) { |
||
168 | $capturedCommand = $commands; |
||
169 | $capturedListeners = $listeners; |
||
170 | })); |
||
171 | |||
172 | $outputPathfile = '/target/file'; |
||
173 | |||
174 | $audio = new Audio(__FILE__, $driver, $ffprobe); |
||
175 | $audio->save($format, $outputPathfile); |
||
176 | |||
177 | $this->assertEquals($expectedCommands, $capturedCommand); |
||
178 | $this->assertEquals($expectedListeners, $capturedListeners); |
||
179 | } |
||
180 | |||
181 | public function provideSaveData() |
||
182 | { |
||
183 | $format = $this->getMock('FFMpeg\Format\AudioInterface'); |
||
184 | $format->expects($this->any()) |
||
185 | ->method('getExtraParams') |
||
186 | ->will($this->returnValue(array())); |
||
187 | $format->expects($this->any()) |
||
188 | ->method('getAudioKiloBitrate') |
||
189 | ->will($this->returnValue(663)); |
||
190 | $format->expects($this->any()) |
||
191 | ->method('getAudioChannels') |
||
192 | ->will($this->returnValue(5)); |
||
193 | |||
194 | $audioFormat = $this->getMock('FFMpeg\Format\AudioInterface'); |
||
195 | $audioFormat->expects($this->any()) |
||
196 | ->method('getExtraParams') |
||
197 | ->will($this->returnValue(array())); |
||
198 | $audioFormat->expects($this->any()) |
||
199 | ->method('getAudioKiloBitrate') |
||
200 | ->will($this->returnValue(664)); |
||
201 | $audioFormat->expects($this->any()) |
||
202 | ->method('getAudioChannels') |
||
203 | ->will($this->returnValue(5)); |
||
204 | $audioFormat->expects($this->any()) |
||
205 | ->method('getAudioCodec') |
||
206 | ->will($this->returnValue('patati-patata-audio')); |
||
207 | |||
208 | $formatExtra = $this->getMock('FFMpeg\Format\AudioInterface'); |
||
209 | $formatExtra->expects($this->any()) |
||
210 | ->method('getExtraParams') |
||
211 | ->will($this->returnValue(array('extra', 'param'))); |
||
212 | $formatExtra->expects($this->any()) |
||
213 | ->method('getAudioKiloBitrate') |
||
214 | ->will($this->returnValue(665)); |
||
215 | $formatExtra->expects($this->any()) |
||
216 | ->method('getAudioChannels') |
||
217 | ->will($this->returnValue(5)); |
||
218 | |||
219 | $listeners = array($this->getMock('Alchemy\BinaryDriver\Listeners\ListenerInterface')); |
||
220 | |||
221 | $progressableFormat = $this->getMockBuilder('Tests\FFMpeg\Unit\Media\AudioProg') |
||
222 | ->disableOriginalConstructor()->getMock(); |
||
223 | $progressableFormat->expects($this->any()) |
||
224 | ->method('getExtraParams') |
||
225 | ->will($this->returnValue(array())); |
||
226 | $progressableFormat->expects($this->any()) |
||
227 | ->method('createProgressListener') |
||
228 | ->will($this->returnValue($listeners)); |
||
229 | $progressableFormat->expects($this->any()) |
||
230 | ->method('getAudioKiloBitrate') |
||
231 | ->will($this->returnValue(666)); |
||
232 | $progressableFormat->expects($this->any()) |
||
233 | ->method('getAudioChannels') |
||
234 | ->will($this->returnValue(5)); |
||
235 | |||
236 | return array( |
||
237 | array(false, array( |
||
238 | '-y', '-i', __FILE__, |
||
239 | '-b:a', '663k', |
||
240 | '-ac', '5', |
||
241 | '/target/file', |
||
242 | ), null, $format), |
||
243 | array(false, array( |
||
244 | '-y', '-i', __FILE__, |
||
245 | '-acodec', 'patati-patata-audio', |
||
246 | '-b:a', '664k', |
||
247 | '-ac', '5', |
||
248 | '/target/file', |
||
249 | ), null, $audioFormat), |
||
250 | array(false, array( |
||
251 | '-y', '-i', __FILE__, |
||
252 | 'extra', 'param', |
||
253 | '-b:a', '665k', |
||
254 | '-ac', '5', |
||
255 | '/target/file', |
||
256 | ), null, $formatExtra), |
||
257 | array(true, array( |
||
258 | '-y', '-i', __FILE__, |
||
259 | '-threads', 24, |
||
260 | '-b:a', '663k', |
||
261 | '-ac', '5', |
||
262 | '/target/file', |
||
263 | ), null, $format), |
||
264 | array(true, array( |
||
265 | '-y', '-i', __FILE__, |
||
266 | 'extra', 'param', |
||
267 | '-threads', 24, |
||
268 | '-b:a', '665k', |
||
269 | '-ac', '5', |
||
270 | '/target/file', |
||
271 | ), null, $formatExtra), |
||
272 | array(false, array( |
||
273 | '-y', '-i', __FILE__, |
||
274 | '-b:a', '666k', |
||
275 | '-ac', '5', |
||
276 | '/target/file', |
||
277 | ), $listeners, $progressableFormat), |
||
278 | array(true, array( |
||
279 | '-y', '-i', __FILE__, |
||
280 | '-threads', 24, |
||
281 | '-b:a', '666k', |
||
282 | '-ac', '5', |
||
283 | '/target/file', |
||
284 | ), $listeners, $progressableFormat), |
||
285 | ); |
||
286 | } |
||
287 | |||
288 | public function testSaveShouldNotStoreCodecFiltersInTheMedia() |
||
289 | { |
||
290 | $driver = $this->getFFMpegDriverMock(); |
||
291 | $ffprobe = $this->getFFProbeMock(); |
||
292 | |||
293 | $configuration = $this->getMock('Alchemy\BinaryDriver\ConfigurationInterface'); |
||
294 | |||
295 | $driver->expects($this->any()) |
||
296 | ->method('getConfiguration') |
||
297 | ->will($this->returnValue($configuration)); |
||
298 | |||
299 | $configuration->expects($this->any()) |
||
300 | ->method('has') |
||
301 | ->with($this->equalTo('ffmpeg.threads')) |
||
302 | ->will($this->returnValue(true)); |
||
303 | |||
304 | $configuration->expects($this->any()) |
||
305 | ->method('get') |
||
306 | ->with($this->equalTo('ffmpeg.threads')) |
||
307 | ->will($this->returnValue(24)); |
||
308 | |||
309 | $capturedCommands = array(); |
||
310 | |||
311 | $driver->expects($this->exactly(2)) |
||
312 | ->method('command') |
||
313 | ->with($this->isType('array'), false, $this->anything()) |
||
314 | ->will($this->returnCallback(function ($commands, $errors, $listeners) use (&$capturedCommands, &$capturedListeners) { |
||
315 | $capturedCommands[] = $commands; |
||
316 | })); |
||
317 | |||
318 | $outputPathfile = '/target/file'; |
||
319 | |||
320 | $format = $this->getMock('FFMpeg\Format\AudioInterface'); |
||
321 | $format->expects($this->any()) |
||
322 | ->method('getExtraParams') |
||
323 | ->will($this->returnValue(array('param'))); |
||
324 | |||
325 | $audio = new Audio(__FILE__, $driver, $ffprobe); |
||
326 | $audio->save($format, $outputPathfile); |
||
327 | $audio->save($format, $outputPathfile); |
||
328 | |||
329 | $expected = array( |
||
330 | '-y', '-i', __FILE__, 'param', '-threads', 24, '/target/file', |
||
331 | ); |
||
332 | |||
333 | foreach ($capturedCommands as $capturedCommand) { |
||
334 | $this->assertEquals($expected, $capturedCommand); |
||
335 | } |
||
336 | } |
||
337 | |||
338 | public function getClassName() |
||
339 | { |
||
340 | return 'FFMpeg\Media\Audio'; |
||
341 | } |
||
342 | } |