SpdxLicenseTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Composer\Test\Util;
  3. use Composer\TestCase;
  4. use Composer\Util\SpdxLicense;
  5. class SpdxLicenseTest extends TestCase
  6. {
  7. /**
  8. * @var object
  9. */
  10. private $license;
  11. public function setUp()
  12. {
  13. $this->license = new SpdxLicense;
  14. }
  15. public static function provideValidLicenses()
  16. {
  17. $json = file_get_contents(__DIR__ . '/../../../../res/spdx-licenses.json');
  18. $licenses = json_decode($json, true);
  19. $identifiers = array_keys($licenses);
  20. $valid = array_merge(
  21. array(
  22. "MIT",
  23. "NONE",
  24. "NOASSERTION",
  25. "LicenseRef-3",
  26. array("LGPL-2.0", "GPL-3.0+"),
  27. "(LGPL-2.0 or GPL-3.0+)",
  28. "(EUDatagrid and GPL-3.0+)",
  29. ),
  30. $identifiers
  31. );
  32. foreach ($valid as &$r) {
  33. $r = array($r);
  34. }
  35. return $valid;
  36. }
  37. public static function provideInvalidLicenses()
  38. {
  39. return array(
  40. array(""),
  41. array(array()),
  42. array("The system pwns you"),
  43. array("()"),
  44. array("(MIT)"),
  45. array("MIT NONE"),
  46. array("MIT (MIT and MIT)"),
  47. array("(MIT and MIT) MIT"),
  48. array(array("LGPL-2.0", "The system pwns you")),
  49. array("and GPL-3.0+"),
  50. array("EUDatagrid and GPL-3.0+"),
  51. array("(GPL-3.0 and GPL-2.0 or GPL-3.0+)"),
  52. array("(EUDatagrid and GPL-3.0+ and )"),
  53. array("(EUDatagrid xor GPL-3.0+)"),
  54. array("(MIT Or MIT)"),
  55. array("(NONE or MIT)"),
  56. array("(NOASSERTION or MIT)"),
  57. );
  58. }
  59. public static function provideInvalidArgument()
  60. {
  61. return array(
  62. array(null),
  63. array(new \stdClass),
  64. array(array(new \stdClass)),
  65. array(array("mixed", new \stdClass)),
  66. array(array(new \stdClass, new \stdClass)),
  67. );
  68. }
  69. /**
  70. * @dataProvider provideValidLicenses
  71. * @param $license
  72. */
  73. public function testValidate($license)
  74. {
  75. $this->assertTrue($this->license->validate($license));
  76. }
  77. /**
  78. * @dataProvider provideInvalidLicenses
  79. * @param string|array $invalidLicense
  80. */
  81. public function testInvalidLicenses($invalidLicense)
  82. {
  83. $this->assertFalse($this->license->validate($invalidLicense));
  84. }
  85. /**
  86. * @dataProvider provideInvalidArgument
  87. * @expectedException InvalidArgumentException
  88. */
  89. public function testInvalidArgument($invalidArgument)
  90. {
  91. $this->license->validate($invalidArgument);
  92. }
  93. public function testGetLicenseByIdentifier()
  94. {
  95. $license = $this->license->getLicenseByIdentifier('AGPL-1.0');
  96. $this->assertEquals($license[0], 'Affero General Public License v1.0'); // fullname
  97. $this->assertFalse($license[1]); // osi approved
  98. }
  99. public function testGetIdentifierByName()
  100. {
  101. $identifier = $this->license->getIdentifierByName('Affero General Public License v1.0');
  102. $this->assertEquals($identifier, 'AGPL-1.0');
  103. $identifier = $this->license->getIdentifierByName('BSD 2-clause "Simplified" License');
  104. $this->assertEquals($identifier, 'BSD-2-Clause');
  105. }
  106. public function testIsOsiApprovedByIdentifier()
  107. {
  108. $osiApproved = $this->license->isOsiApprovedByIdentifier('MIT');
  109. $this->assertTrue($osiApproved);
  110. $osiApproved = $this->license->isOsiApprovedByIdentifier('AGPL-1.0');
  111. $this->assertFalse($osiApproved);
  112. }
  113. }