ComposerSchemaTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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'),
  23. array('property' => 'description', 'message' => 'The property description is required'),
  24. ), $this->check($json));
  25. $json = '{ "name": "vendor/package" }';
  26. $this->assertEquals(array(
  27. array('property' => 'description', 'message' => 'The property description is required'),
  28. ), $this->check($json));
  29. $json = '{ "description": "generic description" }';
  30. $this->assertEquals(array(
  31. array('property' => 'name', 'message' => 'The property name is 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. ),
  47. ), $this->check($json), 'empty string');
  48. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dummy" }';
  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. ),
  54. ), $this->check($json), 'dummy');
  55. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "dev" }';
  56. $this->assertTrue($this->check($json), 'dev');
  57. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "alpha" }';
  58. $this->assertTrue($this->check($json), 'alpha');
  59. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "beta" }';
  60. $this->assertTrue($this->check($json), 'beta');
  61. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "rc" }';
  62. $this->assertTrue($this->check($json), 'rc lowercase');
  63. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "RC" }';
  64. $this->assertTrue($this->check($json), 'rc uppercase');
  65. $json = '{ "name": "vendor/package", "description": "generic description", "minimum-stability": "stable" }';
  66. $this->assertTrue($this->check($json), 'stable');
  67. }
  68. private function check($json)
  69. {
  70. $schema = json_decode(file_get_contents(__DIR__ . '/../../../../res/composer-schema.json'));
  71. $validator = new Validator();
  72. $validator->check(json_decode($json), $schema);
  73. if (!$validator->isValid()) {
  74. return $validator->getErrors();
  75. }
  76. return true;
  77. }
  78. }