Skip to content

Commit

Permalink
Merge pull request #44 from madewithlove/fix-parsing-packages-without…
Browse files Browse the repository at this point in the history
…-license

Ensure dependencies without licenses can be parsed properly
  • Loading branch information
WouterSioen authored Apr 20, 2023
2 parents bfeb4b0 + bb5c6cc commit af994c9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Composer/UsedLicensesParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getPackagesWithLicense(string $license, bool $noDev): array

$decodedJson = $this->retriever->getComposerLicenses($noDev);
foreach ($decodedJson['dependencies'] as $packageName => $licenseInfo) {
if ($licenseInfo['license'][0] === $license) {
if (($licenseInfo['license'][0] ?? null) === $license) {
$packages[] = $packageName;
}
}
Expand Down
70 changes: 70 additions & 0 deletions tests/Composer/UsedLicenseParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,74 @@ private function getJsonData(): array
}', true);
return $jsonDecoded;
}


/**
* @test
*/
public function canParseDependenciesWithoutLicense(): void
{
$expected = [];

$this->licenseRetriever->method('getComposerLicenses')->willReturn($this->getJsonDataWithoutLicenses());

$this->assertEquals(
$expected,
$this->usedLicensesParser->parseLicenses(false)
);
}

/**
* @test
*/
public function canGetPackagesWithLicenseWhenThereAreDependenciesWithoutLicenses(): void
{
$expected = [];

$this->licenseRetriever->method('getComposerLicenses')->willReturn($this->getJsonDataWithoutLicenses());

$this->assertEquals(
$expected,
$this->usedLicensesParser->getPackagesWithLicense('FOO', false)
);
}

/**
* @return array{dependencies:array<string,array{version:string,license:list<string>}>}
*/
private function getJsonDataWithoutLicenses(): array
{
/** @var array{dependencies:array<string,array{version:string,license:list<string>}>} $jsonDecoded */
$jsonDecoded = json_decode('
{
"name": "madewithlove/licence-checker-php",
"version": "dev-master",
"license": [
"MIT"
],
"dependencies": {
"some/dependency": {
"version": "1.0.0",
"license": []
},
"other/dependency": {
"version": "v5.0.5",
"license": []
},
"yet/another-dependency": {
"version": "v1.14.0",
"license": []
},
"repeated/license-for-dependency": {
"version": "v1.14.0",
"license": []
},
"another/repeated-license": {
"version": "v5.0.5",
"license": []
}
}
}', true);
return $jsonDecoded;
}
}

0 comments on commit af994c9

Please sign in to comment.