From 647a33a57146d2d4abd4c780f301ff92c3d0addf Mon Sep 17 00:00:00 2001 From: Michael Chiocca Date: Wed, 29 May 2013 14:36:14 -0700 Subject: [PATCH 1/3] Add draft-03 tests from json-schema/JSON-Schema-Test-Suite. --- .gitignore | 1 - composer.json | 15 +++++- composer.lock | 24 ++++++++++ .../Tests/Drafts/BaseDraftTestCase.php | 48 +++++++++++++++++++ tests/JsonSchema/Tests/Drafts/Draft3Test.php | 25 ++++++++++ 5 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 composer.lock create mode 100644 tests/JsonSchema/Tests/Drafts/BaseDraftTestCase.php create mode 100644 tests/JsonSchema/Tests/Drafts/Draft3Test.php diff --git a/.gitignore b/.gitignore index 629d61a2..f7076ff9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -/composer.lock /vendor /bin !/bin/validate-json diff --git a/composer.json b/composer.json index 982f8851..5505c225 100644 --- a/composer.json +++ b/composer.json @@ -23,8 +23,21 @@ "email": "seroscho@googlemail.com" } ], + "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" diff --git a/composer.lock b/composer.lock new file mode 100644 index 00000000..32cac453 --- /dev/null +++ b/composer.lock @@ -0,0 +1,24 @@ +{ + "hash": "4443ffc1580dcdbd4883a4e898e8ee9e", + "packages": [ + { + "name": "json-schema/JSON-Schema-Test-Suite", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/json-schema/JSON-Schema-Test-Suite", + "reference": "1.1.0" + }, + "type": "library", + "installation-source": "source" + } + ], + "packages-dev": null, + "aliases": [ + + ], + "minimum-stability": "stable", + "stability-flags": [ + + ] +} diff --git a/tests/JsonSchema/Tests/Drafts/BaseDraftTestCase.php b/tests/JsonSchema/Tests/Drafts/BaseDraftTestCase.php new file mode 100644 index 00000000..d7a505ba --- /dev/null +++ b/tests/JsonSchema/Tests/Drafts/BaseDraftTestCase.php @@ -0,0 +1,48 @@ +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(); +} diff --git a/tests/JsonSchema/Tests/Drafts/Draft3Test.php b/tests/JsonSchema/Tests/Drafts/Draft3Test.php new file mode 100644 index 00000000..0c589ff6 --- /dev/null +++ b/tests/JsonSchema/Tests/Drafts/Draft3Test.php @@ -0,0 +1,25 @@ +relativeTestsRoot . '/draft3'), + realpath(__DIR__ . $this->relativeTestsRoot . '/draft3/optional') + ); + } + + protected function getSkippedTests() + { + return array( + 'ref.json', + 'refRemote.json', + 'bignum.json', + 'jsregex.json', + 'zeroTerminatedFloats.json' + ); + } +} From 67254323e120c07901f62bde99d93145244e9fe9 Mon Sep 17 00:00:00 2001 From: Michael Chiocca Date: Wed, 29 May 2013 14:46:44 -0700 Subject: [PATCH 2/3] Delete composer.lock and add it back to .gitignore. --- .gitignore | 1 + composer.lock | 24 ------------------------ 2 files changed, 1 insertion(+), 24 deletions(-) delete mode 100644 composer.lock diff --git a/.gitignore b/.gitignore index f7076ff9..629d61a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/composer.lock /vendor /bin !/bin/validate-json diff --git a/composer.lock b/composer.lock deleted file mode 100644 index 32cac453..00000000 --- a/composer.lock +++ /dev/null @@ -1,24 +0,0 @@ -{ - "hash": "4443ffc1580dcdbd4883a4e898e8ee9e", - "packages": [ - { - "name": "json-schema/JSON-Schema-Test-Suite", - "version": "1.1.0", - "source": { - "type": "git", - "url": "https://github.com/json-schema/JSON-Schema-Test-Suite", - "reference": "1.1.0" - }, - "type": "library", - "installation-source": "source" - } - ], - "packages-dev": null, - "aliases": [ - - ], - "minimum-stability": "stable", - "stability-flags": [ - - ] -} From 60a0e09bc5a3a40661c56a364acdfbf161456404 Mon Sep 17 00:00:00 2001 From: Michael Chiocca Date: Thu, 30 May 2013 03:00:04 -0700 Subject: [PATCH 3/3] Add subset of draft-04 tests from JSON-Schema-Test-Suite. --- src/JsonSchema/Constraints/Format.php | 2 ++ tests/JsonSchema/Tests/Drafts/Draft4Test.php | 36 ++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/JsonSchema/Tests/Drafts/Draft4Test.php diff --git a/src/JsonSchema/Constraints/Format.php b/src/JsonSchema/Constraints/Format.php index 9262d6bd..9bcab3eb 100644 --- a/src/JsonSchema/Constraints/Format.php +++ b/src/JsonSchema/Constraints/Format.php @@ -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"); } @@ -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"); } diff --git a/tests/JsonSchema/Tests/Drafts/Draft4Test.php b/tests/JsonSchema/Tests/Drafts/Draft4Test.php new file mode 100644 index 00000000..e51e06a5 --- /dev/null +++ b/tests/JsonSchema/Tests/Drafts/Draft4Test.php @@ -0,0 +1,36 @@ +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' + ); + } + +} \ No newline at end of file