mirror of
https://github.com/composer/composer
synced 2025-05-09 00:22:53 +00:00
Get rid of all the ->at() mock invocations
This commit is contained in:
parent
095c36ecf8
commit
ffd62795bc
56 changed files with 746 additions and 921 deletions
|
@ -12,31 +12,120 @@
|
|||
|
||||
namespace Composer\Test\Mock;
|
||||
|
||||
use Composer\Config;
|
||||
use Composer\IO\BufferIO;
|
||||
use Composer\IO\IOInterface;
|
||||
use Composer\Util\HttpDownloader;
|
||||
use Composer\Util\Http\Response;
|
||||
use Composer\Downloader\TransportException;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\AssertionFailedError;
|
||||
|
||||
class HttpDownloaderMock extends HttpDownloader
|
||||
{
|
||||
/**
|
||||
* @var array<string, string>
|
||||
* @var array<array{url: string, options: array<mixed>|null, status: int, body: string, headers: list<string>}>|null
|
||||
*/
|
||||
protected $contentMap;
|
||||
private $expectations = null;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $strict = false;
|
||||
/**
|
||||
* @var array{status: int, body: string, headers: array<string>}
|
||||
*/
|
||||
private $defaultHandler = array('status' => 200, 'body' => '', 'headers' => []);
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $log = array();
|
||||
|
||||
public function __construct(IOInterface $io = null, Config $config = null)
|
||||
{
|
||||
if ($io === null) {
|
||||
$io = new BufferIO();
|
||||
}
|
||||
if ($config === null) {
|
||||
$config = new Config(false);
|
||||
}
|
||||
parent::__construct($io, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $contentMap associative array of locations and content
|
||||
* @param array<array{url: string, options?: array<mixed>, status?: int, body?: string, headers?: array<string>}> $expectations
|
||||
* @param bool $strict set to true if you want to provide *all* expected http requests, and not just a subset you are interested in testing
|
||||
* @param array{status?: int, body?: string, headers?: array<string>} $defaultHandler default URL handler for undefined requests if not in strict mode
|
||||
*/
|
||||
public function __construct(array $contentMap)
|
||||
public function expects(array $expectations, bool $strict = false, array $defaultHandler = array('return' => 0, 'stdout' => '', 'stderr' => '')): void
|
||||
{
|
||||
$this->contentMap = $contentMap;
|
||||
$default = ['url' => '', 'options' => null, 'status' => 200, 'body' => '', 'headers' => ['']];
|
||||
$this->expectations = array_map(function (array $expect) use ($default): array {
|
||||
if ($diff = array_diff_key(array_merge($default, $expect), $default)) {
|
||||
throw new \UnexpectedValueException('Unexpected keys in process execution step: '.implode(', ', array_keys($diff)));
|
||||
}
|
||||
|
||||
return array_merge($default, $expect);
|
||||
}, $expectations);
|
||||
$this->strict = $strict;
|
||||
$this->defaultHandler = array_merge($this->defaultHandler, $defaultHandler);
|
||||
}
|
||||
|
||||
public function get($fileUrl, $options = array())
|
||||
public function assertComplete(): void
|
||||
{
|
||||
if (!empty($this->contentMap[$fileUrl])) {
|
||||
return new Response(array('url' => $fileUrl), 200, array(), $this->contentMap[$fileUrl]);
|
||||
// this was not configured to expect anything, so no need to react here
|
||||
if (!is_array($this->expectations)) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new TransportException('The "'.$fileUrl.'" file could not be downloaded (NOT FOUND)', 404);
|
||||
if (count($this->expectations) > 0) {
|
||||
$expectations = array_map(function ($expect) {
|
||||
return $expect['url'];
|
||||
}, $this->expectations);
|
||||
throw new AssertionFailedError(
|
||||
'There are still '.count($this->expectations).' expected HTTP requests which have not been consumed:'.PHP_EOL.
|
||||
implode(PHP_EOL, $expectations).PHP_EOL.PHP_EOL.
|
||||
'Received calls:'.PHP_EOL.implode(PHP_EOL, $this->log)
|
||||
);
|
||||
}
|
||||
|
||||
// dummy assertion to ensure the test is not marked as having no assertions
|
||||
Assert::assertTrue(true);
|
||||
}
|
||||
|
||||
public function get($fileUrl, $options = array()): Response
|
||||
{
|
||||
$this->log[] = $fileUrl;
|
||||
|
||||
if (is_array($this->expectations) && count($this->expectations) > 0 && $fileUrl === $this->expectations[0]['url'] && ($this->expectations[0]['options'] === null || $options === $this->expectations[0]['options'])) {
|
||||
$expect = array_shift($this->expectations);
|
||||
|
||||
return $this->respond($fileUrl, $expect['status'], $expect['headers'], $expect['body']);
|
||||
}
|
||||
|
||||
if (!$this->strict) {
|
||||
return $this->respond($fileUrl, $this->defaultHandler['status'], $this->defaultHandler['headers'], $this->defaultHandler['body']);
|
||||
}
|
||||
|
||||
throw new AssertionFailedError(
|
||||
'Received unexpected request for "'.$fileUrl.'"'.PHP_EOL.
|
||||
(is_array($this->expectations) && count($this->expectations) > 0 ? 'Expected "'.$this->expectations[0]['url'].'" 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))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $headers
|
||||
*/
|
||||
private function respond(string $url, int $status, array $headers, string $body): Response
|
||||
{
|
||||
if ($status < 400) {
|
||||
return new Response(array('url' => $url), $status, $headers, $body);
|
||||
}
|
||||
|
||||
$e = new TransportException('The "'.$url.'" file could not be downloaded', $status);
|
||||
$e->setHeaders($headers);
|
||||
$e->setResponse($body);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue