2023-07-22 20:35:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers\Api;
|
|
|
|
|
2023-07-27 14:34:21 +02:00
|
|
|
use Engelsystem\Http\UrlGeneratorInterface;
|
2023-07-22 20:35:45 +02:00
|
|
|
use Engelsystem\Test\Unit\Controllers\ControllerTest as TestCase;
|
|
|
|
use League\OpenAPIValidation\PSR7\OperationAddress as OpenApiAddress;
|
|
|
|
use League\OpenAPIValidation\PSR7\ResponseValidator as OpenApiResponseValidator;
|
|
|
|
use League\OpenAPIValidation\PSR7\ValidatorBuilder as OpenApiValidatorBuilder;
|
2023-07-27 14:34:21 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2023-07-22 20:35:45 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
|
|
|
abstract class ApiBaseControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
protected OpenApiResponseValidator $validator;
|
2023-07-27 14:34:21 +02:00
|
|
|
protected UrlGeneratorInterface $url;
|
2023-07-22 20:35:45 +02:00
|
|
|
|
|
|
|
protected function validateApiResponse(string $path, string $method, ResponseInterface $response): void
|
|
|
|
{
|
|
|
|
$operation = new OpenApiAddress($path, $method);
|
|
|
|
$this->validator->validate($operation, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->initDatabase();
|
|
|
|
|
|
|
|
$openApiDefinition = $this->app->get('path.resources.api') . '/openapi.yml';
|
|
|
|
$this->validator = (new OpenApiValidatorBuilder())
|
|
|
|
->fromYamlFile($openApiDefinition)
|
|
|
|
->getResponseValidator();
|
2023-07-27 14:34:21 +02:00
|
|
|
|
|
|
|
/** @var UrlGeneratorInterface|MockObject $url */
|
|
|
|
$url = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
|
|
|
|
$url->expects($this->any())
|
|
|
|
->method('to')
|
|
|
|
->willReturnCallback(function (string $path, array $params): string {
|
|
|
|
$query = http_build_query($params);
|
|
|
|
return $path . ($query ? '?' . $query : '');
|
|
|
|
});
|
|
|
|
$this->url = $url;
|
2023-07-22 20:35:45 +02:00
|
|
|
}
|
|
|
|
}
|