-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add response validator with openapi context
- Loading branch information
1 parent
34fac20
commit c5a3011
Showing
7 changed files
with
136 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Pccomponentes\OpenApiMessagingContext\Behat; | ||
|
||
use Behat\Behat\Context\Context; | ||
use Behat\Behat\Hook\Scope\BeforeScenarioScope; | ||
use Behat\MinkExtension\Context\MinkContext; | ||
use Pccomponentes\OpenApiMessagingContext\OpenApi\JsonSchema; | ||
use Pccomponentes\OpenApiMessagingContext\OpenApi\OpenApiSchemaParser; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
final class ResponseValidatorOpenApiContext implements Context | ||
{ | ||
/** @var MinkContext */ | ||
private $minkContext; | ||
private $rootPath; | ||
|
||
public function __construct(string $rootPath) | ||
{ | ||
$this->rootPath = $rootPath; | ||
} | ||
/** | ||
* @BeforeScenario | ||
*/ | ||
public function bootstrapEnvironment(BeforeScenarioScope $scope): void | ||
{ | ||
$this->minkContext = $scope->getEnvironment()->getContext(MinkContext::class); | ||
} | ||
|
||
/** | ||
* @Then the JSON response should be valid according to OpenApi :dumpPath schema :schema | ||
*/ | ||
public function theJsonResponseShouldBeValidAccordingToOpenApiSchema($dumpPath, $schema): void | ||
{ | ||
$path = realpath($this->rootPath . '/' . $dumpPath); | ||
$this->checkSchemaFile($path); | ||
|
||
$responseJson = $this->minkContext->getSession()->getPage()->getContent(); | ||
|
||
$allSpec = Yaml::parse(file_get_contents($path)); | ||
$schemaSpec = (new OpenApiSchemaParser($allSpec))->parse($schema); | ||
|
||
$this->validate($responseJson, new JsonSchema(\json_decode(\json_encode($schemaSpec), false))); | ||
} | ||
|
||
private function checkSchemaFile($filename): void | ||
{ | ||
if (false === is_file($filename)) { | ||
throw new \RuntimeException( | ||
'The JSON schema doesn\'t exist' | ||
); | ||
} | ||
} | ||
|
||
private function validate(string $json, JsonSchema $schema): bool | ||
{ | ||
$validator = new \JsonSchema\Validator(); | ||
|
||
$resolver = new \JsonSchema\SchemaStorage(new \JsonSchema\Uri\UriRetriever(), new \JsonSchema\Uri\UriResolver()); | ||
$schema->resolve($resolver); | ||
|
||
return $schema->validate(\json_decode($json, false), $validator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Pccomponentes\OpenApiMessagingContext\OpenApi; | ||
|
||
final class OpenApiSchemaParser | ||
{ | ||
private $originalContent; | ||
|
||
public function __construct(array $originalContent) | ||
{ | ||
$this->originalContent = $originalContent; | ||
} | ||
|
||
public function parse($name): array | ||
{ | ||
$schemaSpec = $this->originalContent['components']['schemas'][$name]; | ||
if (null === $schemaSpec) { | ||
throw new \InvalidArgumentException(\sprintf('%s schema not found', $name)); | ||
} | ||
return $this->extractData($schemaSpec); | ||
} | ||
|
||
private function extractData(array $data): array | ||
{ | ||
$aux = []; | ||
foreach ($data as $key => $elem) { | ||
if ($key === '$ref') { | ||
$aux = $this->findDefinition($elem); | ||
continue; | ||
} | ||
if (\is_array($elem)) { | ||
$aux[$key] = $this->extractData($elem); | ||
continue; | ||
} | ||
|
||
$aux[$key] = $elem; | ||
} | ||
|
||
return $aux; | ||
} | ||
|
||
private function findDefinition(string $def): array | ||
{ | ||
$cleanDef = \preg_replace('/^\#\//', '', $def); | ||
$explodedDef = \explode('/', $cleanDef); | ||
$foundDef = \array_reduce($explodedDef, function ($last, $elem) { | ||
return (null === $last) ? $this->originalContent[$elem] : $last[$elem]; | ||
}); | ||
|
||
return $this->extractData(\array_key_exists('payload', $foundDef) ? $foundDef['payload'] : $foundDef); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.