SpdxLicenseTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "MIT+",
  24. "NONE",
  25. "NOASSERTION",
  26. "LicenseRef-3",
  27. array("LGPL-2.0", "GPL-3.0+"),
  28. "(LGPL-2.0 or GPL-3.0+)",
  29. "(LGPL-2.0 OR GPL-3.0+)",
  30. "(EUDatagrid and GPL-3.0+)",
  31. "(EUDatagrid AND GPL-3.0+)",
  32. "GPL-2.0 with Autoconf-exception-2.0",
  33. "GPL-2.0 WITH Autoconf-exception-2.0",
  34. "GPL-2.0+ WITH Autoconf-exception-2.0",
  35. ),
  36. $identifiers
  37. );
  38. foreach ($valid as &$r) {
  39. $r = array($r);
  40. }
  41. return $valid;
  42. }
  43. public static function provideInvalidLicenses()
  44. {
  45. return array(
  46. array(""),
  47. array(array()),
  48. array("The system pwns you"),
  49. array("()"),
  50. array("(MIT)"),
  51. array("(MIT"),
  52. array("MIT)"),
  53. array("MIT NONE"),
  54. array("MIT AND NONE"),
  55. array("MIT (MIT and MIT)"),
  56. array("(MIT and MIT) MIT"),
  57. array(array("LGPL-2.0", "The system pwns you")),
  58. array("and GPL-3.0+"),
  59. array("EUDatagrid and GPL-3.0+"),
  60. array("(GPL-3.0 and GPL-2.0 or GPL-3.0+)"),
  61. array("(EUDatagrid and GPL-3.0+ and )"),
  62. array("(EUDatagrid xor GPL-3.0+)"),
  63. array("(MIT Or MIT)"),
  64. array("(NONE or MIT)"),
  65. array("(NOASSERTION or MIT)"),
  66. array("Autoconf-exception-2.0 WITH MIT"),
  67. array("MIT WITH"),
  68. array("MIT OR"),
  69. array("MIT AND"),
  70. );
  71. }
  72. public static function provideInvalidArgument()
  73. {
  74. return array(
  75. array(null),
  76. array(new \stdClass),
  77. array(array(new \stdClass)),
  78. array(array("mixed", new \stdClass)),
  79. array(array(new \stdClass, new \stdClass)),
  80. );
  81. }
  82. /**
  83. * @dataProvider provideValidLicenses
  84. * @param $license
  85. */
  86. public function testValidate($license)
  87. {
  88. $this->assertTrue($this->license->validate($license));
  89. }
  90. /**
  91. * @dataProvider provideInvalidLicenses
  92. * @param string|array $invalidLicense
  93. */
  94. public function testInvalidLicenses($invalidLicense)
  95. {
  96. $this->assertFalse($this->license->validate($invalidLicense));
  97. }
  98. /**
  99. * @dataProvider provideInvalidArgument
  100. * @expectedException InvalidArgumentException
  101. */
  102. public function testInvalidArgument($invalidArgument)
  103. {
  104. $this->license->validate($invalidArgument);
  105. }
  106. public function testGetLicenseByIdentifier()
  107. {
  108. $license = $this->license->getLicenseByIdentifier('AGPL-1.0');
  109. $this->assertEquals($license[0], 'Affero General Public License v1.0'); // fullname
  110. $this->assertFalse($license[1]); // osi approved
  111. }
  112. public function testGetIdentifierByName()
  113. {
  114. $identifier = $this->license->getIdentifierByName('Affero General Public License v1.0');
  115. $this->assertEquals($identifier, 'AGPL-1.0');
  116. $identifier = $this->license->getIdentifierByName('BSD 2-clause "Simplified" License');
  117. $this->assertEquals($identifier, 'BSD-2-Clause');
  118. }
  119. public function testIsOsiApprovedByIdentifier()
  120. {
  121. $osiApproved = $this->license->isOsiApprovedByIdentifier('MIT');
  122. $this->assertTrue($osiApproved);
  123. $osiApproved = $this->license->isOsiApprovedByIdentifier('AGPL-1.0');
  124. $this->assertFalse($osiApproved);
  125. }
  126. }