SpdxLicenseTest.php 4.0 KB

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