From 2ab88479413253ec909a75f438960c5b30a7a323 Mon Sep 17 00:00:00 2001 From: gpenverne Date: Wed, 21 Mar 2018 21:01:07 +0100 Subject: [PATCH 1/3] Use swagger dump as json schemas source --- src/Context/JsonContext.php | 50 +++++++++++++++--- tests/features/json.feature | 12 +++++ tests/fixtures/www/json/swagger.json | 56 +++++++++++++++++++++ tests/fixtures/www/json/swaggerpartial.json | 4 ++ 4 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/www/json/swagger.json create mode 100644 tests/fixtures/www/json/swaggerpartial.json diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index bdb7185b..531e40f1 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -273,8 +273,7 @@ public function theJsonNodeShouldExist($name) try { $node = $this->inspector->evaluate($json, $name); - } - catch (\Exception $e) { + } catch (\Exception $e) { throw new \Exception("The node '$name' does not exist."); } return $node; @@ -287,7 +286,7 @@ public function theJsonNodeShouldExist($name) */ public function theJsonNodeShouldNotExist($name) { - $this->not(function () use($name) { + $this->not(function () use ($name) { return $this->theJsonNodeShouldExist($name); }, "The node '$name' exists."); } @@ -308,7 +307,7 @@ public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema) */ public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema) { - $this->not(function() use($schema) { + $this->not(function () use ($schema) { return $this->theJsonShouldBeValidAccordingToThisSchema($schema); }, 'Expected to receive invalid json, got valid one'); } @@ -336,7 +335,7 @@ public function theJsonShouldBeInvalidAccordingToTheSchema($filename) { $this->checkSchemaFile($filename); - $this->not(function () use($filename) { + $this->not(function () use ($filename) { return $this->theJsonShouldBeValidAccordingToTheSchema($filename); }, "The schema was valid"); } @@ -350,8 +349,7 @@ public function theJsonShouldBeEqualTo(PyStringNode $content) try { $expected = new Json($content); - } - catch (\Exception $e) { + } catch (\Exception $e) { throw new \Exception('The expected JSON is not a valid'); } @@ -371,6 +369,44 @@ public function printLastJsonResponse() ->encode(); } + /** + * Checks, that response JSON matches with a swagger dump + * + * @Then the JSON should be valid according to swagger :dumpPath dump schema :schemaName + */ + public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName) + { + $this->checkSchemaFile($dumpPath); + + $dumpJson = file_get_contents($dumpPath); + $schemas = json_decode($dumpJson, true); + $definition = json_encode( + $schemas['definitions'][$schemaName] + ); + $this->inspector->validate( + $this->getJson(), + new JsonSchema( + $definition + ) + ); + } + /** + * + * Checks, that response JSON not matches with a swagger dump + * + * @Then the JSON should not be valid according to swagger :dumpPath dump schema :schemaName + */ + public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName) + { + try { + $this->theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName); + } catch (\Exception $e) { + return; + } + + throw new \RuntimeException('JSON Schema matches but it should not'); + } + protected function getJson() { return new Json($this->httpCallResultPool->getResult()->getValue()); diff --git a/tests/features/json.feature b/tests/features/json.feature index cbc980a1..f30558b0 100644 --- a/tests/features/json.feature +++ b/tests/features/json.feature @@ -1,3 +1,5 @@ +@JSON + Feature: Testing JSONContext Scenario: Am I a JSON ? @@ -186,3 +188,13 @@ Feature: Testing JSONContext And the JSON node "four" should be equal to the string "foo" And the JSON node "five" should not be null And the JSON node "five" should be equal to the number 5 + + Scenario: Json validation against swagger dump file + Given I am on "/json/swaggerpartial.json" + Then the response should be in JSON + And the JSON should be valid according to swagger "tests/fixtures/www/json/swagger.json" dump schema "sample-definition" + + Scenario: Json validation against swagger dump file + Given I am on "/json/swaggerpartial.json" + Then the response should be in JSON + And the JSON should not be valid according to swagger "tests/fixtures/www/json/swagger.json" dump schema "sample-invalid-definition" diff --git a/tests/fixtures/www/json/swagger.json b/tests/fixtures/www/json/swagger.json new file mode 100644 index 00000000..9e1646fe --- /dev/null +++ b/tests/fixtures/www/json/swagger.json @@ -0,0 +1,56 @@ +{ + "swagger": "2.0", + "basePath": "\/", + "info": { + "title": "Sample API", + "version": "2.0.0", + "description": "Sample API" + }, + "paths": { + "\/a\/path": { + "get": { + "tags": [ + "sample" + ], + "operationId": "sampleId", + "produces": [ + "application\/json", + "text\/html" + ], + "summary": "Just a fixture sample endpoint", + "responses": {}, + "parameters": [] + } + } + }, + "definitions": { + "sample-invalid-definition": { + "type": "object", + "description": "", + "properties": { + "stringValue": { + "readOnly": true, + "type": "integer" + }, + "intValue": { + "readOnly": true, + "type": "integer" + } + } + }, + "sample-definition": { + "type": "object", + "description": "", + "properties": { + "stringValue": { + "readOnly": true, + "type": "string" + }, + "intValue": { + "readOnly": true, + "type": "integer" + } + } + } + } +} diff --git a/tests/fixtures/www/json/swaggerpartial.json b/tests/fixtures/www/json/swaggerpartial.json new file mode 100644 index 00000000..3004b189 --- /dev/null +++ b/tests/fixtures/www/json/swaggerpartial.json @@ -0,0 +1,4 @@ +{ + "stringValue": "value", + "intValue": 7 +} From f3468c18bc7a78a72fc8d9e65266e8b206222b41 Mon Sep 17 00:00:00 2001 From: gpenverne Date: Wed, 21 Mar 2018 22:33:15 +0100 Subject: [PATCH 2/3] Reviews --- i18n/en.xliff.dist | 8 ++++++++ src/Context/JsonContext.php | 11 ++++------- tests/features/json.feature | 2 -- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/i18n/en.xliff.dist b/i18n/en.xliff.dist index 6f595ee8..c7697030 100644 --- a/i18n/en.xliff.dist +++ b/i18n/en.xliff.dist @@ -475,6 +475,14 @@ the response should not be in XML + + the JSON should not be valid according to swagger :dumpPath dump schema :schemaName + + + + the JSON should be valid according to swagger :dumpPath dump schema :schemaName + + diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index 531e40f1..085a37aa 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -398,13 +398,10 @@ public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $sche */ public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName) { - try { - $this->theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName); - } catch (\Exception $e) { - return; - } - - throw new \RuntimeException('JSON Schema matches but it should not'); + $this->not( + [$this, 'theJsonShouldBeValidAccordingToTheSwaggerSchema'], + 'JSON Schema matches but it should not' + ); } protected function getJson() diff --git a/tests/features/json.feature b/tests/features/json.feature index f30558b0..8d619b40 100644 --- a/tests/features/json.feature +++ b/tests/features/json.feature @@ -1,5 +1,3 @@ -@JSON - Feature: Testing JSONContext Scenario: Am I a JSON ? From 7e5b2d9864485d6b2779e66e6f08fe7554c1eefa Mon Sep 17 00:00:00 2001 From: gpenverne Date: Thu, 22 Mar 2018 08:23:42 +0100 Subject: [PATCH 3/3] fix after travis fail --- src/Context/JsonContext.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index 085a37aa..62726a9a 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -398,12 +398,13 @@ public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $sche */ public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName) { - $this->not( - [$this, 'theJsonShouldBeValidAccordingToTheSwaggerSchema'], - 'JSON Schema matches but it should not' - ); + $this->not(function () use ($dumpPath, $schemaName) { + return $this->theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName); + }, 'JSON Schema matches but it should not'); } + + protected function getJson() { return new Json($this->httpCallResultPool->getResult()->getValue());