Skip to content

Commit

Permalink
phpunit: migrate getMock usage to prophecy
Browse files Browse the repository at this point in the history
  • Loading branch information
mhor committed Feb 9, 2020
1 parent 2f00250 commit 52e924e
Showing 1 changed file with 64 additions and 23 deletions.
87 changes: 64 additions & 23 deletions test/Runner/MediaInfoCommandRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Mhor\MediaInfo\Runner\MediaInfoCommandRunner;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\Process\Process;

class MediaInfoCommandRunnerTest extends TestCase
{
Expand All @@ -25,24 +27,36 @@ protected function setUp(): void

public function testRun()
{
$processMock = $this->getMockBuilder('Symfony\Component\Process\Process')
->disableOriginalConstructor()
->getMock();
$process = $this->prophesize(Process::class);

$processMock->method('run')
$process
->setCommandLine(Argument::type('string'))
->shouldBeCalled();

$process
->setEnv(Argument::type('array'))
->shouldBeCalled();

$process
->run()
->shouldBeCalled()
->willReturn(1);

$processMock->method('getOutput')
$process
->getOutput()
->shouldBeCalled()
->willReturn(file_get_contents($this->outputPath));

$processMock->method('isSuccessful')
$process
->isSuccessful()
->shouldBeCalled()
->willReturn(true);

$mediaInfoCommandRunner = new MediaInfoCommandRunner(
$this->filePath,
null,
['--OUTPUT=XML', '-f'],
$processMock
$process->reveal()
);

$this->assertEquals(file_get_contents($this->outputPath), $mediaInfoCommandRunner->run());
Expand All @@ -51,52 +65,79 @@ public function testRun()
public function testRunException()
{
$this->expectException(\RuntimeException::class);
$processMock = $this->getMockBuilder('Symfony\Component\Process\Process')
->disableOriginalConstructor()
->getMock();

$processMock->method('run')
$process = $this->prophesize(Process::class);

$process
->setCommandLine(Argument::type('string'))
->shouldBeCalled();

$process
->setEnv(Argument::type('array'))
->shouldBeCalled();

$process
->run()
->shouldBeCalled()
->willReturn(0);

$processMock->method('getErrorOutput')
$process
->getErrorOutput()
->shouldBeCalled()
->willReturn('Error');

$processMock->method('isSuccessful')
$process
->isSuccessful()
->shouldBeCalled()
->willReturn(false);

$mediaInfoCommandRunner = new MediaInfoCommandRunner(
$this->filePath,
'custom_mediainfo',
['--OUTPUT=XML', '-f'],
$processMock
$process->reveal()
);

$mediaInfoCommandRunner->run();
}

public function testRunAsync()
{
$processMock = $this->getMockBuilder('Symfony\Component\Process\Process')
->disableOriginalConstructor()
->getMock();
$process = $this->prophesize(Process::class);

$process
->setCommandLine(Argument::type('string'))
->shouldBeCalled();

$process
->setEnv(Argument::type('array'))
->shouldBeCalled();

$processMock->method('start')
->willReturn($processMock);
$process
->start()
->shouldBeCalled()
->willReturn($process);

$processMock->method('wait')
$process
->wait()
->shouldBeCalled()
->willReturn(true);

$processMock->method('getOutput')
$process
->getOutput()
->shouldBeCalled()
->willReturn(file_get_contents($this->outputPath));

$processMock->method('isSuccessful')
$process
->isSuccessful()
->shouldBeCalled()
->willReturn(true);

$mediaInfoCommandRunner = new MediaInfoCommandRunner(
$this->filePath,
null,
['--OUTPUT=XML', '-f'],
$processMock
$process->reveal()
);

$mediaInfoCommandRunner->start();
Expand Down

0 comments on commit 52e924e

Please sign in to comment.