@@ -22,11 +22,164 @@ class AssertTraitTest extends TestCase
2222 *
2323 * @see https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema
2424 */
25+ public function testAssertJsonMatchesSchemaSimpleDraft6 ()
26+ {
27+ $ content = json_decode (file_get_contents (Utils::getJsonPath ('assertJsonMatchesSchema_simple_draft6.json ' )));
28+
29+ AssertTraitImpl::assertJsonMatchesSchema (
30+ $ content ,
31+ Utils::getSchemaPath ('assertJsonMatchesSchema_draft6.schema.json ' )
32+ );
33+ }
34+
35+ /**
36+ * @testWith
37+ * ["{\"created_at\": \"2016-01-01T12:00:00Z\"}", true]
38+ * ["{\"created_at\": \"2016/01/01\"}", false]
39+ */
40+ public function testAssertJsonMatchesSchemaDraft6DateTime ($ json , $ pass )
41+ {
42+ if (!$ pass ) {
43+ $ this ->expectException (ExpectationFailedException::class);
44+ }
45+
46+ $ content = json_decode ($ json );
47+
48+ AssertTraitImpl::assertJsonMatchesSchema (
49+ $ content ,
50+ Utils::getSchemaPath ('assertJsonMatchesSchema_draft6.schema.json ' )
51+ );
52+ }
53+
54+ /**
55+ * @testWith
56+ * ["{\"status\": \"active\", \"created_at\": \"2016-01-01T12:00:00Z\"}", true]
57+ * ["{\"status\": \"completed\", \"created_at\": \"2016-01-01T12:00:00Z\"}", true]
58+ * ["{\"status\": \"deleted\", \"created_at\": \"2016-01-01T12:00:00Z\"}", false]
59+ */
60+ public function testAssertJsonMatchesSchemaDraft6EnumAndNot ($ json , $ pass )
61+ {
62+ if (!$ pass ) {
63+ $ this ->expectException (ExpectationFailedException::class);
64+ }
65+
66+ $ content = json_decode ($ json );
67+
68+ AssertTraitImpl::assertJsonMatchesSchema (
69+ $ content ,
70+ Utils::getSchemaPath ('assertJsonMatchesSchema_draft6.schema.json ' )
71+ );
72+ }
73+
74+ /**
75+ * @testWith
76+ * ["{\"status\": \"active\", \"created_at\": \"2016-01-01T12:00:00Z\"}", true]
77+ * ["{\"status\": \"active\"}", false]
78+ */
79+ public function testAssertJsonMatchesSchemaDraft6Dependency ($ json , $ pass )
80+ {
81+ if (!$ pass ) {
82+ $ this ->expectException (ExpectationFailedException::class);
83+ }
84+
85+ $ content = json_decode ($ json );
86+
87+ AssertTraitImpl::assertJsonMatchesSchema (
88+ $ content ,
89+ Utils::getSchemaPath ('assertJsonMatchesSchema_draft6.schema.json ' )
90+ );
91+ }
92+
93+ /**
94+ * @testWith
95+ * ["{\"id\": 2}", true]
96+ * ["{\"id\": 1}", false]
97+ * ["{\"id\": 0}", false]
98+ */
99+ public function testAssertJsonMatchesSchemaDraft6ExclusiveMinimum ($ json , $ pass )
100+ {
101+ if (!$ pass ) {
102+ $ this ->expectException (ExpectationFailedException::class);
103+ }
104+
105+ $ content = json_decode ($ json );
106+
107+ AssertTraitImpl::assertJsonMatchesSchema (
108+ $ content ,
109+ Utils::getSchemaPath ('assertJsonMatchesSchema_draft6.schema.json ' )
110+ );
111+ }
112+
113+ /**
114+ * @testWith
115+ * ["{\"title\": \"A brief description\"}", true]
116+ * ["{\"title\": \"A description that is too long\"}", false]
117+ * ["{\"title\": \"A\"}", false]
118+ */
119+ public function testAssertJsonMatchesSchemaDraft6MaxMinLength ($ json , $ pass )
120+ {
121+ if (!$ pass ) {
122+ $ this ->expectException (ExpectationFailedException::class);
123+ }
124+
125+ $ content = json_decode ($ json );
126+
127+ AssertTraitImpl::assertJsonMatchesSchema (
128+ $ content ,
129+ Utils::getSchemaPath ('assertJsonMatchesSchema_draft6.schema.json ' )
130+ );
131+ }
132+
133+ /**
134+ * @testWith
135+ * ["{\"invalid_name\": \"value\"}", false]
136+ */
137+ public function testAssertJsonMatchesSchemaDraft6AdditionalProperties ($ json , $ pass )
138+ {
139+ if (!$ pass ) {
140+ $ this ->expectException (ExpectationFailedException::class);
141+ }
142+
143+ $ content = json_decode ($ json );
144+
145+ AssertTraitImpl::assertJsonMatchesSchema (
146+ $ content ,
147+ Utils::getSchemaPath ('assertJsonMatchesSchema_draft6.schema.json ' )
148+ );
149+ }
150+
151+ /**
152+ * @testWith
153+ * ["{\"status\": \"completed\", \"completed_at\": \"2020-01-01T12:00:00Z\", \"created_at\": \"2020-01-01T12:00:00Z\"}", true]
154+ * ["{\"status\": \"completed\", \"created_at\": \"2020-01-01T12:00:00Z\"}", false]
155+ * ["{\"status\": \"pending\", \"expected_completion\": \"2020-01-01T12:00:00Z\", \"created_at\": \"2020-01-01T12:00:00Z\"}", true]
156+ * ["{\"status\": \"pending\", \"created_at\": \"2020-01-01T12:00:00Z\"}", false]
157+ * ["{\"status\": \"active\", \"created_at\": \"2020-01-01T12:00:00Z\"}", true]
158+ */
159+ public function testAssertJsonMatchesSchemaDraft6Conditional ($ json , $ pass )
160+ {
161+ $ this ->markTestSkipped ('Conditional validation is not supported by the current implementation. ' );
162+
163+ if (!$ pass ) {
164+ $ this ->expectException (ExpectationFailedException::class);
165+ }
166+
167+ $ content = json_decode ($ json );
168+
169+ AssertTraitImpl::assertJsonMatchesSchema (
170+ $ content ,
171+ Utils::getSchemaPath ('assertJsonMatchesSchema_draft6.schema.json ' )
172+ );
173+ }
174+
25175 public function testAssertJsonMatchesSchemaSimple ()
26176 {
27177 $ content = json_decode (file_get_contents (Utils::getJsonPath ('assertJsonMatchesSchema_simple.json ' )));
28178
29- AssertTraitImpl::assertJsonMatchesSchema ($ content , Utils::getSchemaPath ('assertJsonMatchesSchema_simple.schema.json ' ));
179+ AssertTraitImpl::assertJsonMatchesSchema (
180+ $ content ,
181+ Utils::getSchemaPath ('assertJsonMatchesSchema_simple.schema.json ' )
182+ );
30183 }
31184
32185 public function testAssertJsonMatchesSchema ()
@@ -61,7 +214,10 @@ public function testAssertJsonMatchesSchemaFailMessage()
61214 try {
62215 AssertTraitImpl::assertJsonMatchesSchema ($ content , Utils::getSchemaPath ('test.schema.json ' ));
63216 } catch (ExpectationFailedException $ exception ) {
64- self ::assertStringContainsString ('- Property: foo, Constraint: type, Message: String value found, but an integer is required ' , $ exception ->getMessage ());
217+ self ::assertStringContainsString (
218+ '- Property: foo, Constraint: type, Message: String value found, but an integer is required ' ,
219+ $ exception ->getMessage ()
220+ );
65221 self ::assertStringContainsString ('- Response: {"foo":"123"} ' , $ exception ->getMessage ());
66222 }
67223
@@ -111,7 +267,7 @@ public function testAssertJsonValueEquals(string $expression, $value)
111267
112268 public function testAssertWithSchemaStore ()
113269 {
114- $ obj = new AssertTraitImpl ();
270+ $ obj = new AssertTraitImpl (' testAssertWithSchemaStore ' );
115271 $ obj ->setUp ();
116272
117273 $ schemaStore = $ obj ->testWithSchemaStore ('foobar ' , (object ) ['type ' => 'string ' ]);
@@ -120,7 +276,7 @@ public function testAssertWithSchemaStore()
120276 self ::assertEquals ($ schemaStore ->getSchema ('foobar ' ), (object ) ['type ' => 'string ' ]);
121277 }
122278
123- public function assertJsonValueEqualsProvider (): array
279+ public static function assertJsonValueEqualsProvider (): array
124280 {
125281 return [
126282 ['foo ' , '123 ' ],
@@ -144,7 +300,7 @@ public function testGetJsonObject($expected, $actual)
144300 self ::assertEquals ($ expected , AssertTraitImpl::getJsonObject ($ actual ));
145301 }
146302
147- public function jsonObjectProvider (): array
303+ public static function jsonObjectProvider (): array
148304 {
149305 return [
150306 [[], []],
0 commit comments