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 bdb7185b..62726a9a 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,42 @@ 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) + { + $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()); diff --git a/tests/features/json.feature b/tests/features/json.feature index cbc980a1..8d619b40 100644 --- a/tests/features/json.feature +++ b/tests/features/json.feature @@ -186,3 +186,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 +}