Skip to content

Fix UriRetriever test #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/JsonSchema/Constraints/Undefined.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace JsonSchema\Constraints;

use JsonSchema\Exception\InvalidArgumentException;
use JsonSchema\Uri\UriResolver;

/**
Expand All @@ -24,10 +25,14 @@ class Undefined extends Constraint
*/
public function check($value, $schema = null, $path = null, $i = null)
{
if (!is_object($schema)) {
if (is_null($schema)) {
return;
}

if (!is_object($schema)) {
throw new InvalidArgumentException('Given schema must be an object.');
}

$i = is_null($i) ? "" : $i;
$path = $this->incrementPath($path, $i);

Expand Down
72 changes: 56 additions & 16 deletions tests/JsonSchema/Tests/Uri/UriRetrieverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace JsonSchema\Tests\Uri;

use JsonSchema\Exception\JsonDecodingException;
use JsonSchema\Validator;

class UriRetrieverTest extends \PHPUnit_Framework_TestCase
Expand All @@ -19,37 +20,76 @@ protected function setUp()
{
$this->validator = new Validator();
}

private function getRetrieverMock($returnSchema, $returnMediaType = Validator::SCHEMA_MEDIA_TYPE)
{

$jsonSchema = json_decode($returnSchema);

if (JSON_ERROR_NONE < $error = json_last_error()) {
throw new JsonDecodingException($error);
}

$retriever = $this->getMock('JsonSchema\Uri\UriRetriever', array('retrieve'));

$retriever->expects($this->at(0))
->method('retrieve')
->with($this->equalTo(null), $this->equalTo('http://some.host.at/somewhere/parent'))
->will($this->returnValue($returnSchema));
->will($this->returnValue($jsonSchema));

return $retriever;
}

/**
* @dataProvider jsonProvider
* @dataProvider jsonProvider
*/
public function testChildExtendsParent($childSchema, $parentSchema)
public function testChildExtendsParentValidTest($childSchema, $parentSchema)
{
$retrieverMock = $this->getRetrieverMock($parentSchema);

$json = '{"childProp":"infant", "parentProp":false}';
$decodedJson = json_decode($json);
$decodedJsonSchema = json_decode($childSchema);

$this->validator->setUriRetriever($retrieverMock);
$this->validator->check($decodedJson, $decodedJsonSchema);
$this->assertTrue($this->validator->isValid());
}


/**
* @dataProvider jsonProvider
*/
public function testChildExtendsParentInvalidChildTest($childSchema, $parentSchema)
{
$retrieverMock = $this->getRetrieverMock($parentSchema);

$json = '{"childProp":1, "parentProp":false}';
$decodedJson = json_decode($json);
$decodedJsonSchema = json_decode($childSchema);

$this->validator->setUriRetriever($retrieverMock);
$this->validator->check($decodedJson, $decodedJsonSchema);
$this->assertFalse($this->validator->isValid());
}

/**
* @dataProvider jsonProvider
* @dataProvider jsonProvider
*/
public function testChildExtendsParentInvalidParentTest($childSchema, $parentSchema)
{
$retrieverMock = $this->getRetrieverMock($parentSchema);

$json = '{"childProp":"infant", "parentProp":1}';
$decodedJson = json_decode($json);
$decodedJsonSchema = json_decode($childSchema);

$this->validator->setUriRetriever($retrieverMock);
$this->validator->check($decodedJson, $decodedJsonSchema);
$this->assertFalse($this->validator->isValid());
}

/**
* @dataProvider jsonProvider
*/
public function testResolveRelativeUri($childSchema, $parentSchema)
{
Expand All @@ -58,24 +98,24 @@ public function testResolveRelativeUri($childSchema, $parentSchema)
$json = '{"childProp":"infant", "parentProp":false}';
$decodedJson = json_decode($json);
$decodedJsonSchema = json_decode($childSchema);

$this->validator->setUriRetriever($retrieverMock);
$this->validator->check($decodedJson, $decodedJsonSchema);
$this->assertTrue($this->validator->isValid());
}

private static function setParentSchemaExtendsValue(&$parentSchema, $value)
{
$parentSchemaDecoded = json_decode($parentSchema, true);
$parentSchemaDecoded['extends'] = $value;
$parentSchema = json_encode($parentSchemaDecoded);
}

public function jsonProvider()
{
$childSchema = <<<EOF
{
"type":"object",
"type":"object",
"title":"child",
"extends":"http://some.host.at/somewhere/parent",
"properties":
Expand All @@ -89,7 +129,7 @@ public function jsonProvider()
EOF;
$parentSchema = <<<EOF
{
"type":"object",
"type":"object",
"title":"parent",
"properties":
{
Expand Down