1
0
Fork 0

Introduce ProcessExecutorMock to fix deprecations and amount of mocking pain

pull/10061/head
Jordi Boggiano 2021-08-18 17:12:41 +02:00
parent 5062619046
commit 6aa2d15868
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
3 changed files with 229 additions and 78 deletions

View File

@ -371,6 +371,9 @@ class ProcessExecutor
return $this->errorOutput; return $this->errorOutput;
} }
/**
* @private
*/
public function outputHandler($type, $buffer) public function outputHandler($type, $buffer)
{ {
if ($this->captureOutput) { if ($this->captureOutput) {

View File

@ -17,6 +17,8 @@ use Composer\Config;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Util\Platform; use Composer\Util\Platform;
use Composer\Test\Mock\ProcessExecutorMock;
use DateTime;
use Prophecy\Argument; use Prophecy\Argument;
class GitDownloaderTest extends TestCase class GitDownloaderTest extends TestCase
@ -118,7 +120,11 @@ class GitDownloaderTest extends TestCase
$processExecutor->expects($this->at(1)) $processExecutor->expects($this->at(1))
->method('execute') ->method('execute')
->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0)); ->will($this->returnCallback(function ($cmd, &$output) {
$output = '';
return 0;
}));
$processExecutor->expects($this->at(2)) $processExecutor->expects($this->at(2))
->method('execute') ->method('execute')
@ -189,7 +195,11 @@ class GitDownloaderTest extends TestCase
$processExecutor->expects($this->at(4)) $processExecutor->expects($this->at(4))
->method('execute') ->method('execute')
->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0)); ->will($this->returnCallback(function ($cmd, &$output, $path) {
$output = '';
return 0;
}));
$processExecutor->expects($this->at(5)) $processExecutor->expects($this->at(5))
->method('execute') ->method('execute')
@ -253,7 +263,11 @@ class GitDownloaderTest extends TestCase
$processExecutor->expects($this->at(5)) $processExecutor->expects($this->at(5))
->method('execute') ->method('execute')
->with($this->equalTo('git branch -r')) ->with($this->equalTo('git branch -r'))
->will($this->returnValue(0)); ->will($this->returnCallback(function ($cmd, &$output, $path) {
$output = '';
return 0;
}));
$processExecutor->expects($this->at(6)) $processExecutor->expects($this->at(6))
->method('execute') ->method('execute')
@ -297,37 +311,29 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getPrettyVersion') ->method('getPrettyVersion')
->will($this->returnValue('1.0.0')); ->will($this->returnValue('1.0.0'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->setMethods(array('execute'))->getMock();
$expectedGitCommand = $this->winCompat("git clone --no-checkout -- '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer -- '{$url}' && git fetch composer && git remote set-url origin -- '{$url}' && git remote set-url composer -- '{$url}'"); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') $this->winCompat("git clone --no-checkout -- '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer -- '{$url}' && git fetch composer && git remote set-url origin -- '{$url}' && git remote set-url composer -- '{$url}'"),
->with($this->equalTo($expectedGitCommand)) $this->winCompat("git remote set-url --push origin -- '{$pushUrl}'"),
->will($this->returnValue(0)); 'git branch -r',
$this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"),
$expectedGitCommand = $this->winCompat("git remote set-url --push origin -- '{$pushUrl}'"); ), true);
$processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0));
$processExecutor->expects($this->exactly(4))
->method('execute')
->will($this->returnValue(0));
$config = new Config(); $config = new Config();
$config->merge(array('config' => array('github-protocols' => $protocols))); $config->merge(array('config' => array('github-protocols' => $protocols)));
$downloader = $this->getDownloaderMock(null, $config, $processExecutor); $downloader = $this->getDownloaderMock(null, $config, $process);
$downloader->download($packageMock, 'composerPath'); $downloader->download($packageMock, 'composerPath');
$downloader->prepare('install', $packageMock, 'composerPath'); $downloader->prepare('install', $packageMock, 'composerPath');
$downloader->install($packageMock, 'composerPath'); $downloader->install($packageMock, 'composerPath');
$downloader->cleanup('install', $packageMock, 'composerPath'); $downloader->cleanup('install', $packageMock, 'composerPath');
$process->assertComplete($this);
} }
public function testDownloadThrowsRuntimeExceptionIfGitCommandFails() public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
{ {
$expectedGitCommand = $this->winCompat("git clone --no-checkout -- 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer -- 'https://example.com/composer/composer' && git fetch composer && git remote set-url origin -- 'https://example.com/composer/composer' && git remote set-url composer -- 'https://example.com/composer/composer'");
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getSourceReference') ->method('getSourceReference')
@ -335,19 +341,31 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getSourceUrls') ->method('getSourceUrls')
->will($this->returnValue(array('https://example.com/composer/composer'))); ->will($this->returnValue(array('https://example.com/composer/composer')));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->setMethods(array('execute'))->getMock(); $packageMock->expects($this->any())
$processExecutor->expects($this->at(0)) ->method('getSourceUrl')
->method('execute') ->will($this->returnValue('https://example.com/composer/composer'));
->with($this->equalTo($expectedGitCommand)) $packageMock->expects($this->any())
->will($this->returnValue(1)); ->method('getPrettyVersion')
->will($this->returnValue('1.0.0'));
$process = new ProcessExecutorMock;
$process->expects(array(
array(
'cmd' => $this->winCompat("git clone --no-checkout -- 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer -- 'https://example.com/composer/composer' && git fetch composer && git remote set-url origin -- 'https://example.com/composer/composer' && git remote set-url composer -- 'https://example.com/composer/composer'"),
'return' => 1,
),
));
// not using PHPUnit's expected exception because Prophecy exceptions extend from RuntimeException too so it is not safe // not using PHPUnit's expected exception because Prophecy exceptions extend from RuntimeException too so it is not safe
try { try {
$downloader = $this->getDownloaderMock(null, null, $processExecutor); $downloader = $this->getDownloaderMock(null, null, $process);
$downloader->download($packageMock, 'composerPath'); $downloader->download($packageMock, 'composerPath');
$downloader->prepare('install', $packageMock, 'composerPath'); $downloader->prepare('install', $packageMock, 'composerPath');
$downloader->install($packageMock, 'composerPath'); $downloader->install($packageMock, 'composerPath');
$downloader->cleanup('install', $packageMock, 'composerPath'); $downloader->cleanup('install', $packageMock, 'composerPath');
$process->assertComplete($this);
$this->fail('This test should throw'); $this->fail('This test should throw');
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
if ('RuntimeException' !== get_class($e)) { if ('RuntimeException' !== get_class($e)) {
@ -388,21 +406,29 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getVersion') ->method('getVersion')
->will($this->returnValue('1.0.0.0')); ->will($this->returnValue('1.0.0.0'));
$packageMock->expects($this->any())
->method('getPrettyVersion')
->will($this->returnValue('1.0.0'));
$process = $this->prophesize('Composer\Util\ProcessExecutor'); $process = new ProcessExecutorMock;
$process->execute($this->winCompat('git show-ref --head -d'), Argument::cetera())->willReturn(0); $process->expects(array(
$process->execute($this->winCompat('git status --porcelain --untracked-files=no'), Argument::cetera())->willReturn(0); $this->winCompat('git show-ref --head -d'),
$process->execute($this->winCompat('git remote -v'), Argument::cetera())->willReturn(0); $this->winCompat('git status --porcelain --untracked-files=no'),
$process->execute($this->winCompat('git branch -r'), Argument::cetera())->willReturn(0); $this->winCompat('git remote -v'),
$process->execute($expectedGitUpdateCommand, null, $this->winCompat($this->workingDir))->willReturn(0)->shouldBeCalled(); $expectedGitUpdateCommand,
$process->execute($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"), null, $this->winCompat($this->workingDir))->willReturn(0)->shouldBeCalled(); $this->winCompat('git branch -r'),
$this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"),
$this->winCompat('git remote -v'),
), true);
$this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
$downloader = $this->getDownloaderMock(null, new Config(), $process->reveal()); $downloader = $this->getDownloaderMock(null, new Config(), $process);
$downloader->download($packageMock, $this->workingDir, $packageMock); $downloader->download($packageMock, $this->workingDir, $packageMock);
$downloader->prepare('update', $packageMock, $this->workingDir, $packageMock); $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
$downloader->update($packageMock, $packageMock, $this->workingDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
$downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock); $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
$process->assertComplete($this);
} }
public function testUpdateWithNewRepoUrl() public function testUpdateWithNewRepoUrl()
@ -422,59 +448,38 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getVersion') ->method('getVersion')
->will($this->returnValue('1.0.0.0')); ->will($this->returnValue('1.0.0.0'));
$packageMock->expects($this->any())
->method('getPrettyVersion')
->will($this->returnValue('1.0.0'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->setMethods(array('execute'))->getMock(); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') $this->winCompat("git show-ref --head -d"),
->with($this->equalTo($this->winCompat("git show-ref --head -d"))) $this->winCompat("git status --porcelain --untracked-files=no"),
->will($this->returnValue(0)); $this->winCompat("git remote -v"),
$processExecutor->expects($this->at(1)) $this->winCompat($expectedGitUpdateCommand),
->method('execute') 'git branch -r',
->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no"))) $this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"),
->will($this->returnValue(0)); array(
$processExecutor->expects($this->at(2)) 'cmd' => $this->winCompat("git remote -v"),
->method('execute') 'stdout' => 'origin https://github.com/old/url (fetch)
->with($this->equalTo($this->winCompat("git remote -v")))
->will($this->returnValue(0));
$processExecutor->expects($this->at(3))
->method('execute')
->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
->will($this->returnValue(0));
$processExecutor->expects($this->at(4))
->method('execute')
->with($this->equalTo('git branch -r'))
->will($this->returnValue(0));
$processExecutor->expects($this->at(5))
->method('execute')
->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
->will($this->returnValue(0));
$processExecutor->expects($this->at(6))
->method('execute')
->with($this->equalTo($this->winCompat("git remote -v")))
->will($this->returnCallback(function ($cmd, &$output, $cwd) {
$output = 'origin https://github.com/old/url (fetch)
origin https://github.com/old/url (push) origin https://github.com/old/url (push)
composer https://github.com/old/url (fetch) composer https://github.com/old/url (fetch)
composer https://github.com/old/url (push) composer https://github.com/old/url (push)
'; ',
),
return 0; $this->winCompat("git remote set-url origin -- 'https://github.com/composer/composer'"),
})); $this->winCompat("git remote set-url --push origin -- 'git@github.com:composer/composer.git'"),
$processExecutor->expects($this->at(7)) ), true);
->method('execute')
->with($this->equalTo($this->winCompat("git remote set-url origin -- 'https://github.com/composer/composer'")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
->will($this->returnValue(0));
$processExecutor->expects($this->at(8))
->method('execute')
->with($this->equalTo($this->winCompat("git remote set-url --push origin -- 'git@github.com:composer/composer.git'")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
->will($this->returnValue(0));
$this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
$downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); $downloader = $this->getDownloaderMock(null, new Config(), $process);
$downloader->download($packageMock, $this->workingDir, $packageMock); $downloader->download($packageMock, $this->workingDir, $packageMock);
$downloader->prepare('update', $packageMock, $this->workingDir, $packageMock); $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
$downloader->update($packageMock, $packageMock, $this->workingDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
$downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock); $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
$process->assertComplete($this);
} }
/** /**

View File

@ -0,0 +1,143 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\Mock;
use Composer\Util\ProcessExecutor;
use Composer\Util\Platform;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
use React\Promise\Promise;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ProcessExecutorMock extends ProcessExecutor
{
private $expectations = array();
private $strict = false;
private $defaultHandler = array('return' => 0, 'stdout' => '', 'stderr' => '');
private $log = array();
/**
* @param array<string|array{cmd: string, return: int, stdout?: string, stderr?: string}> $expectations
* @param bool $strict set to true if you want to provide *all* expected commands, and not just a subset you are interested in testing
* @param array{return: int, stdout?: string, stderr?: string} $defaultHandler default command handler for undefined commands if not in strict mode
*/
public function expects(array $expectations, $strict = false, array $defaultHandler = array('return' => 0, 'stdout' => '', 'stderr' => ''))
{
$default = array('return' => 0, 'stdout' => '', 'stderr' => '');
$this->expectations = array_map(function ($expect) use ($default) {
if (is_string($expect)) {
$expect = array('cmd' => $expect);
}
return array_merge($default, $expect);
}, $expectations);
$this->strict = $strict;
$this->defaultHandler = array_merge($default, $defaultHandler);
}
public function assertComplete(TestCase $testCase)
{
if ($this->expectations) {
$expectations = array_map(function ($expect) {
return $expect['cmd'];
}, $this->expectations);
throw new \LogicException(
'There are still '.count($this->expectations).' expected process calls which have not been consumed:'.PHP_EOL.
implode(PHP_EOL, $expectations).PHP_EOL.PHP_EOL.
'Received calls:'.PHP_EOL.implode(PHP_EOL, $this->log)
);
}
$testCase->assertTrue(true);
}
public function execute($command, &$output = null, $cwd = null)
{
if (func_num_args() > 1) {
return $this->doExecute($command, $cwd, false, $output);
}
return $this->doExecute($command, $cwd, false);
}
public function executeTty($command, $cwd = null)
{
if (Platform::isTty()) {
return $this->doExecute($command, $cwd, true);
}
return $this->doExecute($command, $cwd, false);
}
private function doExecute($command, $cwd, $tty, &$output = null)
{
$this->captureOutput = func_num_args() > 3;
$this->errorOutput = '';
$callback = is_callable($output) ? $output : array($this, 'outputHandler');
if ($this->expectations && $command === $this->expectations[0]['cmd']) {
$expect = array_shift($this->expectations);
$stdout = $expect['stdout'];
$stderr = $expect['stderr'];
$return = $expect['return'];
} elseif (!$this->strict) {
$stdout = $this->defaultHandler['stdout'];
$stderr = $this->defaultHandler['stderr'];
$return = $this->defaultHandler['return'];
} else {
throw new \LogicException(
'Received unexpected command "'.$command.'" in "'.$cwd.'"'.PHP_EOL.
($this->expectations ? 'Expected "'.$this->expectations[0]['cmd'].'" at this point.' : 'Expected no more calls at this point.')
);
}
if ($stdout) {
call_user_func($callback, Process::STDOUT, $stdout);
}
if ($stderr) {
call_user_func($callback, Process::ERR, $stderr);
}
if ($this->captureOutput && !is_callable($output)) {
$output = $stdout;
}
$this->log[] = $command;
$this->errorOutput = $stderr;
return $return;
}
public function executeAsync($command, $cwd = null)
{
$resolver = function ($resolve, $reject) {
// TODO strictly speaking this should resolve with a mock Process instance here
$resolve();
};
$canceler = function () {
throw new \RuntimeException('Aborted process');
};
return new Promise($resolver, $canceler);
}
public function getErrorOutput()
{
return $this->errorOutput;
}
}