SpdxLicenseIdentifierTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Composer\Test\Util;
  3. use Composer\Test\TestCase;
  4. use Composer\Util\SpdxLicenseIdentifier;
  5. class SpdxLicenseIdentifierTest extends TestCase
  6. {
  7. public static function provideValidLicenses()
  8. {
  9. $valid = array_merge(
  10. array(
  11. "MIT",
  12. "NONE",
  13. "NOASSERTION",
  14. "LicenseRef-3",
  15. array("LGPL-2.0", "GPL-3.0+"),
  16. "(LGPL-2.0 or GPL-3.0+)",
  17. "(EUDatagrid and GPL-3.0+)",
  18. ),
  19. json_decode(file_get_contents(__DIR__ . '/../../../../res/spdx-identifier.json'))
  20. );
  21. foreach ($valid as &$r) {
  22. $r = array($r);
  23. }
  24. return $valid;
  25. }
  26. public static function provideInvalidLicenses()
  27. {
  28. return array(
  29. array(""),
  30. array(array()),
  31. array("The system pwns you"),
  32. array("()"),
  33. array("(MIT)"),
  34. array("MIT NONE"),
  35. array("MIT (MIT and MIT)"),
  36. array("(MIT and MIT) MIT"),
  37. array(array("LGPL-2.0", "The system pwns you")),
  38. array("and GPL-3.0+"),
  39. array("EUDatagrid and GPL-3.0+"),
  40. array("(GPL-3.0 and GPL-2.0 or GPL-3.0+)"),
  41. array("(EUDatagrid and GPL-3.0+ and )"),
  42. array("(EUDatagrid xor GPL-3.0+)"),
  43. array("(MIT Or MIT)"),
  44. array("(NONE or MIT)"),
  45. array("(NOASSERTION or MIT)"),
  46. );
  47. }
  48. public static function provideInvalidArgument()
  49. {
  50. return array(
  51. array(null),
  52. array(new \stdClass),
  53. array(array(new \stdClass)),
  54. array(array("mixed", new \stdClass)),
  55. array(array(new \stdClass, new \stdClass)),
  56. );
  57. }
  58. /**
  59. * @dataProvider provideValidLicenses
  60. * @param $license
  61. */
  62. public function testValidate($license)
  63. {
  64. $validator = new SpdxLicenseIdentifier();
  65. $this->assertTrue($validator->validate($license));
  66. }
  67. /**
  68. * @dataProvider provideInvalidLicenses
  69. * @param string|array $invalidLicense
  70. */
  71. public function testInvalidLicenses($invalidLicense)
  72. {
  73. $validator = new SpdxLicenseIdentifier();
  74. $this->assertFalse($validator->validate($invalidLicense));
  75. }
  76. /**
  77. * @dataProvider provideInvalidArgument
  78. * @expectedException InvalidArgumentException
  79. */
  80. public function testInvalidArgument($invalidArgument)
  81. {
  82. $validator = new SpdxLicenseIdentifier();
  83. $validator->validate($invalidArgument);
  84. }
  85. }