Skip to content

Commit

Permalink
Update symfony/process component call
Browse files Browse the repository at this point in the history
  • Loading branch information
mhor committed Mar 29, 2020
1 parent e24140b commit 62e02f0
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 148 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 38 additions & 4 deletions src/Builder/MediaInfoCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Mhor\MediaInfo\Runner\MediaInfoCommandRunner;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

class MediaInfoCommandBuilder
{
Expand All @@ -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();
Expand All @@ -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
);
}
}
8 changes: 1 addition & 7 deletions src/DumpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
87 changes: 7 additions & 80 deletions src/Runner/MediaInfoCommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,91 +6,22 @@

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
*/
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;
}

/**
Expand Down Expand Up @@ -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();

Expand Down
77 changes: 67 additions & 10 deletions test/Builder/MediaInfoCommandBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions test/Builder/MediaInfoContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion test/Container/MediaInfoContainerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Mhor\MediaInfo\Test\Container;

use Mhor\MediaInfo\Attribute\Duration;
use Mhor\MediaInfo\Container\MediaInfoContainer;
use Mhor\MediaInfo\Type\Audio;
use Mhor\MediaInfo\Type\General;
Expand All @@ -13,7 +16,7 @@ private function createContainer()
$general = new General();

$general->set('Format', 'MPEG Audio');
$general->set('Duration', '1mn 20s');
$general->set('Duration', new Duration('1200000'));

$audio = new Audio();

Expand Down
15 changes: 15 additions & 0 deletions test/Exception/UnknownTrackTypeExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Mhor\MediaInfo\Test\Exception;

use Mhor\MediaInfo\Exception\UnknownTrackTypeException;
use PHPUnit\Framework\TestCase;

class UnknownTrackTypeExceptionTest extends TestCase
{
public function testGetTrackType()
{
$unknownTrackTypeException = new UnknownTrackTypeException('InvalidType');
$this->assertEquals($unknownTrackTypeException->getTrackType(), 'InvalidType');
}
}
Loading

0 comments on commit 62e02f0

Please sign in to comment.