ComposerSchemaTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 testRequiredProperties()
  20. {
  21. $json = '{ }';
  22. $this->assertEquals(array(
  23. array('property' => 'name', 'message' => 'The property name is required', 'constraint' => 'required'),
  24. array('property' => 'description', 'message' => 'The property description is required', 'constraint' => 'required'),
  25. ), $this->check($json));
  26. $json = '{ "name": "vendor/package" }';
  27. $this->assertEquals(array(
  28. array('property' => 'description', 'message' => 'The property description is required', 'constraint' => 'required'),
  29. ), $this->check($json));
  30. $json = '{ "description": "generic description" }';
  31. $this->assertEquals(array(
  32. array('property' => 'name', 'message' => 'The property name is required', 'constraint' => 'required'),
  33. ), $this->check($json));
  34. }
  35. public function testOptionalAbandonedProperty()
  36. {
  37. $json = '{"name": "name", "description": "description", "abandoned": true}';
  38. $this->assertTrue($this->check($json));
  39. }
  40. public function testRequireTypes()
  41. {
  42. $json = '{"name": "name", "description": "description", "require": {"a": ["b"]} }';
  43. $this->assertEquals(array(
  44. array('property' => 'require.a', 'message' => 'Array value found, but a string is required', 'constraint' => 'type'),
  45. ), $this->check($json));
  46. }
  47. public function testMinimumStabilityValues()
  48. {
  49. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "" }';
  50. $this->assertEquals(array(
  51. array(
  52. 'property' => 'minimum-stability',
  53. 'message' => 'Does not match the regex pattern ^dev|alpha|beta|rc|RC|stable$',
  54. 'constraint' => 'pattern',
  55. 'pattern' => '^dev|alpha|beta|rc|RC|stable$',
  56. ),
  57. ), $this->check($json), 'empty string');
  58. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dummy" }';
  59. $this->assertEquals(array(
  60. array(
  61. 'property' => 'minimum-stability',
  62. 'message' => 'Does not match the regex pattern ^dev|alpha|beta|rc|RC|stable$',
  63. 'constraint' => 'pattern',
  64. 'pattern' => '^dev|alpha|beta|rc|RC|stable$',
  65. ),
  66. ), $this->check($json), 'dummy');
  67. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dev" }';
  68. $this->assertTrue($this->check($json), 'dev');
  69. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "alpha" }';
  70. $this->assertTrue($this->check($json), 'alpha');
  71. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "beta" }';
  72. $this->assertTrue($this->check($json), 'beta');
  73. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "rc" }';
  74. $this->assertTrue($this->check($json), 'rc lowercase');
  75. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "RC" }';
  76. $this->assertTrue($this->check($json), 'rc uppercase');
  77. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "stable" }';
  78. $this->assertTrue($this->check($json), 'stable');
  79. }
  80. private function check($json)
  81. {
  82. $validator = new Validator();
  83. $validator->check(json_decode($json), (object) array('$ref' => 'file://' . __DIR__ . '/../../../../res/composer-schema.json'));
  84. if (!$validator->isValid()) {
  85. $errors = $validator->getErrors();
  86. // remove justinrainbow/json-schema 3.0/5.2 props so it works with all versions
  87. foreach ($errors as &$err) {
  88. unset($err['pointer']);
  89. unset($err['context']);
  90. }
  91. return $errors;
  92. }
  93. return true;
  94. }
  95. }