2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2023-03-21 10:17:55 +00:00
|
|
|
use Composer\Json\JsonFile;
|
2015-02-10 11:46:11 +00:00
|
|
|
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
|
|
|
{
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testNamePattern(): void
|
2020-04-15 11:29:03 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$expectedError = [
|
|
|
|
[
|
2020-04-15 11:29:03 +00:00
|
|
|
'property' => 'name',
|
2022-06-01 19:23:07 +00:00
|
|
|
'message' => 'Does not match the regex pattern ^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]|-{1,2})?[a-z0-9]+)*$',
|
2020-04-15 11:29:03 +00:00
|
|
|
'constraint' => 'pattern',
|
2022-06-01 19:23:07 +00:00
|
|
|
'pattern' => '^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]|-{1,2})?[a-z0-9]+)*$',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
];
|
2020-04-15 11:29:03 +00:00
|
|
|
|
|
|
|
$json = '{"name": "vendor/-pack__age", "description": "description"}';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals($expectedError, $this->check($json));
|
2020-04-15 11:29:03 +00:00
|
|
|
$json = '{"name": "Vendor/Package", "description": "description"}';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals($expectedError, $this->check($json));
|
2020-04-15 11:29:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testOptionalAbandonedProperty(): void
|
2015-11-21 07:58:52 +00:00
|
|
|
{
|
2020-04-15 11:29:03 +00:00
|
|
|
$json = '{"name": "vendor/package", "description": "description", "abandoned": true}';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($this->check($json));
|
2015-11-21 07:58:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testRequireTypes(): void
|
2016-11-03 12:19:20 +00:00
|
|
|
{
|
2020-04-15 11:29:03 +00:00
|
|
|
$json = '{"name": "vendor/package", "description": "description", "require": {"a": ["b"]} }';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals([
|
2022-08-17 12:20:07 +00:00
|
|
|
['property' => 'require.a', 'message' => 'Array value found, but a string is required', 'constraint' => 'type'],
|
|
|
|
], $this->check($json));
|
2016-11-03 12:19:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testMinimumStabilityValues(): void
|
2015-02-10 11:46:11 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$expectedError = [
|
|
|
|
[
|
2015-02-10 11:46:11 +00:00
|
|
|
'property' => 'minimum-stability',
|
2021-05-20 09:04:19 +00:00
|
|
|
'message' => 'Does not have a value in the enumeration ["dev","alpha","beta","rc","RC","stable"]',
|
|
|
|
'constraint' => 'enum',
|
2022-08-17 12:20:07 +00:00
|
|
|
'enum' => ['dev', 'alpha', 'beta', 'rc', 'RC', 'stable'],
|
|
|
|
],
|
|
|
|
];
|
2021-05-20 09:04:19 +00:00
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "" }';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals($expectedError, $this->check($json), 'empty string');
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dummy" }';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals($expectedError, $this->check($json), 'dummy');
|
2021-05-20 09:04:19 +00:00
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "devz" }';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals($expectedError, $this->check($json), 'devz');
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dev" }';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($this->check($json), 'dev');
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "alpha" }';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($this->check($json), 'alpha');
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "beta" }';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($this->check($json), 'beta');
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "rc" }';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($this->check($json), 'rc lowercase');
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "RC" }';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($this->check($json), 'rc uppercase');
|
2015-02-10 11:46:11 +00:00
|
|
|
|
|
|
|
$json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "stable" }';
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($this->check($json), 'stable');
|
2015-02-10 11:46:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 09:37:51 +00:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
private function check(string $json)
|
2015-02-10 11:46:11 +00:00
|
|
|
{
|
|
|
|
$validator = new Validator();
|
2023-03-21 10:17:55 +00:00
|
|
|
$validator->check(json_decode($json), (object) ['$ref' => 'file://' . JsonFile::COMPOSER_SCHEMA_PATH]);
|
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;
|
|
|
|
}
|
|
|
|
}
|