Skip to content

Commit c67157a

Browse files
committed
Merge pull request #54 from alexmmm/fix-uri-retriever-test
Fix UriRetriever test
2 parents 56fe099 + e208c8a commit c67157a

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

src/JsonSchema/Constraints/Undefined.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace JsonSchema\Constraints;
1111

12+
use JsonSchema\Exception\InvalidArgumentException;
1213
use JsonSchema\Uri\UriResolver;
1314

1415
/**
@@ -24,10 +25,14 @@ class Undefined extends Constraint
2425
*/
2526
public function check($value, $schema = null, $path = null, $i = null)
2627
{
27-
if (!is_object($schema)) {
28+
if (is_null($schema)) {
2829
return;
2930
}
3031

32+
if (!is_object($schema)) {
33+
throw new InvalidArgumentException('Given schema must be an object.');
34+
}
35+
3136
$i = is_null($i) ? "" : $i;
3237
$path = $this->incrementPath($path, $i);
3338

tests/JsonSchema/Tests/Uri/UriRetrieverTest.php

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace JsonSchema\Tests\Uri;
1111

12+
use JsonSchema\Exception\JsonDecodingException;
1213
use JsonSchema\Validator;
1314

1415
class 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()
89129
EOF;
90130
$parentSchema = <<<EOF
91131
{
92-
"type":"object",
132+
"type":"object",
93133
"title":"parent",
94134
"properties":
95135
{

0 commit comments

Comments
 (0)