ComposerSchemaTest.php 3.3 KB

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