Skip to content
This repository was archived by the owner on Apr 20, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions i18n/en.xliff.dist
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,14 @@
<source>the response should not be in XML</source>
<target></target>
</trans-unit>
<trans-unit id="the-json-should-not-be-valid-accordind-swagger-dump-schema">
<source>the JSON should not be valid according to swagger :dumpPath dump schema :schemaName</source>
<target></target>
</trans-unit>
<trans-unit id="the-json-should-be-valid-accordind-swagger-dump-schema">
<source>the JSON should be valid according to swagger :dumpPath dump schema :schemaName</source>
<target></target>
</trans-unit>
</body>
</file>
</xliff>
48 changes: 41 additions & 7 deletions src/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.");
}
Expand All @@ -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');
}
Expand Down Expand Up @@ -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");
}
Expand All @@ -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');
}

Expand All @@ -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());
Expand Down
10 changes: 10 additions & 0 deletions tests/features/json.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
56 changes: 56 additions & 0 deletions tests/fixtures/www/json/swagger.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/www/json/swaggerpartial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"stringValue": "value",
"intValue": 7
}