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
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:
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 44
55use Behat \Gherkin \Node \PyStringNode ;
66
7+ use Behat \Mink \Exception \ExpectationException ;
78use Sanpi \Behatch \Json \Json ;
89use Sanpi \Behatch \Json \JsonSchema ;
910use 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 */
Original file line number Diff line number Diff line change 66
77class Json
88{
9- private $ content ;
9+ protected $ content ;
1010
1111 public function __construct ($ content )
1212 {
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments