Skip to content

Commit

Permalink
Support openapi path variables for automatic response content detection
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronBernabeu committed Feb 17, 2020
1 parent b32a7f2 commit ebe5d85
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/Behat/ResponseValidatorOpenApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,21 @@ public function theJsonResponseShouldBeValidAccordingToOpenApiSchema($dumpPath,
}

/**
* @Then the response should be valid according to OpenApi :dumpPath
* @Then the response should be valid according to OpenApi :dumpPath with path :openApiPath
*/
public function theResponseShouldBeValidAccordingToOpenApi($dumpPath): void
public function theResponseShouldBeValidAccordingToOpenApiWithPath(string $dumpPath, string $openApiPath): void
{
$path = realpath($this->rootPath . '/' . $dumpPath);
$this->checkSchemaFile($path);

$currentPath = $this->extractCurrentPath();
$statusCode = $this->extractStatusCode();
$method = $this->extractMethod();
$contentType = $this->extractContentType();

$responseJson = $this->minkContext->getSession()->getPage()->getContent();

$allSpec = Yaml::parse(file_get_contents($path));
$schemaSpec = (new OpenApiSchemaParser($allSpec))->fromResponse($currentPath, $method, $statusCode, $contentType);
$schemaSpec = (new OpenApiSchemaParser($allSpec))->fromResponse($openApiPath, $method, $statusCode, $contentType);

$this->validate($responseJson, new JsonSchema(\json_decode(\json_encode($schemaSpec), false)));
}
Expand Down Expand Up @@ -93,12 +92,6 @@ private function extractMethod(): string
return strtolower($method);
}

private function extractCurrentPath(): string
{
$currentUrl = $this->minkContext->getSession()->getCurrentUrl();
return parse_url($currentUrl)['path'];
}

private function extractStatusCode(): int
{
return $this->minkContext->getSession()->getStatusCode();
Expand Down

0 comments on commit ebe5d85

Please sign in to comment.