1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-10 17:12:51 +00:00

Get rid of all the ->at() mock invocations

This commit is contained in:
Jordi Boggiano 2021-12-09 17:09:07 +01:00
parent 095c36ecf8
commit ffd62795bc
No known key found for this signature in database
GPG key ID: 7BBD42C429EC80BC
56 changed files with 746 additions and 921 deletions

View file

@ -14,6 +14,7 @@ namespace Composer\Test\Mock;
use Composer\Util\ProcessExecutor;
use Composer\Util\Platform;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\AssertionFailedError;
use Symfony\Component\Process\Process;
@ -25,9 +26,9 @@ use React\Promise\Promise;
class ProcessExecutorMock extends ProcessExecutor
{
/**
* @var array<array{cmd: string, return: int, stdout: string, stderr: string, callback: ?callable}>
* @var array<array{cmd: string, return: int, stdout: string, stderr: string, callback: ?callable}>|null
*/
private $expectations = array();
private $expectations = null;
/**
* @var bool
*/
@ -64,10 +65,14 @@ class ProcessExecutorMock extends ProcessExecutor
$this->defaultHandler = array_merge($this->defaultHandler, $defaultHandler);
}
/** @return void */
public function assertComplete(TestCase $testCase)
public function assertComplete(): void
{
if ($this->expectations) {
// this was not configured to expect anything, so no need to react here
if (!is_array($this->expectations)) {
return;
}
if (count($this->expectations) > 0) {
$expectations = array_map(function ($expect) {
return $expect['cmd'];
}, $this->expectations);
@ -78,7 +83,8 @@ class ProcessExecutorMock extends ProcessExecutor
);
}
$testCase->assertTrue(true);
// dummy assertion to ensure the test is not marked as having no assertions
Assert::assertTrue(true);
}
public function execute($command, &$output = null, $cwd = null)
@ -115,7 +121,7 @@ class ProcessExecutorMock extends ProcessExecutor
$this->log[] = $command;
if ($this->expectations && $command === $this->expectations[0]['cmd']) {
if (is_array($this->expectations) && count($this->expectations) > 0 && $command === $this->expectations[0]['cmd']) {
$expect = array_shift($this->expectations);
$stdout = $expect['stdout'];
$stderr = $expect['stderr'];
@ -130,7 +136,7 @@ class ProcessExecutorMock extends ProcessExecutor
} else {
throw new AssertionFailedError(
'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.').PHP_EOL.
(is_array($this->expectations) && count($this->expectations) > 0 ? 'Expected "'.$this->expectations[0]['cmd'].'" at this point.' : 'Expected no more calls at this point.').PHP_EOL.
'Received calls:'.PHP_EOL.implode(PHP_EOL, array_slice($this->log, 0, -1))
);
}