99
1010namespace JsonSchema \Tests \Uri ;
1111
12+ use JsonSchema \Exception \JsonDecodingException ;
1213use JsonSchema \Validator ;
1314
1415class UriRetrieverTest extends \PHPUnit_Framework_TestCase
@@ -19,37 +20,76 @@ protected function setUp()
1920 {
2021 $ this ->validator = new Validator ();
2122 }
22-
23+
2324 private function getRetrieverMock ($ returnSchema , $ returnMediaType = Validator::SCHEMA_MEDIA_TYPE )
2425 {
26+
27+ $ jsonSchema = json_decode ($ returnSchema );
28+
29+ if (JSON_ERROR_NONE < $ error = json_last_error ()) {
30+ throw new JsonDecodingException ($ error );
31+ }
32+
2533 $ retriever = $ this ->getMock ('JsonSchema\Uri\UriRetriever ' , array ('retrieve ' ));
26-
34+
2735 $ retriever ->expects ($ this ->at (0 ))
2836 ->method ('retrieve ' )
2937 ->with ($ this ->equalTo (null ), $ this ->equalTo ('http://some.host.at/somewhere/parent ' ))
30- ->will ($ this ->returnValue ($ returnSchema ));
31-
38+ ->will ($ this ->returnValue ($ jsonSchema ));
39+
3240 return $ retriever ;
3341 }
34-
42+
3543 /**
36- * @dataProvider jsonProvider
44+ * @dataProvider jsonProvider
3745 */
38- public function testChildExtendsParent ($ childSchema , $ parentSchema )
46+ public function testChildExtendsParentValidTest ($ childSchema , $ parentSchema )
3947 {
4048 $ retrieverMock = $ this ->getRetrieverMock ($ parentSchema );
41-
49+
4250 $ json = '{"childProp":"infant", "parentProp":false} ' ;
4351 $ decodedJson = json_decode ($ json );
4452 $ decodedJsonSchema = json_decode ($ childSchema );
45-
53+
4654 $ this ->validator ->setUriRetriever ($ retrieverMock );
4755 $ this ->validator ->check ($ decodedJson , $ decodedJsonSchema );
4856 $ this ->assertTrue ($ this ->validator ->isValid ());
4957 }
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+
5175 /**
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
5393 */
5494 public function testResolveRelativeUri ($ childSchema , $ parentSchema )
5595 {
@@ -58,24 +98,24 @@ public function testResolveRelativeUri($childSchema, $parentSchema)
5898 $ json = '{"childProp":"infant", "parentProp":false} ' ;
5999 $ decodedJson = json_decode ($ json );
60100 $ decodedJsonSchema = json_decode ($ childSchema );
61-
101+
62102 $ this ->validator ->setUriRetriever ($ retrieverMock );
63103 $ this ->validator ->check ($ decodedJson , $ decodedJsonSchema );
64104 $ this ->assertTrue ($ this ->validator ->isValid ());
65105 }
66-
106+
67107 private static function setParentSchemaExtendsValue (&$ parentSchema , $ value )
68108 {
69109 $ parentSchemaDecoded = json_decode ($ parentSchema , true );
70110 $ parentSchemaDecoded ['extends ' ] = $ value ;
71111 $ parentSchema = json_encode ($ parentSchemaDecoded );
72112 }
73-
113+
74114 public function jsonProvider ()
75115 {
76116 $ childSchema = <<<EOF
77117{
78- "type":"object",
118+ "type":"object",
79119 "title":"child",
80120 "extends":"http://some.host.at/somewhere/parent",
81121 "properties":
@@ -89,7 +129,7 @@ public function jsonProvider()
89129EOF ;
90130 $ parentSchema = <<<EOF
91131{
92- "type":"object",
132+ "type":"object",
93133 "title":"parent",
94134 "properties":
95135 {
0 commit comments