From d02eb8760b0b43749b86662bd59a3e1c9672007a Mon Sep 17 00:00:00 2001 From: Rob Bast Date: Tue, 10 Feb 2015 12:46:11 +0100 Subject: [PATCH] strict check, testcase(s) --- res/composer-schema.json | 3 +- .../Composer/Test/Json/ComposerSchemaTest.php | 90 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tests/Composer/Test/Json/ComposerSchemaTest.php diff --git a/res/composer-schema.json b/res/composer-schema.json index 4c40bdfb2..d9a99a710 100644 --- a/res/composer-schema.json +++ b/res/composer-schema.json @@ -284,7 +284,8 @@ }, "minimum-stability": { "type": ["string"], - "description": "The minimum stability the packages must have to be install-able. Possible values are: dev, alpha, beta, RC, stable." + "description": "The minimum stability the packages must have to be install-able. Possible values are: dev, alpha, beta, RC, stable.", + "pattern": "^dev|alpha|beta|rc|RC|stable$" }, "prefer-stable": { "type": ["boolean"], diff --git a/tests/Composer/Test/Json/ComposerSchemaTest.php b/tests/Composer/Test/Json/ComposerSchemaTest.php new file mode 100644 index 000000000..1b8805f48 --- /dev/null +++ b/tests/Composer/Test/Json/ComposerSchemaTest.php @@ -0,0 +1,90 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Json; + +use JsonSchema\Validator; + +/** + * @author Rob Bast + */ +class ComposerSchemaTest extends \PHPUnit_Framework_TestCase +{ + public function testRequiredProperties() + { + $json = '{ }'; + $this->assertEquals(array( + array('property' => '', 'message' => 'the property name is required'), + array('property' => '', 'message' => 'the property description is required'), + ), $this->check($json)); + + $json = '{ "name": "vendor/package" }'; + $this->assertEquals(array( + array('property' => '', 'message' => 'the property description is required'), + ), $this->check($json)); + + $json = '{ "description": "generic description" }'; + $this->assertEquals(array( + array('property' => '', 'message' => 'the property name is required'), + ), $this->check($json)); + } + + public function testMinimumStabilityValues() + { + $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "" }'; + $this->assertEquals(array( + array( + 'property' => 'minimum-stability', + 'message' => 'does not match the regex pattern ^dev|alpha|beta|rc|RC|stable$' + ), + ), $this->check($json), 'empty string'); + + $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dummy" }'; + $this->assertEquals(array( + array( + 'property' => 'minimum-stability', + 'message' => 'does not match the regex pattern ^dev|alpha|beta|rc|RC|stable$' + ), + ), $this->check($json), 'dummy'); + + $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dev" }'; + $this->assertTrue($this->check($json), 'dev'); + + $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "alpha" }'; + $this->assertTrue($this->check($json), 'alpha'); + + $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "beta" }'; + $this->assertTrue($this->check($json), 'beta'); + + $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "rc" }'; + $this->assertTrue($this->check($json), 'rc lowercase'); + + $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "RC" }'; + $this->assertTrue($this->check($json), 'rc uppercase'); + + $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "stable" }'; + $this->assertTrue($this->check($json), 'stable'); + } + + private function check($json) + { + $schema = json_decode(file_get_contents(__DIR__ . '/../../../../res/composer-schema.json')); + $validator = new Validator(); + $validator->check(json_decode($json), $schema); + + if (!$validator->isValid()) { + return $validator->getErrors(); + } + + return true; + } +}