2015-02-10 11:46:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* 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;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Rob Bast <rob.bast@gmail.com>
|
|
|
|
*/
|
2017-11-04 14:52:13 +00:00
|
|
|
class ComposerSchemaTest extends TestCase
|
2015-02-10 11:46:11 +00:00
|
|
|
{
|
2020-04-15 11:29:03 +00:00
|
|
|
public function testNamePattern()
|
|
|
|
{
|
|
|
|
$expectedError = array(
|
|
|
|
array(
|
|
|
|
'property' => 'name',
|
|
|
|
'message' => 'Does not match the regex pattern ^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$',
|
|
|
|
'constraint' => 'pattern',
|
|
|
|
'pattern' => '^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$json = '{"name": "vendor/-pack__age", "description": "description"}';
|
|
|
|
$this->assertEquals($expectedError, $this->check($json));
|
|
|
|
$json = '{"name": "Vendor/Package", "description": "description"}';
|
|
|
|
$this->assertEquals($expectedError, $this->check($json));
|
|
|
|
}
|
|
|
|
|
2015-02-10 11:46:11 +00:00
|
|
|
public function testRequiredProperties()
|
|
|
|
{
|
|
|
|
$json = '{ }';
|
2020-07-02 06:12:25 +00:00
|
|
|
$result = $this->check($json);
|
|
|
|
$this->assertContains(array('property' => 'name', 'message' => 'The property name is required', 'constraint' => 'required'), $result);
|
|
|
|
$this->assertContains(array('property' => 'description', 'message' => 'The property description is required', 'constraint' => 'required'), $result);
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package" }';
|
|
|
|
$this->assertEquals(array(
|
2016-01-09 16:52:06 +00:00
|
|
|
array('property' => 'description', 'message' => 'The property description is required', 'constraint' => 'required'),
|
2015-02-10 11:46:11 +00:00
|
|
|
), $this->check($json));
|
|
|
|
|
|
|
|
$json = '{ "description": "generic description" }';
|
|
|
|
$this->assertEquals(array(
|
2016-01-09 16:52:06 +00:00
|
|
|
array('property' => 'name', 'message' => 'The property name is required', 'constraint' => 'required'),
|
2015-02-10 11:46:11 +00:00
|
|
|
), $this->check($json));
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:58:52 +00:00
|
|
|
public function testOptionalAbandonedProperty()
|
|
|
|
{
|
2020-04-15 11:29:03 +00:00
|
|
|
$json = '{"name": "vendor/package", "description": "description", "abandoned": true}';
|
2015-11-21 07:58:52 +00:00
|
|
|
$this->assertTrue($this->check($json));
|
|
|
|
}
|
|
|
|
|
2016-11-03 12:19:20 +00:00
|
|
|
public function testRequireTypes()
|
|
|
|
{
|
2020-04-15 11:29:03 +00:00
|
|
|
$json = '{"name": "vendor/package", "description": "description", "require": {"a": ["b"]} }';
|
2016-11-03 12:19:20 +00:00
|
|
|
$this->assertEquals(array(
|
|
|
|
array('property' => 'require.a', 'message' => 'Array value found, but a string is required', 'constraint' => 'type'),
|
|
|
|
), $this->check($json));
|
|
|
|
}
|
|
|
|
|
2015-02-10 11:46:11 +00:00
|
|
|
public function testMinimumStabilityValues()
|
|
|
|
{
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "" }';
|
|
|
|
$this->assertEquals(array(
|
|
|
|
array(
|
|
|
|
'property' => 'minimum-stability',
|
2015-09-28 09:51:14 +00:00
|
|
|
'message' => 'Does not match the regex pattern ^dev|alpha|beta|rc|RC|stable$',
|
2016-01-09 16:52:06 +00:00
|
|
|
'constraint' => 'pattern',
|
|
|
|
'pattern' => '^dev|alpha|beta|rc|RC|stable$',
|
2015-02-10 11:46:11 +00:00
|
|
|
),
|
|
|
|
), $this->check($json), 'empty string');
|
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dummy" }';
|
|
|
|
$this->assertEquals(array(
|
|
|
|
array(
|
|
|
|
'property' => 'minimum-stability',
|
2015-09-28 09:51:14 +00:00
|
|
|
'message' => 'Does not match the regex pattern ^dev|alpha|beta|rc|RC|stable$',
|
2016-01-09 16:52:06 +00:00
|
|
|
'constraint' => 'pattern',
|
|
|
|
'pattern' => '^dev|alpha|beta|rc|RC|stable$',
|
2015-02-10 11:46:11 +00:00
|
|
|
),
|
|
|
|
), $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)
|
|
|
|
{
|
|
|
|
$validator = new Validator();
|
2017-01-03 10:57:35 +00:00
|
|
|
$validator->check(json_decode($json), (object) array('$ref' => 'file://' . __DIR__ . '/../../../../res/composer-schema.json'));
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
if (!$validator->isValid()) {
|
2016-09-14 16:06:37 +00:00
|
|
|
$errors = $validator->getErrors();
|
|
|
|
|
2017-05-22 06:01:51 +00:00
|
|
|
// remove justinrainbow/json-schema 3.0/5.2 props so it works with all versions
|
2016-09-14 16:06:37 +00:00
|
|
|
foreach ($errors as &$err) {
|
2020-09-11 21:51:55 +00:00
|
|
|
unset($err['pointer'], $err['context']);
|
2016-09-14 16:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
2015-02-10 11:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|