| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-06 23:38:04 +01:00
										 |  |  | namespace Tests\FFMpeg\Unit; | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 10:46:53 +01:00
										 |  |  | use Alchemy\BinaryDriver\Configuration; | 
					
						
							|  |  |  | use Alchemy\BinaryDriver\ConfigurationInterface; | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  | use FFMpeg\FFProbe; | 
					
						
							| 
									
										
										
										
											2021-12-20 10:46:53 +01:00
										 |  |  | use Symfony\Component\Cache\CacheItem; | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  | use Symfony\Component\Process\ExecutableFinder; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FFProbeTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function testGetSetParser() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); | 
					
						
							|  |  |  |         $parser = $this->getFFProbeParserMock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe->setParser($parser); | 
					
						
							|  |  |  |         $this->assertSame($parser, $ffprobe->getParser()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testGetSetFFProbeDriver() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); | 
					
						
							|  |  |  |         $driver = $this->getFFProbeDriverMock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe->setFFProbeDriver($driver); | 
					
						
							|  |  |  |         $this->assertSame($driver, $ffprobe->getFFProbeDriver()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testGetSetFFProbeMapper() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); | 
					
						
							|  |  |  |         $mapper = $this->getFFProbeMapperMock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe->setMapper($mapper); | 
					
						
							|  |  |  |         $this->assertSame($mapper, $ffprobe->getMapper()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testGetSetOptionsTester() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); | 
					
						
							|  |  |  |         $tester = $this->getFFProbeOptionsTesterMock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe->setOptionsTester($tester); | 
					
						
							|  |  |  |         $this->assertSame($tester, $ffprobe->getOptionsTester()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testGetSetCache() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); | 
					
						
							|  |  |  |         $cache = $this->getCacheMock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe->setCache($cache); | 
					
						
							|  |  |  |         $this->assertSame($cache, $ffprobe->getCache()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function provideDataWhitoutCache() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $stream = $this->getStreamMock(); | 
					
						
							|  |  |  |         $format = $this->getFormatMock(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         return [ | 
					
						
							|  |  |  |             [$stream, 'streams', ['-show_streams', '-print_format'], FFProbe::TYPE_STREAMS, [__FILE__, '-show_streams', '-print_format', 'json'], false], | 
					
						
							|  |  |  |             [$format, 'format', ['-show_format', '-print_format'], FFProbe::TYPE_FORMAT, [__FILE__, '-show_format', '-print_format', 'json'], false], | 
					
						
							|  |  |  |             [$stream, 'streams', ['-show_streams'], FFProbe::TYPE_STREAMS, [__FILE__, '-show_streams'], true], | 
					
						
							|  |  |  |             [$format, 'format', ['-show_format'], FFProbe::TYPE_FORMAT, [__FILE__, '-show_format'], true], | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider provideDataWhitoutCache | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testProbeWithoutCache($output, $method, $commands, $type, $caughtCommands, $isRaw) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $pathfile = __FILE__; | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         $data = ['key' => 'value']; | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |         $rawData = 'raw data'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $mapper = $this->getFFProbeMapperMock(); | 
					
						
							|  |  |  |         $mapper->expects($this->once()) | 
					
						
							|  |  |  |             ->method('map') | 
					
						
							|  |  |  |             ->with($type, $data) | 
					
						
							|  |  |  |             ->will($this->returnValue($output)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $parser = $this->getFFProbeParserMock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($isRaw) { | 
					
						
							|  |  |  |             $parser->expects($this->once()) | 
					
						
							|  |  |  |                 ->method('parse') | 
					
						
							|  |  |  |                 ->with($type, $rawData) | 
					
						
							|  |  |  |                 ->will($this->returnValue($data)); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $parser->expects($this->never()) | 
					
						
							|  |  |  |                 ->method('parse'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $tester = $this->getFFProbeOptionsTesterMockWithOptions($commands); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cache = $this->getCacheMock(); | 
					
						
							|  |  |  |         $cache->expects($this->once()) | 
					
						
							| 
									
										
										
										
											2021-12-20 10:46:53 +01:00
										 |  |  |             ->method('hasItem') | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |             ->will($this->returnValue(false)); | 
					
						
							| 
									
										
										
										
											2021-12-20 10:46:53 +01:00
										 |  |  |         $cache->expects($this->once()) | 
					
						
							|  |  |  |             ->method('getItem') | 
					
						
							|  |  |  |             ->will($this->returnValue(new CacheItem)); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |         $cache->expects($this->once()) | 
					
						
							|  |  |  |             ->method('save') | 
					
						
							| 
									
										
										
										
											2021-12-20 10:46:53 +01:00
										 |  |  |             ->with($this->anything()); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $driver = $this->getFFProbeDriverMock(); | 
					
						
							|  |  |  |         $driver->expects($this->once()) | 
					
						
							|  |  |  |             ->method('command') | 
					
						
							|  |  |  |             ->with($caughtCommands) | 
					
						
							|  |  |  |             ->will($this->returnValue($isRaw ? $rawData : json_encode($data))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe->setOptionsTester($tester) | 
					
						
							|  |  |  |             ->setCache($cache) | 
					
						
							|  |  |  |             ->setMapper($mapper) | 
					
						
							|  |  |  |             ->setFFProbeDriver($driver) | 
					
						
							|  |  |  |             ->setParser($parser); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         $this->assertEquals($output, call_user_func([$ffprobe, $method], $pathfile)); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function provideDataForInvalidJson() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $stream = $this->getStreamMock(); | 
					
						
							|  |  |  |         $format = $this->getFormatMock(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         return [ | 
					
						
							|  |  |  |             [$stream, 'streams', ['-show_streams', '-print_format'], FFProbe::TYPE_STREAMS, [__FILE__, '-show_streams', '-print_format', 'json']], | 
					
						
							|  |  |  |             [$format, 'format', ['-show_format', '-print_format'], FFProbe::TYPE_FORMAT, [__FILE__, '-show_format', '-print_format', 'json']], | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider provideDataForInvalidJson | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testProbeWithWrongJson($output, $method, $commands, $type, $caughtCommands) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $pathfile = __FILE__; | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         $data = ['key' => 'value']; | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $mapper = $this->getFFProbeMapperMock(); | 
					
						
							|  |  |  |         $mapper->expects($this->once()) | 
					
						
							|  |  |  |             ->method('map') | 
					
						
							|  |  |  |             ->with($this->isType('string'), 'good data parsed') | 
					
						
							|  |  |  |             ->will($this->returnValue($output)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $parser = $this->getFFProbeParserMock(); | 
					
						
							|  |  |  |         $parser->expects($this->once()) | 
					
						
							|  |  |  |             ->method('parse') | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |             ->with($this->isType('string', json_encode($data).'lala')) | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |             ->will($this->returnValue('good data parsed')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $tester = $this->getFFProbeOptionsTesterMockWithOptions($commands); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cache = $this->getCacheMock(); | 
					
						
							|  |  |  |         $cache->expects($this->exactly(2)) | 
					
						
							| 
									
										
										
										
											2021-12-20 10:46:53 +01:00
										 |  |  |             ->method('hasItem') | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |             ->will($this->returnValue(false)); | 
					
						
							| 
									
										
										
										
											2021-12-20 10:46:53 +01:00
										 |  |  |         $cache->expects($this->once()) | 
					
						
							|  |  |  |             ->method('getItem') | 
					
						
							|  |  |  |             ->will($this->returnValue(new CacheItem)); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $driver = $this->getFFProbeDriverMock(); | 
					
						
							|  |  |  |         $driver->expects($this->exactly(2)) | 
					
						
							|  |  |  |             ->method('command') | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |             ->will($this->returnValue(json_encode($data).'lala')); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe->setOptionsTester($tester) | 
					
						
							|  |  |  |             ->setCache($cache) | 
					
						
							|  |  |  |             ->setMapper($mapper) | 
					
						
							|  |  |  |             ->setFFProbeDriver($driver) | 
					
						
							|  |  |  |             ->setParser($parser); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         $this->assertEquals($output, call_user_func([$ffprobe, $method], $pathfile)); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function provideProbingDataWithCache() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $stream = $this->getStreamMock(); | 
					
						
							|  |  |  |         $format = $this->getFormatMock(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         return [ | 
					
						
							|  |  |  |             [$stream, 'streams'], | 
					
						
							|  |  |  |             [$format, 'format'], | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider provideProbingDataWithCache | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testProbeWithCache($output, $method) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $pathfile = __FILE__; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $mapper = $this->getFFProbeMapperMock(); | 
					
						
							|  |  |  |         $mapper->expects($this->never()) | 
					
						
							|  |  |  |             ->method('map'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $tester = $this->getFFProbeOptionsTesterMock(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 10:46:53 +01:00
										 |  |  |         $cacheItem = new CacheItem; | 
					
						
							|  |  |  |         $cacheItem->set($output); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |         $cache = $this->getCacheMock(); | 
					
						
							|  |  |  |         $cache->expects($this->once()) | 
					
						
							| 
									
										
										
										
											2021-12-20 10:46:53 +01:00
										 |  |  |             ->method('hasItem') | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |             ->will($this->returnValue(true)); | 
					
						
							|  |  |  |         $cache->expects($this->once()) | 
					
						
							| 
									
										
										
										
											2021-12-20 10:46:53 +01:00
										 |  |  |             ->method('getItem') | 
					
						
							|  |  |  |             ->will($this->returnValue($cacheItem)); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |         $cache->expects($this->never()) | 
					
						
							|  |  |  |             ->method('save'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $driver = $this->getFFProbeDriverMock(); | 
					
						
							|  |  |  |         $driver->expects($this->never()) | 
					
						
							|  |  |  |             ->method('command'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe->setOptionsTester($tester) | 
					
						
							|  |  |  |             ->setCache($cache) | 
					
						
							|  |  |  |             ->setMapper($mapper) | 
					
						
							|  |  |  |             ->setFFProbeDriver($driver); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         $this->assertEquals($output, call_user_func([$ffprobe, $method], $pathfile)); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function provideProbeMethod() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         return [ | 
					
						
							|  |  |  |             ['streams'], | 
					
						
							|  |  |  |             ['format'], | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider provideProbeMethod | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testProbeWithoutShowStreamsAvailable($method) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-13 00:39:31 +01:00
										 |  |  |         $this->expectException('\FFMpeg\Exception\RuntimeException'); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |         $pathfile = __FILE__; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe = new FFProbe($this->getFFProbeDriverMock(), $this->getCacheMock()); | 
					
						
							|  |  |  |         $ffprobe->setOptionsTester($this->getFFProbeOptionsTesterMock()); | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         call_user_func([$ffprobe, $method], $pathfile); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider provideCreateOptions | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testCreate($logger, $conf, $cache) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $finder = new ExecutableFinder(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $found = false; | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         foreach (['avprobe', 'ffprobe'] as $name) { | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |             if (null !== $finder->find($name)) { | 
					
						
							|  |  |  |                 $found = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$found) { | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |             $this->markTestSkipped('Unable to find avprobe or ffprobe on system'); | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe = FFProbe::create(); | 
					
						
							|  |  |  |         $this->assertInstanceOf('FFMpeg\FFprobe', $ffprobe); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $ffprobe = FFProbe::create($conf, $logger, $cache); | 
					
						
							|  |  |  |         $this->assertInstanceOf('FFMpeg\FFprobe', $ffprobe); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null !== $cache) { | 
					
						
							|  |  |  |             $this->assertSame($cache, $ffprobe->getCache()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null !== $logger) { | 
					
						
							|  |  |  |             $this->assertSame($logger, $ffprobe->getFFProbeDriver()->getProcessRunner()->getLogger()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($conf instanceof ConfigurationInterface) { | 
					
						
							|  |  |  |             $this->assertSame($conf, $ffprobe->getFFProbeDriver()->getConfiguration()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function provideCreateOptions() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-02-09 14:32:43 +01:00
										 |  |  |         return [ | 
					
						
							|  |  |  |             [null, ['key' => 'value'], null], | 
					
						
							|  |  |  |             [$this->getLoggerMock(), ['key' => 'value'], null], | 
					
						
							|  |  |  |             [null, new Configuration(), null], | 
					
						
							|  |  |  |             [null, ['key' => 'value'], $this->getCacheMock()], | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2013-06-25 10:03:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |