Skip to content

Commit 0c4fd72

Browse files
Added Tests for array assoc json decode
1 parent 1791dec commit 0c4fd72

File tree

2 files changed

+81
-13
lines changed

2 files changed

+81
-13
lines changed

tests/Constraints/BaseTestCase.php

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

1010
namespace JsonSchema\Tests\Constraints;
1111

12+
use JsonSchema\Constraints\Constraint;
1213
use JsonSchema\RefResolver;
1314
use JsonSchema\Uri\UriResolver;
1415
use JsonSchema\Validator;
@@ -28,8 +29,10 @@ abstract class BaseTestCase extends \PHPUnit_Framework_TestCase
2829
/**
2930
* @dataProvider getInvalidTests
3031
*/
31-
public function testInvalidCases($input, $jsonSchema, $checkMode = Validator::CHECK_MODE_NORMAL, $errors = array())
32+
public function testInvalidCases($input, $jsonSchema, $checkMode = Constraint::CHECK_MODE_NORMAL, $errors = array())
3233
{
34+
$checkMode = $checkMode === null ? Constraint::CHECK_MODE_NORMAL : $checkMode;
35+
3336
$schema = json_decode($jsonSchema);
3437
if (is_object($schema)) {
3538
$schema = $this->resolveSchema($schema);
@@ -46,10 +49,36 @@ public function testInvalidCases($input, $jsonSchema, $checkMode = Validator::CH
4649
$this->assertFalse($validator->isValid(), print_r($validator->getErrors(), true));
4750
}
4851

52+
/**
53+
* @dataProvider getInvalidForAssocTests
54+
*/
55+
public function testInvalidCasesUsingAssoc($input, $jsonSchema, $checkMode = Constraint::CHECK_MODE_TYPE_CAST, $errors = array())
56+
{
57+
$checkMode = $checkMode === null ? Constraint::CHECK_MODE_TYPE_CAST : $checkMode;
58+
if ($checkMode !== Constraint::CHECK_MODE_TYPE_CAST) {
59+
$this->markTestSkipped('Test indicates that it is not for "CHECK_MODE_TYPE_CAST"');
60+
}
61+
62+
$schema = json_decode($jsonSchema);
63+
if (is_object($schema)) {
64+
$schema = $this->resolveSchema($schema);
65+
}
66+
67+
$value = json_decode($input, true);
68+
69+
$validator = new Validator($checkMode);
70+
$validator->check($value, $schema);
71+
72+
if (array() !== $errors) {
73+
$this->assertEquals($errors, $validator->getErrors(), print_r($validator->getErrors(), true));
74+
}
75+
$this->assertFalse($validator->isValid(), print_r($validator->getErrors(), true));
76+
}
77+
4978
/**
5079
* @dataProvider getValidTests
5180
*/
52-
public function testValidCases($input, $schema, $checkMode = Validator::CHECK_MODE_NORMAL)
81+
public function testValidCases($input, $schema, $checkMode = Constraint::CHECK_MODE_NORMAL)
5382
{
5483
$schema = json_decode($schema);
5584
if (is_object($schema)) {
@@ -63,16 +92,53 @@ public function testValidCases($input, $schema, $checkMode = Validator::CHECK_MO
6392
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
6493
}
6594

95+
/**
96+
* @dataProvider getValidForAssocTests
97+
*/
98+
public function testValidCasesUsingAssoc($input, $schema, $checkMode = Constraint::CHECK_MODE_TYPE_CAST)
99+
{
100+
if ($checkMode !== Constraint::CHECK_MODE_TYPE_CAST) {
101+
$this->markTestSkipped('Test indicates that it is not for "CHECK_MODE_TYPE_CAST"');
102+
}
103+
104+
$schema = json_decode($schema);
105+
if (is_object($schema)) {
106+
$schema = $this->resolveSchema($schema);
107+
}
108+
109+
$value = json_decode($input, true);
110+
$validator = new Validator($checkMode);
111+
112+
$validator->check($value, $schema);
113+
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
114+
}
115+
66116
/**
67117
* @return array[]
68118
*/
69119
abstract public function getValidTests();
70120

121+
/**
122+
* @return array[]
123+
*/
124+
public function getValidForAssocTests()
125+
{
126+
return $this->getValidTests();
127+
}
128+
71129
/**
72130
* @return array[]
73131
*/
74132
abstract public function getInvalidTests();
75133

134+
/**
135+
* @return array[]
136+
*/
137+
public function getInvalidForAssocTests()
138+
{
139+
return $this->getInvalidTests();
140+
}
141+
76142
/**
77143
* @param object $schema
78144
* @return object

tests/Drafts/BaseDraftTestCase.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ private function setUpTests($isValid)
2121
foreach ($filePaths as $path) {
2222
foreach (glob($path . '/*.json') as $file) {
2323
$filename = basename($file);
24-
if (!in_array($filename, $skippedTests)) {
25-
$suites = json_decode(file_get_contents($file));
26-
foreach ($suites as $suite) {
27-
$suiteDescription = $suite->description;
28-
foreach ($suite->tests as $test) {
29-
$testCaseDescription = $test->description;
30-
if ($isValid === $test->valid) {
31-
$tests[
32-
$this->createDataSetPath($filename, $suiteDescription, $testCaseDescription)
33-
] = array(json_encode($test->data), json_encode($suite->schema));
34-
}
24+
if (in_array($filename, $skippedTests)) {
25+
continue;
26+
}
27+
28+
$suites = json_decode(file_get_contents($file));
29+
foreach ($suites as $suite) {
30+
$suiteDescription = $suite->description;
31+
foreach ($suite->tests as $test) {
32+
$testCaseDescription = $test->description;
33+
if ($isValid === $test->valid) {
34+
$tests[
35+
$this->createDataSetPath($filename, $suiteDescription, $testCaseDescription)
36+
] = array(json_encode($test->data), json_encode($suite->schema));
3537
}
3638
}
3739
}

0 commit comments

Comments
 (0)