2022-09-13 09:05:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace WerkraumMedia\ThueCat\Tests\Functional;
|
|
|
|
|
2023-11-30 13:52:23 +01:00
|
|
|
use Exception;
|
2022-09-13 09:05:47 +02:00
|
|
|
use GuzzleHttp\Handler\MockHandler;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
|
|
|
|
|
|
|
class GuzzleClientFaker
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var MockHandler
|
|
|
|
*/
|
|
|
|
private static $mockHandler;
|
|
|
|
|
|
|
|
public static function registerClient(): void
|
|
|
|
{
|
|
|
|
self::getMockHandler()->reset();
|
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler']['faker'] = function (callable $handler) {
|
|
|
|
return self::getMockHandler();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans things up, call it in tests tearDown() method.
|
|
|
|
*/
|
|
|
|
public static function tearDown(): void
|
|
|
|
{
|
|
|
|
self::getMockHandler()->reset();
|
|
|
|
unset($GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler']['faker']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new response to the stack with defaults, returning the file contents of given file.
|
|
|
|
*/
|
|
|
|
public static function appendResponseFromFile(string $fileName): void
|
|
|
|
{
|
|
|
|
$fileContent = file_get_contents($fileName);
|
|
|
|
if ($fileContent === false) {
|
2023-11-30 13:52:23 +01:00
|
|
|
throw new Exception('Could not load file: ' . $fileName, 1656485162);
|
2022-09-13 09:05:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self::appendResponseFromContent($fileContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function appendNotFoundResponse(): void
|
|
|
|
{
|
|
|
|
self::appendResponse(new Response(SymfonyResponse::HTTP_NOT_FOUND));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function appendResponseFromContent(string $content): void
|
|
|
|
{
|
|
|
|
self::appendResponse(new Response(
|
|
|
|
SymfonyResponse::HTTP_OK,
|
|
|
|
[],
|
|
|
|
$content
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function getMockHandler(): MockHandler
|
|
|
|
{
|
|
|
|
if (!self::$mockHandler instanceof MockHandler) {
|
|
|
|
self::$mockHandler = new MockHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$mockHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function appendResponse(Response $response): void
|
|
|
|
{
|
|
|
|
self::getMockHandler()->append($response);
|
|
|
|
}
|
|
|
|
}
|