Skip to content

Commit

Permalink
Merge pull request #23 from PcComponentes/feature/cache-schemas
Browse files Browse the repository at this point in the history
feat: Cache Schemas
  • Loading branch information
zoilomora authored Jul 31, 2023
2 parents 31901ca + 61c7712 commit 40016d8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
22 changes: 18 additions & 4 deletions src/Behat/MessageValidatorOpenApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 = [];
Expand Down
30 changes: 25 additions & 5 deletions src/Behat/ResponseValidatorOpenApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand All @@ -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(
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/Behat/MessageValidatorOpenApiContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 40016d8

Please sign in to comment.