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

Commit ec35992

Browse files
author
Guillaume MOREL
committed
Fix ability to test json payload against json schema
1 parent 67de9a8 commit ec35992

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

features/json.feature

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,51 @@ Feature: Testing JSONContext
7272
}
7373
"""
7474

75+
76+
Scenario: Json validation deep
77+
Given I am on "/json/booking.json"
78+
Then the JSON should be invalid according to this schema:
79+
"""
80+
{
81+
"type":"object",
82+
"$schema": "http://json-schema.org/draft-03/schema",
83+
"id": "http://jsonschema.net",
84+
"required":false,
85+
"properties":{
86+
"Booking": {
87+
"type":"object",
88+
"id": "http://jsonschema.net/Booking",
89+
"required":false
90+
},
91+
"Metadata": {
92+
"type":"object",
93+
"id": "http://jsonschema.net/Metadata",
94+
"required":false,
95+
"properties":{
96+
"First": {
97+
"type":"object",
98+
"id": "http://jsonschema.net/Metadata/First",
99+
"required":false,
100+
"properties":{
101+
"default_value": {
102+
"type":"boolean",
103+
"id": "http://jsonschema.net/Metadata/First/default_value",
104+
"required":false
105+
},
106+
"enabled": {
107+
"type":"boolean",
108+
"id": "http://jsonschema.net/Metadata/First/enabled",
109+
"required":true
110+
}
111+
}
112+
}
113+
}
114+
}
115+
}
116+
}
117+
"""
118+
119+
75120
Scenario: Json contents validation
76121
Given I am on "/json/imajson.json"
77122
Then the JSON should be equal to:

fixtures/www/json/booking.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Booking": {
3+
"id": "1",
4+
"price": "77.21"
5+
}, "Metadata":
6+
{
7+
"First": {
8+
"bad_property_name": true,
9+
"default_value": true
10+
}
11+
}
12+
}

src/Context/JsonContext.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Behat\Gherkin\Node\PyStringNode;
66

7+
use Behat\Mink\Exception\ExpectationException;
78
use Sanpi\Behatch\Json\Json;
89
use Sanpi\Behatch\Json\JsonSchema;
910
use Sanpi\Behatch\Json\JsonInspector;
@@ -158,6 +159,26 @@ public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema)
158159
);
159160
}
160161

162+
/**
163+
* @Then the JSON should be invalid according to this schema:
164+
*/
165+
public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema)
166+
{
167+
try {
168+
$isValid = $this->inspector->validate(
169+
$this->getJson(),
170+
new JsonSchema($schema)
171+
);
172+
173+
} catch (\Exception $e) {
174+
$isValid = false;
175+
}
176+
177+
if (true === $isValid) {
178+
throw new ExpectationException('Expected to receive invalid json, got valid one', $this->getSession());
179+
}
180+
}
181+
161182
/**
162183
* @Then the JSON should be valid according to the schema :filename
163184
*/

src/Json/Json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Json
88
{
9-
private $content;
9+
protected $content;
1010

1111
public function __construct($content)
1212
{

src/Json/JsonSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function resolve(RefResolver $resolver)
2828

2929
public function validate(Json $json, Validator $validator)
3030
{
31-
$validator->check($json, $this);
31+
$validator->check($json->content, $this->content);
3232

3333
if (!$validator->isValid()) {
3434
$msg = "JSON does not validate. Violations:".PHP_EOL;

0 commit comments

Comments
 (0)