Skip to content
This repository was archived by the owner on Apr 20, 2021. It is now read-only.

Commit 2ab8847

Browse files
committed
Use swagger dump as json schemas source
1 parent b9fcd3b commit 2ab8847

File tree

4 files changed

+115
-7
lines changed

4 files changed

+115
-7
lines changed

src/Context/JsonContext.php

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ public function theJsonNodeShouldExist($name)
273273

274274
try {
275275
$node = $this->inspector->evaluate($json, $name);
276-
}
277-
catch (\Exception $e) {
276+
} catch (\Exception $e) {
278277
throw new \Exception("The node '$name' does not exist.");
279278
}
280279
return $node;
@@ -287,7 +286,7 @@ public function theJsonNodeShouldExist($name)
287286
*/
288287
public function theJsonNodeShouldNotExist($name)
289288
{
290-
$this->not(function () use($name) {
289+
$this->not(function () use ($name) {
291290
return $this->theJsonNodeShouldExist($name);
292291
}, "The node '$name' exists.");
293292
}
@@ -308,7 +307,7 @@ public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema)
308307
*/
309308
public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema)
310309
{
311-
$this->not(function() use($schema) {
310+
$this->not(function () use ($schema) {
312311
return $this->theJsonShouldBeValidAccordingToThisSchema($schema);
313312
}, 'Expected to receive invalid json, got valid one');
314313
}
@@ -336,7 +335,7 @@ public function theJsonShouldBeInvalidAccordingToTheSchema($filename)
336335
{
337336
$this->checkSchemaFile($filename);
338337

339-
$this->not(function () use($filename) {
338+
$this->not(function () use ($filename) {
340339
return $this->theJsonShouldBeValidAccordingToTheSchema($filename);
341340
}, "The schema was valid");
342341
}
@@ -350,8 +349,7 @@ public function theJsonShouldBeEqualTo(PyStringNode $content)
350349

351350
try {
352351
$expected = new Json($content);
353-
}
354-
catch (\Exception $e) {
352+
} catch (\Exception $e) {
355353
throw new \Exception('The expected JSON is not a valid');
356354
}
357355

@@ -371,6 +369,44 @@ public function printLastJsonResponse()
371369
->encode();
372370
}
373371

372+
/**
373+
* Checks, that response JSON matches with a swagger dump
374+
*
375+
* @Then the JSON should be valid according to swagger :dumpPath dump schema :schemaName
376+
*/
377+
public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName)
378+
{
379+
$this->checkSchemaFile($dumpPath);
380+
381+
$dumpJson = file_get_contents($dumpPath);
382+
$schemas = json_decode($dumpJson, true);
383+
$definition = json_encode(
384+
$schemas['definitions'][$schemaName]
385+
);
386+
$this->inspector->validate(
387+
$this->getJson(),
388+
new JsonSchema(
389+
$definition
390+
)
391+
);
392+
}
393+
/**
394+
*
395+
* Checks, that response JSON not matches with a swagger dump
396+
*
397+
* @Then the JSON should not be valid according to swagger :dumpPath dump schema :schemaName
398+
*/
399+
public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName)
400+
{
401+
try {
402+
$this->theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $schemaName);
403+
} catch (\Exception $e) {
404+
return;
405+
}
406+
407+
throw new \RuntimeException('JSON Schema matches but it should not');
408+
}
409+
374410
protected function getJson()
375411
{
376412
return new Json($this->httpCallResultPool->getResult()->getValue());

tests/features/json.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@JSON
2+
13
Feature: Testing JSONContext
24

35
Scenario: Am I a JSON ?
@@ -186,3 +188,13 @@ Feature: Testing JSONContext
186188
And the JSON node "four" should be equal to the string "foo"
187189
And the JSON node "five" should not be null
188190
And the JSON node "five" should be equal to the number 5
191+
192+
Scenario: Json validation against swagger dump file
193+
Given I am on "/json/swaggerpartial.json"
194+
Then the response should be in JSON
195+
And the JSON should be valid according to swagger "tests/fixtures/www/json/swagger.json" dump schema "sample-definition"
196+
197+
Scenario: Json validation against swagger dump file
198+
Given I am on "/json/swaggerpartial.json"
199+
Then the response should be in JSON
200+
And the JSON should not be valid according to swagger "tests/fixtures/www/json/swagger.json" dump schema "sample-invalid-definition"

tests/fixtures/www/json/swagger.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"swagger": "2.0",
3+
"basePath": "\/",
4+
"info": {
5+
"title": "Sample API",
6+
"version": "2.0.0",
7+
"description": "Sample API"
8+
},
9+
"paths": {
10+
"\/a\/path": {
11+
"get": {
12+
"tags": [
13+
"sample"
14+
],
15+
"operationId": "sampleId",
16+
"produces": [
17+
"application\/json",
18+
"text\/html"
19+
],
20+
"summary": "Just a fixture sample endpoint",
21+
"responses": {},
22+
"parameters": []
23+
}
24+
}
25+
},
26+
"definitions": {
27+
"sample-invalid-definition": {
28+
"type": "object",
29+
"description": "",
30+
"properties": {
31+
"stringValue": {
32+
"readOnly": true,
33+
"type": "integer"
34+
},
35+
"intValue": {
36+
"readOnly": true,
37+
"type": "integer"
38+
}
39+
}
40+
},
41+
"sample-definition": {
42+
"type": "object",
43+
"description": "",
44+
"properties": {
45+
"stringValue": {
46+
"readOnly": true,
47+
"type": "string"
48+
},
49+
"intValue": {
50+
"readOnly": true,
51+
"type": "integer"
52+
}
53+
}
54+
}
55+
}
56+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"stringValue": "value",
3+
"intValue": 7
4+
}

0 commit comments

Comments
 (0)