ComposerSchemaTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Json;
  12. use JsonSchema\Validator;
  13. use Composer\Test\TestCase;
  14. /**
  15. * @author Rob Bast <rob.bast@gmail.com>
  16. */
  17. class ComposerSchemaTest extends TestCase
  18. {
  19. public function testNamePattern()
  20. {
  21. $expectedError = array(
  22. array(
  23. 'property' => 'name',
  24. 'message' => 'Does not match the regex pattern ^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$',
  25. 'constraint' => 'pattern',
  26. 'pattern' => '^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$'
  27. ),
  28. );
  29. $json = '{"name": "vendor/-pack__age", "description": "description"}';
  30. $this->assertEquals($expectedError, $this->check($json));
  31. $json = '{"name": "Vendor/Package", "description": "description"}';
  32. $this->assertEquals($expectedError, $this->check($json));
  33. }
  34. public function testRequiredProperties()
  35. {
  36. $json = '{ }';
  37. $this->assertEquals(array(
  38. array('property' => 'name', 'message' => 'The property name is required', 'constraint' => 'required'),
  39. array('property' => 'description', 'message' => 'The property description is required', 'constraint' => 'required'),
  40. ), $this->check($json));
  41. $json = '{ "name": "vendor/package" }';
  42. $this->assertEquals(array(
  43. array('property' => 'description', 'message' => 'The property description is required', 'constraint' => 'required'),
  44. ), $this->check($json));
  45. $json = '{ "description": "generic description" }';
  46. $this->assertEquals(array(
  47. array('property' => 'name', 'message' => 'The property name is required', 'constraint' => 'required'),
  48. ), $this->check($json));
  49. }
  50. public function testOptionalAbandonedProperty()
  51. {
  52. $json = '{"name": "vendor/package", "description": "description", "abandoned": true}';
  53. $this->assertTrue($this->check($json));
  54. }
  55. public function testRequireTypes()
  56. {
  57. $json = '{"name": "vendor/package", "description": "description", "require": {"a": ["b"]} }';
  58. $this->assertEquals(array(
  59. array('property' => 'require.a', 'message' => 'Array value found, but a string is required', 'constraint' => 'type'),
  60. ), $this->check($json));
  61. }
  62. public function testMinimumStabilityValues()
  63. {
  64. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "" }';
  65. $this->assertEquals(array(
  66. array(
  67. 'property' => 'minimum-stability',
  68. 'message' => 'Does not match the regex pattern ^dev|alpha|beta|rc|RC|stable$',
  69. 'constraint' => 'pattern',
  70. 'pattern' => '^dev|alpha|beta|rc|RC|stable$',
  71. ),
  72. ), $this->check($json), 'empty string');
  73. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dummy" }';
  74. $this->assertEquals(array(
  75. array(
  76. 'property' => 'minimum-stability',
  77. 'message' => 'Does not match the regex pattern ^dev|alpha|beta|rc|RC|stable$',
  78. 'constraint' => 'pattern',
  79. 'pattern' => '^dev|alpha|beta|rc|RC|stable$',
  80. ),
  81. ), $this->check($json), 'dummy');
  82. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dev" }';
  83. $this->assertTrue($this->check($json), 'dev');
  84. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "alpha" }';
  85. $this->assertTrue($this->check($json), 'alpha');
  86. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "beta" }';
  87. $this->assertTrue($this->check($json), 'beta');
  88. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "rc" }';
  89. $this->assertTrue($this->check($json), 'rc lowercase');
  90. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "RC" }';
  91. $this->assertTrue($this->check($json), 'rc uppercase');
  92. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "stable" }';
  93. $this->assertTrue($this->check($json), 'stable');
  94. }
  95. private function check($json)
  96. {
  97. $validator = new Validator();
  98. $validator->check(json_decode($json), (object) array('$ref' => 'file://' . __DIR__ . '/../../../../res/composer-schema.json'));
  99. if (!$validator->isValid()) {
  100. $errors = $validator->getErrors();
  101. // remove justinrainbow/json-schema 3.0/5.2 props so it works with all versions
  102. foreach ($errors as &$err) {
  103. unset($err['pointer']);
  104. unset($err['context']);
  105. }
  106. return $errors;
  107. }
  108. return true;
  109. }
  110. }