1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
<?php
/*
* This file is part of composer/spdx-licenses.
*
* (c) Composer <https://github.com/composer>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Composer\Spdx;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class SpdxLicensesTest extends TestCase
{
/**
* @var SpdxLicenses
*/
private $licenses;
/**
* @return void
*/
public function setUp(): void
{
$this->licenses = new SpdxLicenses();
}
/**
* @dataProvider provideValidLicenses
*
* @param string|string[] $license
* @return void
*/
#[DataProvider('provideValidLicenses')]
public function testValidate($license)
{
$this->assertTrue($this->licenses->validate($license));
}
/**
* @dataProvider provideInvalidLicenses
*
* @param string|string[] $invalidLicense
* @return void
*/
#[DataProvider('provideInValidLicenses')]
public function testInvalidLicenses($invalidLicense)
{
$this->assertFalse($this->licenses->validate($invalidLicense));
}
/**
* @dataProvider provideInvalidArgument
*
* @param mixed $invalidArgument
* @return void
*/
#[DataProvider('provideInValidArgument')]
public function testInvalidArgument($invalidArgument)
{
$this->expectException('\InvalidArgumentException');
$this->licenses->validate($invalidArgument);
}
/**
* @testdox Resources directory exists at expected locatation.
* @return void
*/
public function testGetResourcesDir()
{
$dir = SpdxLicenses::getResourcesDir();
$this->assertTrue(
is_dir($dir),
'Expected resources directory to exist.'
);
$this->assertEquals(
realpath($dir),
realpath(__DIR__ . '/../../data/Composer/res'),
'Expected resources directory to be "res" (relative to project root).'
);
}
/**
* @testdox Resources files exist at expected locations.
* @dataProvider provideResourceFiles
*
* @param string $file
* @return void
*/
#[DataProvider('provideResourceFiles')]
public function testResourceFilesExist($file)
{
$this->assertFileExists(
SpdxLicenses::getResourcesDir() . '/' . $file,
'Expected file to exist in resources dir: ' . $file
);
}
/**
* @testdox Resources files contain valid JSON.
* @dataProvider provideResourceFiles
*
* @param string $file
* @return void
*/
#[DataProvider('provideResourceFiles')]
public function testResourceFilesContainJson($file)
{
$contents = file_get_contents(SpdxLicenses::getResourcesDir() . '/' . $file);
if (false === $contents) {
$this->fail('Could not read the license file at '.SpdxLicenses::getResourcesDir() . '/' . $file);
return;
}
$json = json_decode($contents, true);
if (null === $json && json_last_error()) {
switch (json_last_error()) {
case JSON_ERROR_NONE:
$error = ' - no errors';
break;
case JSON_ERROR_DEPTH:
$error = ' - maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$error = ' - underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$error = ' - unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error = ' - syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$error = ' - malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$error = ' - unknown error';
break;
}
$this->fail('Could not decode JSON within ' . $file . $error);
}
$this->assertNotEmpty($json);
}
/**
* @return void
*/
public function testGetLicenseByIdentifier()
{
/** @var SPDXLicense $license */
$license = $this->licenses->getLicenseByIdentifier('AGPL-1.0-only');
$this->assertTrue(is_array($license));
$this->assertEquals('Affero General Public License v1.0 only', $license[0]);
$this->assertFalse($license[1]);
$this->assertStringStartsWith('https://spdx.org/licenses/', $license[2]);
$this->assertFalse($license[3]);
$licenseNull = $this->licenses->getLicenseByIdentifier('AGPL-1.0-Illegal');
$this->assertNull($licenseNull);
}
/**
* @return void
*/
public function testGetLicenses()
{
$results = $this->licenses->getLicenses();
$this->assertArrayHasKey('cc-by-sa-4.0', $results);
$this->assertArrayHasKey(0, $results['cc-by-sa-4.0']);
$this->assertEquals('CC-BY-SA-4.0', $results['cc-by-sa-4.0'][0]);
$this->assertEquals('Creative Commons Attribution Share Alike 4.0 International', $results['cc-by-sa-4.0'][1]);
$this->assertEquals(false, $results['cc-by-sa-4.0'][2]);
$this->assertEquals(false, $results['cc-by-sa-4.0'][3]);
}
/**
* @return void
*/
public function testGetExceptionByIdentifier()
{
$licenseNull = $this->licenses->getExceptionByIdentifier('Font-exception-2.0-Errorl');
$this->assertNull($licenseNull);
/** @var SPDXLicenseException $license */
$license = $this->licenses->getExceptionByIdentifier('Font-exception-2.0');
$this->assertIsArray($license);
$this->assertSame('Font exception 2.0', $license[0]);
}
/**
* @return void
*/
public function testGetIdentifierByName()
{
$identifier = $this->licenses->getIdentifierByName('Affero General Public License v1.0');
$this->assertEquals($identifier, 'AGPL-1.0');
$identifier = $this->licenses->getIdentifierByName('BSD 2-Clause "Simplified" License');
$this->assertEquals($identifier, 'BSD-2-Clause');
$identifier = $this->licenses->getIdentifierByName('Font exception 2.0');
$this->assertEquals($identifier, 'Font-exception-2.0');
$identifier = $this->licenses->getIdentifierByName('null-identifier-name');
$this->assertNull($identifier);
}
/**
* @return void
*/
public function testIsOsiApprovedByIdentifier()
{
$osiApproved = $this->licenses->isOsiApprovedByIdentifier('MIT');
$this->assertTrue($osiApproved);
$osiApproved = $this->licenses->isOsiApprovedByIdentifier('AGPL-1.0');
$this->assertFalse($osiApproved);
}
/**
* @return void
*/
public function testIsDeprecatedByIdentifier()
{
$deprecated = $this->licenses->isDeprecatedByIdentifier('GPL-3.0');
$this->assertTrue($deprecated);
$deprecated = $this->licenses->isDeprecatedByIdentifier('GPL-3.0-only');
$this->assertFalse($deprecated);
}
/**
* @return array[]
*/
public static function provideResourceFiles()
{
return array(
array(SpdxLicenses::LICENSES_FILE),
array(SpdxLicenses::EXCEPTIONS_FILE),
);
}
/**
* @return array<int, string|string[]>
*/
public static function provideValidLicenses()
{
$contents = file_get_contents(SpdxLicenses::getResourcesDir() . '/' . SpdxLicenses::LICENSES_FILE);
if (false === $contents) {
throw new \LogicException('Could not read the license file at '.SpdxLicenses::getResourcesDir() . '/' . SpdxLicenses::LICENSES_FILE);
}
$licenses = json_decode($contents, true);
/** @var string[] $identifiers */
$identifiers = array_keys($licenses);
$valid = array_merge(
array(
'MIT',
'MIT+',
array('(MIT)'),
'NONE',
'NOASSERTION',
'LicenseRef-3',
array('LGPL-2.0-only', 'GPL-3.0-or-later'),
'(LGPL-2.0-only or GPL-3.0-or-later)',
'(LGPL-2.0-only OR GPL-3.0-or-later)',
array('EUDatagrid and GPL-3.0-or-later'),
'(EUDatagrid and GPL-3.0-or-later)',
'(EUDatagrid AND GPL-3.0-or-later)',
'GPL-2.0-only with Autoconf-exception-2.0',
'GPL-2.0-only WITH Autoconf-exception-2.0',
'GPL-2.0-or-later WITH Autoconf-exception-2.0',
array('(GPL-3.0-only and GPL-2.0-only or GPL-3.0-or-later)'),
),
$identifiers
);
foreach ($valid as &$r) {
$r = array($r);
}
return $valid;
}
/**
* @return array[]
*/
public static function provideInvalidLicenses()
{
return array(
array(''),
array(array()),
array('The system pwns you'),
array('()'),
array('(MIT'),
array('MIT)'),
array('MIT NONE'),
array('MIT AND NONE'),
array('MIT (MIT and MIT)'),
array('(MIT and MIT) MIT'),
array(array('LGPL-2.0-only', 'The system pwns you')),
array('and GPL-3.0-or-later'),
array('(EUDatagrid and GPL-3.0-or-later and )'),
array('(EUDatagrid xor GPL-3.0-or-later)'),
array('(NONE or MIT)'),
array('(NOASSERTION or MIT)'),
array('Autoconf-exception-2.0 WITH MIT'),
array('MIT WITH'),
array('MIT OR'),
array('MIT AND'),
);
}
/**
* @return array[]
*/
public static function provideInvalidArgument()
{
return array(
array(null),
array(new \stdClass()),
array(array(new \stdClass())),
array(array('mixed', new \stdClass())),
array(array(new \stdClass(), new \stdClass())),
);
}
/**
* @param string $exception
* @param string|null $message
* @param int|null $code
* @return void
*/
public function setExpectedException($exception, $message = null, $code = null)
{
if (!class_exists('PHPUnit\Framework\Error\Notice')) {
$exception = str_replace('PHPUnit\\Framework\\Error\\', 'PHPUnit_Framework_Error_', $exception);
}
if (method_exists($this, 'expectException')) {
$this->expectException($exception);
if (null !== $message) {
$this->expectExceptionMessage($message);
}
} else {
/** @phpstan-ignore-next-line */
parent::setExpectedException($exception, $message, $code);
}
}
}
|