9
9
10
10
namespace JsonSchema \Tests \Uri ;
11
11
12
+ use JsonSchema \Exception \JsonDecodingException ;
12
13
use JsonSchema \Validator ;
13
14
14
15
class UriRetrieverTest extends \PHPUnit_Framework_TestCase
@@ -19,37 +20,76 @@ protected function setUp()
19
20
{
20
21
$ this ->validator = new Validator ();
21
22
}
22
-
23
+
23
24
private function getRetrieverMock ($ returnSchema , $ returnMediaType = Validator::SCHEMA_MEDIA_TYPE )
24
25
{
26
+
27
+ $ jsonSchema = json_decode ($ returnSchema );
28
+
29
+ if (JSON_ERROR_NONE < $ error = json_last_error ()) {
30
+ throw new JsonDecodingException ($ error );
31
+ }
32
+
25
33
$ retriever = $ this ->getMock ('JsonSchema\Uri\UriRetriever ' , array ('retrieve ' ));
26
-
34
+
27
35
$ retriever ->expects ($ this ->at (0 ))
28
36
->method ('retrieve ' )
29
37
->with ($ this ->equalTo (null ), $ this ->equalTo ('http://some.host.at/somewhere/parent ' ))
30
- ->will ($ this ->returnValue ($ returnSchema ));
31
-
38
+ ->will ($ this ->returnValue ($ jsonSchema ));
39
+
32
40
return $ retriever ;
33
41
}
34
-
42
+
35
43
/**
36
- * @dataProvider jsonProvider
44
+ * @dataProvider jsonProvider
37
45
*/
38
- public function testChildExtendsParent ($ childSchema , $ parentSchema )
46
+ public function testChildExtendsParentValidTest ($ childSchema , $ parentSchema )
39
47
{
40
48
$ retrieverMock = $ this ->getRetrieverMock ($ parentSchema );
41
-
49
+
42
50
$ json = '{"childProp":"infant", "parentProp":false} ' ;
43
51
$ decodedJson = json_decode ($ json );
44
52
$ decodedJsonSchema = json_decode ($ childSchema );
45
-
53
+
46
54
$ this ->validator ->setUriRetriever ($ retrieverMock );
47
55
$ this ->validator ->check ($ decodedJson , $ decodedJsonSchema );
48
56
$ this ->assertTrue ($ this ->validator ->isValid ());
49
57
}
50
-
58
+
59
+ /**
60
+ * @dataProvider jsonProvider
61
+ */
62
+ public function testChildExtendsParentInvalidChildTest ($ childSchema , $ parentSchema )
63
+ {
64
+ $ retrieverMock = $ this ->getRetrieverMock ($ parentSchema );
65
+
66
+ $ json = '{"childProp":1, "parentProp":false} ' ;
67
+ $ decodedJson = json_decode ($ json );
68
+ $ decodedJsonSchema = json_decode ($ childSchema );
69
+
70
+ $ this ->validator ->setUriRetriever ($ retrieverMock );
71
+ $ this ->validator ->check ($ decodedJson , $ decodedJsonSchema );
72
+ $ this ->assertFalse ($ this ->validator ->isValid ());
73
+ }
74
+
51
75
/**
52
- * @dataProvider jsonProvider
76
+ * @dataProvider jsonProvider
77
+ */
78
+ public function testChildExtendsParentInvalidParentTest ($ childSchema , $ parentSchema )
79
+ {
80
+ $ retrieverMock = $ this ->getRetrieverMock ($ parentSchema );
81
+
82
+ $ json = '{"childProp":"infant", "parentProp":1} ' ;
83
+ $ decodedJson = json_decode ($ json );
84
+ $ decodedJsonSchema = json_decode ($ childSchema );
85
+
86
+ $ this ->validator ->setUriRetriever ($ retrieverMock );
87
+ $ this ->validator ->check ($ decodedJson , $ decodedJsonSchema );
88
+ $ this ->assertFalse ($ this ->validator ->isValid ());
89
+ }
90
+
91
+ /**
92
+ * @dataProvider jsonProvider
53
93
*/
54
94
public function testResolveRelativeUri ($ childSchema , $ parentSchema )
55
95
{
@@ -58,24 +98,24 @@ public function testResolveRelativeUri($childSchema, $parentSchema)
58
98
$ json = '{"childProp":"infant", "parentProp":false} ' ;
59
99
$ decodedJson = json_decode ($ json );
60
100
$ decodedJsonSchema = json_decode ($ childSchema );
61
-
101
+
62
102
$ this ->validator ->setUriRetriever ($ retrieverMock );
63
103
$ this ->validator ->check ($ decodedJson , $ decodedJsonSchema );
64
104
$ this ->assertTrue ($ this ->validator ->isValid ());
65
105
}
66
-
106
+
67
107
private static function setParentSchemaExtendsValue (&$ parentSchema , $ value )
68
108
{
69
109
$ parentSchemaDecoded = json_decode ($ parentSchema , true );
70
110
$ parentSchemaDecoded ['extends ' ] = $ value ;
71
111
$ parentSchema = json_encode ($ parentSchemaDecoded );
72
112
}
73
-
113
+
74
114
public function jsonProvider ()
75
115
{
76
116
$ childSchema = <<<EOF
77
117
{
78
- "type":"object",
118
+ "type":"object",
79
119
"title":"child",
80
120
"extends":"http://some.host.at/somewhere/parent",
81
121
"properties":
@@ -89,7 +129,7 @@ public function jsonProvider()
89
129
EOF ;
90
130
$ parentSchema = <<<EOF
91
131
{
92
- "type":"object",
132
+ "type":"object",
93
133
"title":"parent",
94
134
"properties":
95
135
{
0 commit comments