From ebe5d85a037a69767f0a94229d6635bd03725b5e Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 17 Feb 2020 07:43:00 +0100 Subject: [PATCH] Support openapi path variables for automatic response content detection --- src/Behat/ResponseValidatorOpenApiContext.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/Behat/ResponseValidatorOpenApiContext.php b/src/Behat/ResponseValidatorOpenApiContext.php index 41df33e..b63dca4 100644 --- a/src/Behat/ResponseValidatorOpenApiContext.php +++ b/src/Behat/ResponseValidatorOpenApiContext.php @@ -45,14 +45,13 @@ 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(); @@ -60,7 +59,7 @@ public function theResponseShouldBeValidAccordingToOpenApi($dumpPath): void $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))); } @@ -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();