ComposerSchemaTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 testMinimumStabilityValues()
  40. {
  41. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "" }';
  42. $this->assertEquals(array(
  43. array(
  44. 'property' => 'minimum-stability',
  45. 'message' => 'Does not match the regex pattern ^dev|alpha|beta|rc|RC|stable$',
  46. 'constraint' => 'pattern',
  47. 'pattern' => '^dev|alpha|beta|rc|RC|stable$',
  48. ),
  49. ), $this->check($json), 'empty string');
  50. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dummy" }';
  51. $this->assertEquals(array(
  52. array(
  53. 'property' => 'minimum-stability',
  54. 'message' => 'Does not match the regex pattern ^dev|alpha|beta|rc|RC|stable$',
  55. 'constraint' => 'pattern',
  56. 'pattern' => '^dev|alpha|beta|rc|RC|stable$',
  57. ),
  58. ), $this->check($json), 'dummy');
  59. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dev" }';
  60. $this->assertTrue($this->check($json), 'dev');
  61. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "alpha" }';
  62. $this->assertTrue($this->check($json), 'alpha');
  63. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "beta" }';
  64. $this->assertTrue($this->check($json), 'beta');
  65. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "rc" }';
  66. $this->assertTrue($this->check($json), 'rc lowercase');
  67. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "RC" }';
  68. $this->assertTrue($this->check($json), 'rc uppercase');
  69. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "stable" }';
  70. $this->assertTrue($this->check($json), 'stable');
  71. }
  72. private function check($json)
  73. {
  74. $schema = json_decode(file_get_contents(__DIR__ . '/../../../../res/composer-schema.json'));
  75. $validator = new Validator();
  76. $validator->check(json_decode($json), $schema);
  77. if (!$validator->isValid()) {
  78. return $validator->getErrors();
  79. }
  80. return true;
  81. }
  82. }