From 62e02f016318d58d278a088423d7d0fb80ffa752 Mon Sep 17 00:00:00 2001 From: Maxime Horcholle Date: Sun, 29 Mar 2020 19:49:37 +0200 Subject: [PATCH] Update symfony/process component call --- .travis.yml | 2 + src/Builder/MediaInfoCommandBuilder.php | 42 ++++++++- src/DumpTrait.php | 8 +- src/Runner/MediaInfoCommandRunner.php | 87 ++---------------- test/Builder/MediaInfoCommandBuilderTest.php | 77 +++++++++++++--- .../Builder/MediaInfoContainerBuilderTest.php | 6 ++ test/Container/MediaInfoContainerTest.php | 5 +- .../UnknownTrackTypeExceptionTest.php | 15 ++++ test/Runner/MediaInfoCommandRunnerTest.php | 89 +++++++++---------- 9 files changed, 183 insertions(+), 148 deletions(-) create mode 100644 test/Exception/UnknownTrackTypeExceptionTest.php diff --git a/.travis.yml b/.travis.yml index 616c0ac..5eb3bf5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,8 @@ matrix: env: SYMFONY_VERSION=4.0.* - php: 7.4 env: SYMFONY_VERSION=4.4.* + - php: 7.4 + env: SYMFONY_VERSION=5.0.* env: global: diff --git a/src/Builder/MediaInfoCommandBuilder.php b/src/Builder/MediaInfoCommandBuilder.php index fc0b9c1..e794ed0 100644 --- a/src/Builder/MediaInfoCommandBuilder.php +++ b/src/Builder/MediaInfoCommandBuilder.php @@ -4,6 +4,7 @@ use Mhor\MediaInfo\Runner\MediaInfoCommandRunner; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Process\Process; class MediaInfoCommandBuilder { @@ -15,7 +16,7 @@ class MediaInfoCommandBuilder * * @return MediaInfoCommandRunner */ - public function buildMediaInfoCommandRunner($filePath, array $configuration = []) + public function buildMediaInfoCommandRunner(string $filePath, array $configuration = []): MediaInfoCommandRunner { if (filter_var($filePath, FILTER_VALIDATE_URL) === false) { $fileSystem = new Filesystem(); @@ -37,12 +38,45 @@ public function buildMediaInfoCommandRunner($filePath, array $configuration = [] 'use_oldxml_mediainfo_output_format' => true, ]; - return new MediaInfoCommandRunner( + return new MediaInfoCommandRunner($this->buildMediaInfoProcess( $filePath, $configuration['command'], - null, - null, $configuration['use_oldxml_mediainfo_output_format'] + )); + } + + /** + * @param string $filePath + * @param string|null $command + * @param bool $forceOldXmlOutput + * + * @return Process + */ + private function buildMediaInfoProcess(string $filePath, string $command = null, array $arguments = null, bool $forceOldXmlOutput = true): Process + { + if ($command === null) { + $command = MediaInfoCommandRunner::MEDIAINFO_COMMAND; + } + + // arguments are given through ENV vars in order to have system escape them + $arguments = [ + 'MEDIAINFO_VAR_FILE_PATH' => $filePath, + 'MEDIAINFO_VAR_FULL_DISPLAY' => MediaInfoCommandRunner::MEDIAINFO_FULL_DISPLAY_ARGUMENT, + 'MEDIAINFO_VAR_OUTPUT' => MediaInfoCommandRunner::MEDIAINFO_OLDXML_OUTPUT_ARGUMENT, + ]; + + if ($forceOldXmlOutput === false) { + $arguments['MEDIAINFO_VAR_OUTPUT'] = MediaInfoCommandRunner::MEDIAINFO_XML_OUTPUT_ARGUMENT; + } + + $env = $arguments + [ + 'LANG' => setlocale(LC_CTYPE, 0), + ]; + + return new Process( + array_merge([$command], array_values($arguments)), + null, + $env ); } } diff --git a/src/DumpTrait.php b/src/DumpTrait.php index fbb465f..8f8d439 100644 --- a/src/DumpTrait.php +++ b/src/DumpTrait.php @@ -11,13 +11,7 @@ trait DumpTrait */ public function jsonSerialize() { - $array = get_object_vars($this); - - if (isset($array['attributes'])) { - $array = $array['attributes']; - } - - return $array; + return get_object_vars($this); } /** diff --git a/src/Runner/MediaInfoCommandRunner.php b/src/Runner/MediaInfoCommandRunner.php index 16fca8a..918f762 100644 --- a/src/Runner/MediaInfoCommandRunner.php +++ b/src/Runner/MediaInfoCommandRunner.php @@ -6,13 +6,10 @@ class MediaInfoCommandRunner { - const FORCED_OLDXML_OUTPUT_FORMAT_ARGUMENTS = ['--OUTPUT=OLDXML', '-f']; - const XML_OUTPUT_FORMAT_ARGUMENTS = ['--OUTPUT=XML', '-f']; - - /** - * @var string - */ - protected $filePath; + const MEDIAINFO_COMMAND = 'mediainfo'; + const MEDIAINFO_OLDXML_OUTPUT_ARGUMENT = '--OUTPUT=OLDXML'; + const MEDIAINFO_XML_OUTPUT_ARGUMENT = '--OUTPUT=XML'; + const MEDIAINFO_FULL_DISPLAY_ARGUMENT = '-f'; /** * @var Process @@ -20,77 +17,11 @@ class MediaInfoCommandRunner protected $process; /** - * @var string - */ - protected $command = 'mediainfo'; - - /** - * @var array - */ - protected $arguments = []; - - /** - * @param string $filePath - * @param string $command - * @param array $arguments * @param Process $process - * @param bool $forceOldXmlOutput */ - public function __construct( - $filePath, - $command = null, - array $arguments = null, - Process $process = null, - $forceOldXmlOutput = true - ) { - $this->filePath = $filePath; - if ($command !== null) { - $this->command = $command; - } - - $this->arguments = self::XML_OUTPUT_FORMAT_ARGUMENTS; - if ($forceOldXmlOutput) { - $this->arguments = self::FORCED_OLDXML_OUTPUT_FORMAT_ARGUMENTS; - } - - if ($arguments !== null) { - $this->arguments = $arguments; - } - - // /path/to/mediainfo $MEDIAINFO_VAR0 $MEDIAINFO_VAR1... - // args are given through ENV vars in order to have system escape them - - $args = $this->arguments; - array_unshift($args, $this->filePath); - - $env = [ - 'LANG' => setlocale(LC_CTYPE, 0), - ]; - $finalCommand = [$this->command]; - - $i = 0; - foreach ($args as $value) { - $var = 'MEDIAINFO_VAR_'.$i++; - - if (stripos(PHP_OS, 'WIN') === 0) { - // windows needs an other declaration of env-variables - $finalCommand[] = '"%'.$var.'%"'; - } else { - $finalCommand[] = '"$'.$var.'"'; - } - - $env[$var] = $value; - } - - $finalCommandString = implode(' ', $finalCommand); - - if (null !== $process) { - $process->setCommandLine($finalCommandString); - $process->setEnv($env); - $this->process = $process; - } else { - $this->process = new Process($finalCommandString, null, $env); - } + public function __construct(Process $process) + { + $this->process = $process; } /** @@ -129,10 +60,6 @@ public function start() */ public function wait() { - if ($this->process == null) { - throw new \Exception('You must run `start` before running `wait`'); - } - // blocks here until process completes $this->process->wait(); diff --git a/test/Builder/MediaInfoCommandBuilderTest.php b/test/Builder/MediaInfoCommandBuilderTest.php index b461ee1..27216d5 100644 --- a/test/Builder/MediaInfoCommandBuilderTest.php +++ b/test/Builder/MediaInfoCommandBuilderTest.php @@ -5,6 +5,7 @@ use Mhor\MediaInfo\Builder\MediaInfoCommandBuilder; use Mhor\MediaInfo\Runner\MediaInfoCommandRunner; use PHPUnit\Framework\TestCase; +use Symfony\Component\Process\Process; class MediaInfoCommandBuilderTest extends TestCase { @@ -15,18 +16,51 @@ protected function setUp(): void $this->filePath = __DIR__.'/../fixtures/test.mp3'; } - public function testBuilderCommandWithUrl() + public function testBuilderCommandWithHttpsUrl() { $mediaInfoCommandBuilder = new MediaInfoCommandBuilder(); $mediaInfoCommandRunner = $mediaInfoCommandBuilder->buildMediaInfoCommandRunner('https://example.org/'); - $equalsMediaInfoCommandRunner = new MediaInfoCommandRunner('https://example.org/'); + $equalsMediaInfoCommandRunner = new MediaInfoCommandRunner(new Process( + [ + 'mediainfo', + 'https://example.org/', + '-f', + '--OUTPUT=OLDXML', + ], + null, + [ + 'MEDIAINFO_VAR_FILE_PATH' => 'https://example.org/', + 'MEDIAINFO_VAR_FULL_DISPLAY' => '-f', + 'MEDIAINFO_VAR_OUTPUT' => '--OUTPUT=OLDXML', + 'LANG' => 'en_US.UTF-8', + ] + )); + $this->assertEquals($equalsMediaInfoCommandRunner, $mediaInfoCommandRunner); + } + public function testBuilderCommandWithHttpUrl() + { $mediaInfoCommandBuilder = new MediaInfoCommandBuilder(); $mediaInfoCommandRunner = $mediaInfoCommandBuilder->buildMediaInfoCommandRunner('http://example.org/'); - $equalsMediaInfoCommandRunner = new MediaInfoCommandRunner('http://example.org/'); + $equalsMediaInfoCommandRunner = new MediaInfoCommandRunner(new Process( + [ + 'mediainfo', + 'http://example.org/', + '-f', + '--OUTPUT=OLDXML', + ], + null, + [ + 'MEDIAINFO_VAR_FILE_PATH' => 'http://example.org/', + 'MEDIAINFO_VAR_FULL_DISPLAY' => '-f', + 'MEDIAINFO_VAR_OUTPUT' => '--OUTPUT=OLDXML', + 'LANG' => 'en_US.UTF-8', + ] + )); + $this->assertEquals($equalsMediaInfoCommandRunner, $mediaInfoCommandRunner); } @@ -51,7 +85,22 @@ public function testBuilderCommand() $mediaInfoCommandBuilder = new MediaInfoCommandBuilder(); $mediaInfoCommandRunner = $mediaInfoCommandBuilder->buildMediaInfoCommandRunner($this->filePath); - $equalsMediaInfoCommandRunner = new MediaInfoCommandRunner($this->filePath); + $equalsMediaInfoCommandRunner = new MediaInfoCommandRunner(new Process( + [ + 'mediainfo', + $this->filePath, + '-f', + '--OUTPUT=OLDXML', + ], + null, + [ + 'MEDIAINFO_VAR_FILE_PATH' => $this->filePath, + 'MEDIAINFO_VAR_FULL_DISPLAY' => '-f', + 'MEDIAINFO_VAR_OUTPUT' => '--OUTPUT=OLDXML', + 'LANG' => 'en_US.UTF-8', + ] + )); + $this->assertEquals($equalsMediaInfoCommandRunner, $mediaInfoCommandRunner); } @@ -66,13 +115,21 @@ public function testConfiguredCommand() ] ); - $equalsMediaInfoCommandRunner = new MediaInfoCommandRunner( - $this->filePath, - '/usr/bin/local/mediainfo', - null, + $equalsMediaInfoCommandRunner = new MediaInfoCommandRunner(new Process( + [ + '/usr/bin/local/mediainfo', + $this->filePath, + '-f', + '--OUTPUT=XML', + ], null, - false - ); + [ + 'MEDIAINFO_VAR_FILE_PATH' => $this->filePath, + 'MEDIAINFO_VAR_FULL_DISPLAY' => '-f', + 'MEDIAINFO_VAR_OUTPUT' => '--OUTPUT=XML', + 'LANG' => 'en_US.UTF-8', + ] + )); $this->assertEquals($equalsMediaInfoCommandRunner, $mediaInfoCommandRunner); } diff --git a/test/Builder/MediaInfoContainerBuilderTest.php b/test/Builder/MediaInfoContainerBuilderTest.php index 40290f6..aaf2d8d 100644 --- a/test/Builder/MediaInfoContainerBuilderTest.php +++ b/test/Builder/MediaInfoContainerBuilderTest.php @@ -61,8 +61,14 @@ public function testAddTrackType() public function testAddInvalidType() { $this->expectException(UnknownTrackTypeException::class); + $this->expectExceptionMessage('Type doesn\'t exist: InvalidType'); + $mediaInfoContainerBuilder = new MediaInfoContainerBuilder(); $mediaInfoContainerBuilder->addTrackType('InvalidType', []); + + $mediaInfoContainerBuilder = new MediaInfoContainerBuilder(); + + $mediaInfoContainerBuilder->addTrackType('InvalidType', []); } public function testAddInvalidTypeOnMediaInfoContainer() diff --git a/test/Container/MediaInfoContainerTest.php b/test/Container/MediaInfoContainerTest.php index 38c8a8f..dd75446 100644 --- a/test/Container/MediaInfoContainerTest.php +++ b/test/Container/MediaInfoContainerTest.php @@ -1,5 +1,8 @@ set('Format', 'MPEG Audio'); - $general->set('Duration', '1mn 20s'); + $general->set('Duration', new Duration('1200000')); $audio = new Audio(); diff --git a/test/Exception/UnknownTrackTypeExceptionTest.php b/test/Exception/UnknownTrackTypeExceptionTest.php new file mode 100644 index 0000000..89d95b2 --- /dev/null +++ b/test/Exception/UnknownTrackTypeExceptionTest.php @@ -0,0 +1,15 @@ +assertEquals($unknownTrackTypeException->getTrackType(), 'InvalidType'); + } +} diff --git a/test/Runner/MediaInfoCommandRunnerTest.php b/test/Runner/MediaInfoCommandRunnerTest.php index 2cf52d0..0407e4c 100644 --- a/test/Runner/MediaInfoCommandRunnerTest.php +++ b/test/Runner/MediaInfoCommandRunnerTest.php @@ -4,7 +4,6 @@ use Mhor\MediaInfo\Runner\MediaInfoCommandRunner; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; use Symfony\Component\Process\Process; class MediaInfoCommandRunnerTest extends TestCase @@ -28,15 +27,6 @@ protected function setUp(): void public function testRun() { $process = $this->prophesize(Process::class); - - $process - ->setCommandLine(Argument::type('string')) - ->shouldBeCalled(); - - $process - ->setEnv(Argument::type('array')) - ->shouldBeCalled(); - $process ->run() ->shouldBeCalled() @@ -52,12 +42,7 @@ public function testRun() ->shouldBeCalled() ->willReturn(true); - $mediaInfoCommandRunner = new MediaInfoCommandRunner( - $this->filePath, - null, - ['--OUTPUT=XML', '-f'], - $process->reveal() - ); + $mediaInfoCommandRunner = new MediaInfoCommandRunner($process->reveal()); $this->assertEquals(file_get_contents($this->outputPath), $mediaInfoCommandRunner->run()); } @@ -67,15 +52,6 @@ public function testRunException() $this->expectException(\RuntimeException::class); $process = $this->prophesize(Process::class); - - $process - ->setCommandLine(Argument::type('string')) - ->shouldBeCalled(); - - $process - ->setEnv(Argument::type('array')) - ->shouldBeCalled(); - $process ->run() ->shouldBeCalled() @@ -91,12 +67,7 @@ public function testRunException() ->shouldBeCalled() ->willReturn(false); - $mediaInfoCommandRunner = new MediaInfoCommandRunner( - $this->filePath, - 'custom_mediainfo', - ['--OUTPUT=XML', '-f'], - $process->reveal() - ); + $mediaInfoCommandRunner = new MediaInfoCommandRunner($process->reveal()); $mediaInfoCommandRunner->run(); } @@ -104,15 +75,6 @@ public function testRunException() public function testRunAsync() { $process = $this->prophesize(Process::class); - - $process - ->setCommandLine(Argument::type('string')) - ->shouldBeCalled(); - - $process - ->setEnv(Argument::type('array')) - ->shouldBeCalled(); - $process ->start() ->shouldBeCalled() @@ -133,12 +95,7 @@ public function testRunAsync() ->shouldBeCalled() ->willReturn(true); - $mediaInfoCommandRunner = new MediaInfoCommandRunner( - $this->filePath, - null, - ['--OUTPUT=XML', '-f'], - $process->reveal() - ); + $mediaInfoCommandRunner = new MediaInfoCommandRunner($process->reveal()); $mediaInfoCommandRunner->start(); @@ -153,4 +110,44 @@ public function testRunAsync() $this->assertEquals(file_get_contents($this->outputPath), $output); } + + public function testRunAsyncFail() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Error'); + + $process = $this->prophesize(Process::class); + $process + ->start() + ->shouldBeCalled() + ->willReturn($process); + + $process + ->wait() + ->shouldBeCalled() + ->willReturn(true); + + $process + ->getErrorOutput() + ->shouldBeCalled() + ->willReturn('Error'); + + $process + ->isSuccessful() + ->shouldBeCalled() + ->willReturn(false); + + $mediaInfoCommandRunner = new MediaInfoCommandRunner($process->reveal()); + + $mediaInfoCommandRunner->start(); + + // do some stuff in between, count to 5 + $i = 0; + do { + $i++; + } while ($i < 5); + + // block and complete operation + $mediaInfoCommandRunner->wait(); + } }