From c4743223bc337b66f68ae3934ae25da963a7baad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Pluchino?= Date: Tue, 28 Mar 2023 21:15:31 +0200 Subject: [PATCH] Make compatible tests with Composer 2.3, 2.4 and 2.5 --- Fallback/ComposerFallback.php | 2 +- Tests/Fallback/ComposerFallbackTest.php | 7 + .../Util/AbstractProcessExecutorMock.php | 144 +++++++++++++++++ Tests/Fixtures/Util/ProcessExecutorMock.php | 149 +++--------------- .../Fixtures/Util/ProcessExecutorMockTest.php | 4 +- 5 files changed, 177 insertions(+), 129 deletions(-) create mode 100644 Tests/Fixtures/Util/AbstractProcessExecutorMock.php diff --git a/Fallback/ComposerFallback.php b/Fallback/ComposerFallback.php index bb74771..483c06d 100644 --- a/Fallback/ComposerFallback.php +++ b/Fallback/ComposerFallback.php @@ -143,7 +143,7 @@ protected function restoreLockData() $this->getLockValue('platform', array()), $this->getLockValue('platform-dev', array()), $this->getLockValue('aliases', array()), - $this->getLockValue('minimum-stability'), + $this->getLockValue('minimum-stability', ''), $this->getLockValue('stability-flags', array()), $this->getLockValue('prefer-stable', false), $this->getLockValue('prefer-lowest', false), diff --git a/Tests/Fallback/ComposerFallbackTest.php b/Tests/Fallback/ComposerFallbackTest.php index fa809fa..f21d7fd 100644 --- a/Tests/Fallback/ComposerFallbackTest.php +++ b/Tests/Fallback/ComposerFallbackTest.php @@ -199,6 +199,13 @@ public function testRestore(array $packages) 'prefer-stable' => true, ))); + $this->input->expects(static::any()) + ->method('getOption') + ->willReturnCallback(function ($option) { + return 'verbose' === $option ? false : null; + }) + ; + $ed = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock(); $this->composer->expects(static::any()) ->method('getEventDispatcher') diff --git a/Tests/Fixtures/Util/AbstractProcessExecutorMock.php b/Tests/Fixtures/Util/AbstractProcessExecutorMock.php new file mode 100644 index 0000000..3661d63 --- /dev/null +++ b/Tests/Fixtures/Util/AbstractProcessExecutorMock.php @@ -0,0 +1,144 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Foxy\Tests\Fixtures\Util; + +use Composer\Util\ProcessExecutor; + +/** + * Mock of ProcessExecutor. + * + * @author François Pluchino + */ +abstract class AbstractProcessExecutorMock extends ProcessExecutor +{ + /** + * @var array + */ + private $expectedValues = array(); + + /** + * @var array + */ + private $executedCommands = array(); + + /** + * @var int + */ + private $position = 0; + + public function doExecute($command, &$output = null, ?string $cwd = null): int + { + $expected = isset($this->expectedValues[$this->position]) + ? $this->expectedValues[$this->position] + : array(0, $output); + + list($returnedCode, $output) = $expected; + $this->executedCommands[] = array($command, $returnedCode, $output); + ++$this->position; + + return $returnedCode; + } + + /** + * @param int $returnedCode The returned code + * @param null $output The output + * + * @return self + */ + public function addExpectedValues($returnedCode = 0, $output = null) + { + $this->expectedValues[] = array($returnedCode, $output); + + return $this; + } + + /** + * Get the executed command. + * + * @param int $position The position of executed command + * + * @return null|string + */ + public function getExecutedCommand($position) + { + return $this->getExecutedValue($position, 0); + } + + /** + * Get the executed returned code. + * + * @param int $position The position of executed command + * + * @return null|int + */ + public function getExecutedReturnedCode($position) + { + return $this->getExecutedValue($position, 1); + } + + /** + * Get the executed command. + * + * @param int $position The position of executed command + * + * @return null|string + */ + public function getExecutedOutput($position) + { + return $this->getExecutedValue($position, 2); + } + + /** + * Get the last executed command. + * + * @return null|string + */ + public function getLastCommand() + { + return $this->getExecutedCommand(\count($this->executedCommands) - 1); + } + + /** + * Get the last executed returned code. + * + * @return null|int + */ + public function getLastReturnedCode() + { + return $this->getExecutedReturnedCode(\count($this->executedCommands) - 1); + } + + /** + * Get the last executed output. + * + * @return null|string + */ + public function getLastOutput() + { + return $this->getExecutedOutput(\count($this->executedCommands) - 1); + } + + /** + * Get the value of the executed command. + * + * @param int $position The position + * @param int $index The index of value + * + * @return null|int|string + */ + private function getExecutedValue($position, $index) + { + return isset($this->executedCommands[$position]) + ? $this->executedCommands[$position][$index] + : null; + } +} diff --git a/Tests/Fixtures/Util/ProcessExecutorMock.php b/Tests/Fixtures/Util/ProcessExecutorMock.php index 1078d98..de79a69 100644 --- a/Tests/Fixtures/Util/ProcessExecutorMock.php +++ b/Tests/Fixtures/Util/ProcessExecutorMock.php @@ -11,137 +11,34 @@ namespace Foxy\Tests\Fixtures\Util; +use Composer\Composer; use Composer\Util\ProcessExecutor; -/** +/* * Mock of ProcessExecutor. * * @author François Pluchino */ -class ProcessExecutorMock extends ProcessExecutor -{ - /** - * @var array - */ - private $expectedValues = array(); - - /** - * @var array - */ - private $executedCommands = array(); - - /** - * @var int - */ - private $position = 0; - - /** - * {@inheritdoc} - */ - public function execute($command, &$output = null, $cwd = null) - { - $expected = isset($this->expectedValues[$this->position]) - ? $this->expectedValues[$this->position] - : array(null, $output); - - list($returnedCode, $output) = $expected; - $this->executedCommands[] = array($command, $returnedCode, $output); - ++$this->position; - - return $returnedCode; - } - - /** - * @param int $returnedCode The returned code - * @param null $output The output - * - * @return self - */ - public function addExpectedValues($returnedCode = 0, $output = null) - { - $this->expectedValues[] = array($returnedCode, $output); - - return $this; - } - - /** - * Get the executed command. - * - * @param int $position The position of executed command - * - * @return null|string - */ - public function getExecutedCommand($position) - { - return $this->getExecutedValue($position, 0); - } - - /** - * Get the executed returned code. - * - * @param int $position The position of executed command - * - * @return null|int - */ - public function getExecutedReturnedCode($position) - { - return $this->getExecutedValue($position, 1); - } - - /** - * Get the executed command. - * - * @param int $position The position of executed command - * - * @return null|string - */ - public function getExecutedOutput($position) - { - return $this->getExecutedValue($position, 2); - } - - /** - * Get the last executed command. - * - * @return null|string - */ - public function getLastCommand() - { - return $this->getExecutedCommand(\count($this->executedCommands) - 1); - } - - /** - * Get the last executed returned code. - * - * @return null|int - */ - public function getLastReturnedCode() - { - return $this->getExecutedReturnedCode(\count($this->executedCommands) - 1); - } - - /** - * Get the last executed output. - * - * @return null|string - */ - public function getLastOutput() - { - return $this->getExecutedOutput(\count($this->executedCommands) - 1); - } - - /** - * Get the value of the executed command. - * - * @param int $position The position - * @param int $index The index of value - * - * @return null|int|string - */ - private function getExecutedValue($position, $index) - { - return isset($this->executedCommands[$position]) - ? $this->executedCommands[$position][$index] - : null; +if (version_compare(Composer::VERSION, '2.3.0', '<')) { + class ProcessExecutorMock extends AbstractProcessExecutorMock + { + /** + * {@inheritdoc} + */ + public function execute($command, &$output = null, $cwd = null) + { + return $this->doExecute($command, $output, $cwd); + } + } +} else { + class ProcessExecutorMock extends AbstractProcessExecutorMock + { + /** + * {@inheritdoc} + */ + public function execute($command, &$output = null, ?string $cwd = null): int + { + return $this->doExecute($command, $output, $cwd); + } } } diff --git a/Tests/Fixtures/Util/ProcessExecutorMockTest.php b/Tests/Fixtures/Util/ProcessExecutorMockTest.php index c00cd35..39b1087 100644 --- a/Tests/Fixtures/Util/ProcessExecutorMockTest.php +++ b/Tests/Fixtures/Util/ProcessExecutorMockTest.php @@ -29,7 +29,7 @@ public function testExecuteWithoutExpectedValues() $executor->execute('run', $output); static::assertSame('run', $executor->getExecutedCommand(0)); - static::assertNull($executor->getExecutedReturnedCode(0)); + static::assertEquals(0, $executor->getExecutedReturnedCode(0)); static::assertNull($executor->getExecutedOutput(0)); static::assertNull($executor->getExecutedCommand(1)); @@ -37,7 +37,7 @@ public function testExecuteWithoutExpectedValues() static::assertNull($executor->getExecutedOutput(1)); static::assertSame('run', $executor->getLastCommand()); - static::assertNull($executor->getLastReturnedCode()); + static::assertEquals(0, $executor->getLastReturnedCode()); static::assertNull($executor->getLastOutput()); static::assertNull($output);