diff --git a/composer.json b/composer.json index 65497b4..d7db7df 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "friends-of-behat/symfony-extension": "^2.4", "justinrainbow/json-schema": "^5.2", "pccomponentes/ddd": "^3.0 || ^4.0", - "symfony/cache-contracts": "^3.0", + "symfony/cache": "^5.0 || ^6.0", "symfony/messenger": "^5.0 || ^6.0", "symfony/yaml": "^5.0 || ^6.0" }, diff --git a/src/Behat/MessageValidatorOpenApiContext.php b/src/Behat/MessageValidatorOpenApiContext.php index 042c57b..f926b4b 100644 --- a/src/Behat/MessageValidatorOpenApiContext.php +++ b/src/Behat/MessageValidatorOpenApiContext.php @@ -11,11 +11,16 @@ use PcComponentes\OpenApiMessagingContext\OpenApi\JsonValidationException; use PcComponentes\OpenApiMessagingContext\OpenApi\JsonValidator; use Symfony\Component\Yaml\Yaml; +use Symfony\Contracts\Cache\CacheInterface; +use Symfony\Contracts\Cache\ItemInterface; final class MessageValidatorOpenApiContext extends ValidatorApiContext implements Context { - public function __construct(private string $rootPath, private SpyMiddleware $spyMiddleware) - { + public function __construct( + private string $rootPath, + private SpyMiddleware $spyMiddleware, + private CacheInterface $cacheAdapter, + ) { } /** @BeforeScenario */ @@ -32,8 +37,17 @@ public function theMessageShouldBeValidAccordingToTheSwagger($name, $dumpPath): $jsonMessages = $this->spyMiddleware->getMessagesFromName($name); - $allSpec = Yaml::parse(file_get_contents($path)); - $allSpec = $this->getDataExternalReferences($allSpec, $path); + $allSpec = $this->cacheAdapter->get( + \md5($path), + function (ItemInterface $item) use ($path) { + $item->expiresAfter(null); + + $allSpec = Yaml::parse(file_get_contents($path)); + + return $this->getDataExternalReferences($allSpec, $path); + }, + ); + $schema = (new AsyncApiParser($allSpec))->parse($name); $validations = []; diff --git a/src/Behat/ResponseValidatorOpenApiContext.php b/src/Behat/ResponseValidatorOpenApiContext.php index fcd74ed..3b36e03 100644 --- a/src/Behat/ResponseValidatorOpenApiContext.php +++ b/src/Behat/ResponseValidatorOpenApiContext.php @@ -9,12 +9,14 @@ use PcComponentes\OpenApiMessagingContext\OpenApi\JsonValidator; use PcComponentes\OpenApiMessagingContext\OpenApi\OpenApiSchemaParser; use Symfony\Component\Yaml\Yaml; +use Symfony\Contracts\Cache\CacheInterface; +use Symfony\Contracts\Cache\ItemInterface; abstract class ResponseValidatorOpenApiContext extends ValidatorApiContext implements Context { private const HTTP_NO_CONTENT_CODE = 204; - public function __construct(private string $rootPath) + public function __construct(private string $rootPath, private CacheInterface $cacheAdapter) { } @@ -26,8 +28,17 @@ public function theJsonResponseShouldBeValidAccordingToOpenApiSchema($dumpPath, $responseJson = $this->extractContent(); - $allSpec = Yaml::parse(file_get_contents($path)); - $allSpec = $this->getDataExternalReferences($allSpec, $path); + $allSpec = $this->cacheAdapter->get( + \md5($path), + function (ItemInterface $item) use ($path) { + $item->expiresAfter(null); + + $allSpec = Yaml::parse(file_get_contents($path)); + + return $this->getDataExternalReferences($allSpec, $path); + }, + ); + $schemaSpec = (new OpenApiSchemaParser($allSpec))->parse($schema); $validator = new JsonValidator( @@ -53,8 +64,17 @@ public function theResponseShouldBeValidAccordingToOpenApiWithPath(string $dumpP $responseJson = $this->extractContent(); - $allSpec = Yaml::parse(\file_get_contents($path)); - $allSpec = $this->getDataExternalReferences($allSpec, $path); + $allSpec = $this->cacheAdapter->get( + \md5($path), + function (ItemInterface $item) use ($path) { + $item->expiresAfter(null); + + $allSpec = Yaml::parse(file_get_contents($path)); + + return $this->getDataExternalReferences($allSpec, $path); + }, + ); + $schemaSpec = (new OpenApiSchemaParser($allSpec))->fromResponse( $openApiPath, $method, diff --git a/src/Behat/ResponseValidatorOpenApiContext/RequestHistoryResponseValidatorOpenApiContext.php b/src/Behat/ResponseValidatorOpenApiContext/RequestHistoryResponseValidatorOpenApiContext.php index 4f0f47e..ed71a78 100644 --- a/src/Behat/ResponseValidatorOpenApiContext/RequestHistoryResponseValidatorOpenApiContext.php +++ b/src/Behat/ResponseValidatorOpenApiContext/RequestHistoryResponseValidatorOpenApiContext.php @@ -5,14 +5,15 @@ use PcComponentes\OpenApiMessagingContext\Behat\ResponseValidatorOpenApiContext; use PcComponentes\OpenApiMessagingContext\Utils\RequestHistory; +use Symfony\Contracts\Cache\CacheInterface; final class RequestHistoryResponseValidatorOpenApiContext extends ResponseValidatorOpenApiContext { private const CONTENT_TYPE_RESPONSE_HEADER_KEY = 'content-type'; - public function __construct(string $rootPath, private RequestHistory $requestHistory) + public function __construct(string $rootPath, private RequestHistory $requestHistory, CacheInterface $cacheAdapter) { - parent::__construct($rootPath); + parent::__construct($rootPath, $cacheAdapter); } protected function extractMethod(): string diff --git a/tests/Behat/MessageValidatorOpenApiContextTest.php b/tests/Behat/MessageValidatorOpenApiContextTest.php index 9c94d55..24b8d63 100644 --- a/tests/Behat/MessageValidatorOpenApiContextTest.php +++ b/tests/Behat/MessageValidatorOpenApiContextTest.php @@ -14,20 +14,25 @@ use PcComponentes\OpenApiMessagingContext\OpenApi\JsonValidationException; use PcComponentes\OpenApiMessagingContext\Tests\Messaging\DomainEventFake; use PHPUnit\Framework\TestCase; +use Symfony\Component\Cache\Adapter\NullAdapter; use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Middleware\StackMiddleware; +use Symfony\Contracts\Cache\CacheInterface; class MessageValidatorOpenApiContextTest extends TestCase { private MessageValidatorOpenApiContext $messageValidatorOpenApiContext; private SpyMiddleware $spyMiddleware; + private CacheInterface $cacheAdapter; protected function setUp(): void { $this->spyMiddleware = new SpyMiddleware(); + $this->cacheAdapter = new NullAdapter(); $this->messageValidatorOpenApiContext = new MessageValidatorOpenApiContext( __DIR__, $this->spyMiddleware, + $this->cacheAdapter, ); $this->spyMiddleware->reset(); }