scratch – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | namespace Tests\FFMpeg\Unit; |
||
4 | |||
5 | use FFMpeg\FFProbe; |
||
6 | use Symfony\Component\Process\ExecutableFinder; |
||
7 | use Alchemy\BinaryDriver\ConfigurationInterface; |
||
8 | use Alchemy\BinaryDriver\Configuration; |
||
9 | |||
10 | class FFProbeTest extends TestCase |
||
11 | { |
||
12 | public function testGetSetParser() |
||
13 | { |
||
14 | $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); |
||
15 | $parser = $this->getFFProbeParserMock(); |
||
16 | |||
17 | $ffprobe->setParser($parser); |
||
18 | $this->assertSame($parser, $ffprobe->getParser()); |
||
19 | } |
||
20 | |||
21 | public function testGetSetFFProbeDriver() |
||
22 | { |
||
23 | $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); |
||
24 | $driver = $this->getFFProbeDriverMock(); |
||
25 | |||
26 | $ffprobe->setFFProbeDriver($driver); |
||
27 | $this->assertSame($driver, $ffprobe->getFFProbeDriver()); |
||
28 | } |
||
29 | |||
30 | public function testGetSetFFProbeMapper() |
||
31 | { |
||
32 | $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); |
||
33 | $mapper = $this->getFFProbeMapperMock(); |
||
34 | |||
35 | $ffprobe->setMapper($mapper); |
||
36 | $this->assertSame($mapper, $ffprobe->getMapper()); |
||
37 | } |
||
38 | |||
39 | public function testGetSetOptionsTester() |
||
40 | { |
||
41 | $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); |
||
42 | $tester = $this->getFFProbeOptionsTesterMock(); |
||
43 | |||
44 | $ffprobe->setOptionsTester($tester); |
||
45 | $this->assertSame($tester, $ffprobe->getOptionsTester()); |
||
46 | } |
||
47 | |||
48 | public function testGetSetCache() |
||
49 | { |
||
50 | $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); |
||
51 | $cache = $this->getCacheMock(); |
||
52 | |||
53 | $ffprobe->setCache($cache); |
||
54 | $this->assertSame($cache, $ffprobe->getCache()); |
||
55 | } |
||
56 | |||
57 | public function provideDataWhitoutCache() |
||
58 | { |
||
59 | $stream = $this->getStreamMock(); |
||
60 | $format = $this->getFormatMock(); |
||
61 | |||
62 | return array( |
||
63 | array($stream, 'streams', array('-show_streams', '-print_format'), FFProbe::TYPE_STREAMS, array(__FILE__, '-show_streams', '-print_format', 'json'), false), |
||
64 | array($format, 'format', array('-show_format', '-print_format'), FFProbe::TYPE_FORMAT, array(__FILE__, '-show_format', '-print_format', 'json'), false), |
||
65 | array($stream, 'streams', array('-show_streams'), FFProbe::TYPE_STREAMS, array(__FILE__, '-show_streams'), true), |
||
66 | array($format, 'format', array('-show_format'), FFProbe::TYPE_FORMAT, array(__FILE__, '-show_format'), true), |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @dataProvider provideDataWhitoutCache |
||
72 | */ |
||
73 | public function testProbeWithoutCache($output, $method, $commands, $type, $caughtCommands, $isRaw) |
||
74 | { |
||
75 | $pathfile = __FILE__; |
||
76 | $data = array('key' => 'value'); |
||
77 | $rawData = 'raw data'; |
||
78 | |||
79 | $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); |
||
80 | |||
81 | $mapper = $this->getFFProbeMapperMock(); |
||
82 | $mapper->expects($this->once()) |
||
83 | ->method('map') |
||
84 | ->with($type, $data) |
||
85 | ->will($this->returnValue($output)); |
||
86 | |||
87 | $parser = $this->getFFProbeParserMock(); |
||
88 | |||
89 | if ($isRaw) { |
||
90 | $parser->expects($this->once()) |
||
91 | ->method('parse') |
||
92 | ->with($type, $rawData) |
||
93 | ->will($this->returnValue($data)); |
||
94 | } else { |
||
95 | $parser->expects($this->never()) |
||
96 | ->method('parse'); |
||
97 | } |
||
98 | |||
99 | $tester = $this->getFFProbeOptionsTesterMockWithOptions($commands); |
||
100 | |||
101 | $cache = $this->getCacheMock(); |
||
102 | $cache->expects($this->once()) |
||
103 | ->method('contains') |
||
104 | ->will($this->returnValue(false)); |
||
105 | $cache->expects($this->never()) |
||
106 | ->method('fetch'); |
||
107 | $cache->expects($this->once()) |
||
108 | ->method('save') |
||
109 | ->with($this->anything(), $output); |
||
110 | |||
111 | $driver = $this->getFFProbeDriverMock(); |
||
112 | $driver->expects($this->once()) |
||
113 | ->method('command') |
||
114 | ->with($caughtCommands) |
||
115 | ->will($this->returnValue($isRaw ? $rawData : json_encode($data))); |
||
116 | |||
117 | $ffprobe->setOptionsTester($tester) |
||
118 | ->setCache($cache) |
||
119 | ->setMapper($mapper) |
||
120 | ->setFFProbeDriver($driver) |
||
121 | ->setParser($parser); |
||
122 | |||
123 | $this->assertEquals($output, call_user_func(array($ffprobe, $method), $pathfile)); |
||
124 | } |
||
125 | |||
126 | public function provideDataForInvalidJson() |
||
127 | { |
||
128 | $stream = $this->getStreamMock(); |
||
129 | $format = $this->getFormatMock(); |
||
130 | |||
131 | return array( |
||
132 | array($stream, 'streams', array('-show_streams', '-print_format'), FFProbe::TYPE_STREAMS, array(__FILE__, '-show_streams', '-print_format', 'json')), |
||
133 | array($format, 'format', array('-show_format', '-print_format'), FFProbe::TYPE_FORMAT, array(__FILE__, '-show_format', '-print_format', 'json')), |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @dataProvider provideDataForInvalidJson |
||
139 | */ |
||
140 | public function testProbeWithWrongJson($output, $method, $commands, $type, $caughtCommands) |
||
141 | { |
||
142 | $pathfile = __FILE__; |
||
143 | $data = array('key' => 'value'); |
||
144 | |||
145 | $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); |
||
146 | |||
147 | $mapper = $this->getFFProbeMapperMock(); |
||
148 | $mapper->expects($this->once()) |
||
149 | ->method('map') |
||
150 | ->with($this->isType('string'), 'good data parsed') |
||
151 | ->will($this->returnValue($output)); |
||
152 | |||
153 | $parser = $this->getFFProbeParserMock(); |
||
154 | $parser->expects($this->once()) |
||
155 | ->method('parse') |
||
156 | ->with($this->isType('string', json_encode($data) . 'lala')) |
||
157 | ->will($this->returnValue('good data parsed')); |
||
158 | |||
159 | $tester = $this->getFFProbeOptionsTesterMockWithOptions($commands); |
||
160 | |||
161 | $cache = $this->getCacheMock(); |
||
162 | $cache->expects($this->exactly(2)) |
||
163 | ->method('contains') |
||
164 | ->will($this->returnValue(false)); |
||
165 | $cache->expects($this->never()) |
||
166 | ->method('fetch'); |
||
167 | |||
168 | $driver = $this->getFFProbeDriverMock(); |
||
169 | $driver->expects($this->exactly(2)) |
||
170 | ->method('command') |
||
171 | ->will($this->returnValue(json_encode($data) . 'lala')); |
||
172 | |||
173 | $ffprobe->setOptionsTester($tester) |
||
174 | ->setCache($cache) |
||
175 | ->setMapper($mapper) |
||
176 | ->setFFProbeDriver($driver) |
||
177 | ->setParser($parser); |
||
178 | |||
179 | $this->assertEquals($output, call_user_func(array($ffprobe, $method), $pathfile)); |
||
180 | } |
||
181 | |||
182 | public function provideProbingDataWithCache() |
||
183 | { |
||
184 | $stream = $this->getStreamMock(); |
||
185 | $format = $this->getFormatMock(); |
||
186 | |||
187 | return array( |
||
188 | array($stream, 'streams'), |
||
189 | array($format, 'format'), |
||
190 | ); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @dataProvider provideProbingDataWithCache |
||
195 | */ |
||
196 | public function testProbeWithCache($output, $method) |
||
197 | { |
||
198 | $pathfile = __FILE__; |
||
199 | |||
200 | $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); |
||
201 | |||
202 | $mapper = $this->getFFProbeMapperMock(); |
||
203 | $mapper->expects($this->never()) |
||
204 | ->method('map'); |
||
205 | |||
206 | $tester = $this->getFFProbeOptionsTesterMock(); |
||
207 | |||
208 | $cache = $this->getCacheMock(); |
||
209 | $cache->expects($this->once()) |
||
210 | ->method('contains') |
||
211 | ->will($this->returnValue(true)); |
||
212 | $cache->expects($this->once()) |
||
213 | ->method('fetch') |
||
214 | ->will($this->returnValue($output)); |
||
215 | $cache->expects($this->never()) |
||
216 | ->method('save'); |
||
217 | |||
218 | $driver = $this->getFFProbeDriverMock(); |
||
219 | $driver->expects($this->never()) |
||
220 | ->method('command'); |
||
221 | |||
222 | $ffprobe->setOptionsTester($tester) |
||
223 | ->setCache($cache) |
||
224 | ->setMapper($mapper) |
||
225 | ->setFFProbeDriver($driver); |
||
226 | |||
227 | $this->assertEquals($output, call_user_func(array($ffprobe, $method), $pathfile)); |
||
228 | } |
||
229 | |||
230 | public function provideProbeMethod() |
||
231 | { |
||
232 | return array( |
||
233 | array('streams'), |
||
234 | array('format'), |
||
235 | ); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @expectedException FFMpeg\Exception\RuntimeException |
||
240 | * @dataProvider provideProbeMethod |
||
241 | */ |
||
242 | public function testProbeWithoutShowStreamsAvailable($method) |
||
243 | { |
||
244 | $pathfile = __FILE__; |
||
245 | |||
246 | $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); |
||
247 | $ffprobe->setOptionsTester($this->getFFProbeOptionsTesterMock()); |
||
248 | call_user_func(array($ffprobe, $method), $pathfile); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @dataProvider provideCreateOptions |
||
253 | */ |
||
254 | public function testCreate($logger, $conf, $cache) |
||
255 | { |
||
256 | $finder = new ExecutableFinder(); |
||
257 | |||
258 | $found = false; |
||
259 | foreach (array('avprobe', 'ffprobe') as $name) { |
||
260 | if (null !== $finder->find($name)) { |
||
261 | $found = true; |
||
262 | } |
||
263 | } |
||
264 | |||
265 | if (!$found) { |
||
266 | $this->markTestSkipped("Unable to find avprobe or ffprobe on system"); |
||
267 | } |
||
268 | |||
269 | $ffprobe = FFProbe::create(); |
||
270 | $this->assertInstanceOf('FFMpeg\FFprobe', $ffprobe); |
||
271 | |||
272 | $ffprobe = FFProbe::create($conf, $logger, $cache); |
||
273 | $this->assertInstanceOf('FFMpeg\FFprobe', $ffprobe); |
||
274 | |||
275 | if (null !== $cache) { |
||
276 | $this->assertSame($cache, $ffprobe->getCache()); |
||
277 | } |
||
278 | if (null !== $logger) { |
||
279 | $this->assertSame($logger, $ffprobe->getFFProbeDriver()->getProcessRunner()->getLogger()); |
||
280 | } |
||
281 | if ($conf instanceof ConfigurationInterface) { |
||
282 | $this->assertSame($conf, $ffprobe->getFFProbeDriver()->getConfiguration()); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | public function provideCreateOptions() |
||
287 | { |
||
288 | return array( |
||
289 | array(null, array('key' => 'value'), null), |
||
290 | array($this->getLoggerMock(), array('key' => 'value'), null), |
||
291 | array(null, new Configuration(), null), |
||
292 | array(null, array('key' => 'value'), $this->getCacheMock()), |
||
293 | ); |
||
294 | } |
||
295 | } |