Skip to content

Commit

Permalink
Add tests to validate referenced schemas are loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
estahn committed Mar 16, 2016
1 parent 08df277 commit e46f40a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public static function assertJsonMatchesSchema($schema, $content)
$retriever = new UriRetriever();
$schema = $retriever->retrieve('file://'.realpath($schema));

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

$validator = new Validator();
$validator->check($content, $schema);
Expand Down
20 changes: 20 additions & 0 deletions tests/AssertTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ public function testAssertJsonSchemaFailMessage()
self::assertInstanceOf('PHPUnit_Framework_ExpectationFailedException', $exception);
}

/**
* Tests if referenced schemas are loaded automatically
*/
public function testAssertJsonSchemaWithRefs()
{
$content = json_decode('{"code":123, "message":"Nothing works."}');

AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('error.schema.json'), $content);
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
*/
public function testAssertJsonSchemaWithRefsFails()
{
$content = json_decode('{"code":"123", "message":"Nothing works."}');

AssertTraitImpl::assertJsonMatchesSchema(self::getSchema('error.schema.json'), $content);
}

public function testAssertJsonMatchesSchemaString()
{
$content = json_decode('{"foo":123}');
Expand Down
20 changes: 20 additions & 0 deletions tests/schemas/definitions.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"definitions" : {
"link" : {
"type" : "object",
"additionalProperties" : false,
"properties" : {
"href" : { "type" : "string" }
}
},
"error" : {
"type" : "object",
"additionalProperties" : false,
"properties" : {
"code" : { "type" : "integer" },
"message" : { "type" : "string" }
}
}
}
}
1 change: 1 addition & 0 deletions tests/schemas/error.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "$ref" : "definitions.schema.json#/definitions/error" }

0 comments on commit e46f40a

Please sign in to comment.