This repository was archived by the owner on Apr 20, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +80
-2
lines changed Expand file tree Collapse file tree 5 files changed +80
-2
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,51 @@ Feature: Testing JSONContext
72
72
}
73
73
"""
74
74
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
+
75
120
Scenario : Json contents validation
76
121
Given I am on "/json/imajson.json"
77
122
Then the JSON should be equal to:
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 4
4
5
5
use Behat \Gherkin \Node \PyStringNode ;
6
6
7
+ use Behat \Mink \Exception \ExpectationException ;
7
8
use Sanpi \Behatch \Json \Json ;
8
9
use Sanpi \Behatch \Json \JsonSchema ;
9
10
use Sanpi \Behatch \Json \JsonInspector ;
@@ -158,6 +159,26 @@ public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema)
158
159
);
159
160
}
160
161
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
+
161
182
/**
162
183
* @Then the JSON should be valid according to the schema :filename
163
184
*/
Original file line number Diff line number Diff line change 6
6
7
7
class Json
8
8
{
9
- private $ content ;
9
+ protected $ content ;
10
10
11
11
public function __construct ($ content )
12
12
{
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ public function resolve(RefResolver $resolver)
28
28
29
29
public function validate (Json $ json , Validator $ validator )
30
30
{
31
- $ validator ->check ($ json , $ this );
31
+ $ validator ->check ($ json-> content , $ this -> content );
32
32
33
33
if (!$ validator ->isValid ()) {
34
34
$ msg = "JSON does not validate. Violations: " .PHP_EOL ;
You can’t perform that action at this time.
0 commit comments