Skip to content

Commit

Permalink
Make compatible tests with Composer 2.3, 2.4 and 2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Mar 28, 2023
1 parent a9ad13a commit c474322
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 129 deletions.
2 changes: 1 addition & 1 deletion Fallback/ComposerFallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions Tests/Fallback/ComposerFallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
144 changes: 144 additions & 0 deletions Tests/Fixtures/Util/AbstractProcessExecutorMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

/*
* This file is part of the Foxy package.
*
* (c) François Pluchino <[email protected]>
*
* 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 <[email protected]>
*/
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;
}
}
149 changes: 23 additions & 126 deletions Tests/Fixtures/Util/ProcessExecutorMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,137 +11,34 @@

namespace Foxy\Tests\Fixtures\Util;

use Composer\Composer;
use Composer\Util\ProcessExecutor;

/**
/*
* Mock of ProcessExecutor.
*
* @author François Pluchino <[email protected]>
*/
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);
}
}
}
4 changes: 2 additions & 2 deletions Tests/Fixtures/Util/ProcessExecutorMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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));
static::assertNull($executor->getExecutedReturnedCode(1));
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);
Expand Down

0 comments on commit c474322

Please sign in to comment.