ComposerSchemaTest.php 4.4 KB

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