@@ -22,11 +22,164 @@ class AssertTraitTest extends TestCase
22
22
*
23
23
* @see https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema
24
24
*/
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
+
25
175
public function testAssertJsonMatchesSchemaSimple ()
26
176
{
27
177
$ content = json_decode (file_get_contents (Utils::getJsonPath ('assertJsonMatchesSchema_simple.json ' )));
28
178
29
- AssertTraitImpl::assertJsonMatchesSchema ($ content , Utils::getSchemaPath ('assertJsonMatchesSchema_simple.schema.json ' ));
179
+ AssertTraitImpl::assertJsonMatchesSchema (
180
+ $ content ,
181
+ Utils::getSchemaPath ('assertJsonMatchesSchema_simple.schema.json ' )
182
+ );
30
183
}
31
184
32
185
public function testAssertJsonMatchesSchema ()
@@ -61,7 +214,10 @@ public function testAssertJsonMatchesSchemaFailMessage()
61
214
try {
62
215
AssertTraitImpl::assertJsonMatchesSchema ($ content , Utils::getSchemaPath ('test.schema.json ' ));
63
216
} 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
+ );
65
221
self ::assertStringContainsString ('- Response: {"foo":"123"} ' , $ exception ->getMessage ());
66
222
}
67
223
@@ -100,7 +256,7 @@ public function testAssertJsonMatchesSchemaString()
100
256
* @dataProvider assertJsonValueEqualsProvider
101
257
*
102
258
* @param string $expression
103
- * @param mixed $value
259
+ * @param mixed $value
104
260
*/
105
261
public function testAssertJsonValueEquals (string $ expression , $ value )
106
262
{
@@ -114,10 +270,10 @@ public function testAssertWithSchemaStore()
114
270
$ obj = new AssertTraitImpl ();
115
271
$ obj ->setUp ();
116
272
117
- $ schemaStore = $ obj ->testWithSchemaStore ('foobar ' , (object ) ['type ' => 'string ' ]);
273
+ $ schemaStore = $ obj ->testWithSchemaStore ('foobar ' , (object )['type ' => 'string ' ]);
118
274
119
275
self ::assertInstanceOf ('JsonSchema\SchemaStorage ' , $ schemaStore );
120
- self ::assertEquals ($ schemaStore ->getSchema ('foobar ' ), (object ) ['type ' => 'string ' ]);
276
+ self ::assertEquals ($ schemaStore ->getSchema ('foobar ' ), (object )['type ' => 'string ' ]);
121
277
}
122
278
123
279
public function assertJsonValueEqualsProvider (): array
0 commit comments