Skip to content

Add Tests from json-schema/JSON-Schema-Test-Suite GitHub Project #43

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 3 commits into from
Jun 1, 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
15 changes: 14 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@
"email": "[email protected]"
}
],
"repositories": [{
"type": "package",
"package": {
"name": "json-schema/JSON-Schema-Test-Suite",
"version": "1.1.0",
"source": {
"url": "https://github.com/json-schema/JSON-Schema-Test-Suite",
"type": "git",
"reference": "1.1.0"
}
}
}],
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"json-schema/JSON-Schema-Test-Suite": "1.1.0"
},
"require-dev": {
"phpunit/phpunit": "~3.7.0"
Expand Down
2 changes: 2 additions & 0 deletions src/JsonSchema/Constraints/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function check($element, $schema = null, $path = null, $i = null)
break;

case 'ip-address':
case 'ipv4':
if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4)) {
$this->addError($path, "Invalid IP address");
}
Expand All @@ -104,6 +105,7 @@ public function check($element, $schema = null, $path = null, $i = null)
break;

case 'host-name':
case 'hostname':
if (!$this->validateHostname($element)) {
$this->addError($path, "Invalid hostname");
}
Expand Down
48 changes: 48 additions & 0 deletions tests/JsonSchema/Tests/Drafts/BaseDraftTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace JsonSchema\Tests\Drafts;

use JsonSchema\Tests\Constraints\BaseTestCase;

abstract class BaseDraftTestCase extends BaseTestCase
{
protected $relativeTestsRoot = '/../../../../vendor/json-schema/JSON-Schema-Test-Suite/tests';

private function setUpTests($isValid)
{
$filePaths = $this->getFilePaths();
$skippedTests = $this->getSkippedTests();
$tests = array();

foreach ($filePaths as $path) {
foreach (glob($path . '/*.json') as $file) {
if (!in_array(basename($file), $skippedTests)) {
$suites = json_decode(file_get_contents($file));
foreach ($suites as $suite) {
foreach ($suite->tests as $test) {
if ($isValid === $test->valid) {
$tests[] = array(json_encode($test->data), json_encode($suite->schema));
}
}
}
}
}
}

return $tests;
}

public function getInvalidTests()
{
return $this->setUpTests(false);
}

public function getValidTests()
{
return $this->setUpTests(true);
}

protected abstract function getFilePaths();

protected abstract function getSkippedTests();
}
25 changes: 25 additions & 0 deletions tests/JsonSchema/Tests/Drafts/Draft3Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace JsonSchema\Tests\Drafts;

class Draft3Test extends BaseDraftTestCase
{
protected function getFilePaths()
{
return array(
realpath(__DIR__ . $this->relativeTestsRoot . '/draft3'),
realpath(__DIR__ . $this->relativeTestsRoot . '/draft3/optional')
);
}

protected function getSkippedTests()
{
return array(
'ref.json',
'refRemote.json',
'bignum.json',
'jsregex.json',
'zeroTerminatedFloats.json'
);
}
}
36 changes: 36 additions & 0 deletions tests/JsonSchema/Tests/Drafts/Draft4Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace JsonSchema\Tests\Drafts;

class Draft4Test extends BaseDraftTestCase
{
protected function getFilePaths()
{
return array(
realpath(__DIR__ . $this->relativeTestsRoot . '/draft4'),
realpath(__DIR__ . $this->relativeTestsRoot . '/draft4/optional')
);
}

protected function getSkippedTests()
{
return array(
// Not Yet Implemented
'allOf.json',
'anyOf.json',
'definitions.json',
'maxProperties.json',
'minProperties.json',
'multipleOf.json',
'not.json',
'oneOf.json',
// Partially Implemented
'ref.json',
'refRemote.json',
// Optional
'bignum.json',
'zeroTerminatedFloats.json'
);
}

}