Skip to content

Commit e46f40a

Browse files
committed
Add tests to validate referenced schemas are loaded
1 parent 08df277 commit e46f40a

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

src/Assert.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ public static function assertJsonMatchesSchema($schema, $content)
3939
$retriever = new UriRetriever();
4040
$schema = $retriever->retrieve('file://'.realpath($schema));
4141

42+
// Assume references are relative to the current file
43+
// Create an issue or pull request if you need more complex use cases
4244
$refResolver = new RefResolver($retriever);
43-
$refResolver->resolve($schema, 'file://'.__DIR__.'/../Resources/schemas/');
45+
$refResolver->resolve($schema, $schema->id);
4446

4547
$validator = new Validator();
4648
$validator->check($content, $schema);

tests/AssertTraitTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ public function testAssertJsonSchemaFailMessage()
5656
self::assertInstanceOf('PHPUnit_Framework_ExpectationFailedException', $exception);
5757
}
5858

59+
/**
60+
* Tests if referenced schemas are loaded automatically
61+
*/
62+
public function testAssertJsonSchemaWithRefs()
63+
{
64+
$content = json_decode('{"code":123, "message":"Nothing works."}');
65+
66+
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('error.schema.json'), $content);
67+
}
68+
69+
/**
70+
* @expectedException \PHPUnit_Framework_ExpectationFailedException
71+
*/
72+
public function testAssertJsonSchemaWithRefsFails()
73+
{
74+
$content = json_decode('{"code":"123", "message":"Nothing works."}');
75+
76+
AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('error.schema.json'), $content);
77+
}
78+
5979
public function testAssertJsonMatchesSchemaString()
6080
{
6181
$content = json_decode('{"foo":123}');

tests/schemas/definitions.schema.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema" : "http://json-schema.org/draft-04/schema#",
3+
"definitions" : {
4+
"link" : {
5+
"type" : "object",
6+
"additionalProperties" : false,
7+
"properties" : {
8+
"href" : { "type" : "string" }
9+
}
10+
},
11+
"error" : {
12+
"type" : "object",
13+
"additionalProperties" : false,
14+
"properties" : {
15+
"code" : { "type" : "integer" },
16+
"message" : { "type" : "string" }
17+
}
18+
}
19+
}
20+
}

tests/schemas/error.schema.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "$ref" : "definitions.schema.json#/definitions/error" }

0 commit comments

Comments
 (0)