2022-10-12 15:25:28 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Command;
|
|
|
|
|
|
|
|
use Composer\Test\TestCase;
|
|
|
|
|
|
|
|
class LicensesCommandTest extends TestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->initTempComposer([
|
|
|
|
'name' => 'test/pkg',
|
2024-02-23 09:47:36 +00:00
|
|
|
'version' => '1.2.3',
|
2022-10-12 15:25:28 +00:00
|
|
|
'license' => 'MIT',
|
|
|
|
'require' => [
|
|
|
|
'first/pkg' => '^2.0',
|
|
|
|
'second/pkg' => '3.*',
|
|
|
|
'third/pkg' => '^1.3',
|
|
|
|
],
|
|
|
|
'require-dev' => [
|
|
|
|
'dev/pkg' => '~2.0',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
$first = self::getPackage('first/pkg', '2.3.4');
|
2022-10-12 15:25:28 +00:00
|
|
|
$first->setLicense(['MIT']);
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
$second = self::getPackage('second/pkg', '3.4.0');
|
2022-10-12 15:25:28 +00:00
|
|
|
$second->setLicense(['LGPL-2.0-only']);
|
|
|
|
$second->setHomepage('https://example.org');
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
$third = self::getPackage('third/pkg', '1.5.4');
|
2022-10-12 15:25:28 +00:00
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
$dev = self::getPackage('dev/pkg', '2.3.4.5');
|
2022-10-12 15:25:28 +00:00
|
|
|
$dev->setLicense(['MIT']);
|
|
|
|
|
|
|
|
$this->createInstalledJson([$first, $second, $third], [$dev]);
|
|
|
|
$this->createComposerLock([$first, $second, $third], [$dev]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicRun(): void
|
|
|
|
{
|
|
|
|
$appTester = $this->getApplicationTester();
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertSame(0, $appTester->run(['command' => 'license']));
|
2022-10-12 15:25:28 +00:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
["Name:", "test/pkg"],
|
2024-02-23 09:47:36 +00:00
|
|
|
["Version:", "1.2.3"],
|
2022-10-12 15:25:28 +00:00
|
|
|
["Licenses:", "MIT"],
|
|
|
|
["Dependencies:"],
|
|
|
|
[],
|
|
|
|
["Name", "Version", "Licenses"],
|
|
|
|
["dev/pkg", "2.3.4.5", "MIT"],
|
|
|
|
["first/pkg", "2.3.4", "MIT"],
|
|
|
|
["second/pkg", "3.4.0", "LGPL-2.0-only"],
|
|
|
|
["third/pkg", "1.5.4", "none"],
|
|
|
|
];
|
|
|
|
|
|
|
|
array_walk_recursive($expected, static function (&$value) {
|
|
|
|
$value = preg_quote($value, '/');
|
|
|
|
});
|
|
|
|
|
|
|
|
foreach (explode(PHP_EOL, $appTester->getDisplay()) as $i => $line) {
|
|
|
|
if (trim($line) === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-27 15:11:31 +00:00
|
|
|
if (!isset($expected[$i])) {
|
|
|
|
$this->fail('Got more output lines than expected');
|
|
|
|
}
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertMatchesRegularExpression("/" . implode("\s+", $expected[$i]) . "/", $line);
|
2022-10-12 15:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoDev(): void
|
|
|
|
{
|
|
|
|
$appTester = $this->getApplicationTester();
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertSame(0, $appTester->run(['command' => 'license', '--no-dev' => true]));
|
2022-10-12 15:25:28 +00:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
["Name:", "test/pkg"],
|
2024-02-23 09:47:36 +00:00
|
|
|
["Version:", "1.2.3"],
|
2022-10-12 15:25:28 +00:00
|
|
|
["Licenses:", "MIT"],
|
|
|
|
["Dependencies:"],
|
|
|
|
[],
|
|
|
|
["Name", "Version", "Licenses"],
|
|
|
|
["first/pkg", "2.3.4", "MIT"],
|
|
|
|
["second/pkg", "3.4.0", "LGPL-2.0-only"],
|
|
|
|
["third/pkg", "1.5.4", "none"],
|
|
|
|
];
|
|
|
|
|
|
|
|
array_walk_recursive($expected, static function (&$value) {
|
|
|
|
$value = preg_quote($value, '/');
|
|
|
|
});
|
|
|
|
|
|
|
|
foreach (explode(PHP_EOL, $appTester->getDisplay()) as $i => $line) {
|
|
|
|
if (trim($line) === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-27 15:11:31 +00:00
|
|
|
if (!isset($expected[$i])) {
|
|
|
|
$this->fail('Got more output lines than expected');
|
|
|
|
}
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertMatchesRegularExpression("/" . implode("\s+", $expected[$i]) . "/", $line);
|
2022-10-12 15:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormatJson(): void
|
|
|
|
{
|
|
|
|
$appTester = $this->getApplicationTester();
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertSame(0, $appTester->run(['command' => 'license', '--format' => 'json'], ['capture_stderr_separately' => true]));
|
2022-10-12 15:25:28 +00:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
"name" => "test/pkg",
|
2024-02-23 09:47:36 +00:00
|
|
|
"version" => "1.2.3",
|
2022-10-12 15:25:28 +00:00
|
|
|
"license" => ["MIT"],
|
|
|
|
"dependencies" => [
|
|
|
|
"dev/pkg" => [
|
|
|
|
"version" => "2.3.4.5",
|
|
|
|
"license" => [
|
|
|
|
"MIT"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"first/pkg" => [
|
|
|
|
"version" => "2.3.4",
|
|
|
|
"license" => [
|
|
|
|
"MIT"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"second/pkg" => [
|
|
|
|
"version" => "3.4.0",
|
|
|
|
"license" => [
|
|
|
|
"LGPL-2.0-only"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"third/pkg" => [
|
|
|
|
"version" => "1.5.4",
|
|
|
|
"license" => []
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertSame($expected, json_decode($appTester->getDisplay(), true));
|
2022-10-12 15:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormatSummary(): void
|
|
|
|
{
|
|
|
|
$appTester = $this->getApplicationTester();
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertSame(0, $appTester->run(['command' => 'license', '--format' => 'summary']));
|
2022-10-12 15:25:28 +00:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
['-', '-'],
|
|
|
|
['License', 'Number of dependencies'],
|
|
|
|
['-', '-'],
|
|
|
|
['MIT', '2'],
|
|
|
|
['LGPL-2.0-only', '1'],
|
|
|
|
['none', '1'],
|
|
|
|
['-', '-'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$lines = explode("\n", $appTester->getDisplay());
|
|
|
|
|
|
|
|
foreach ($expected as $i => $expect) {
|
|
|
|
[$key, $value] = $expect;
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertMatchesRegularExpression("/$key\s+$value/", $lines[$i]);
|
2022-10-12 15:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormatUnknown(): void
|
|
|
|
{
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
|
|
|
|
$appTester = $this->getApplicationTester();
|
|
|
|
$appTester->run(['command' => 'license', '--format' => 'unknown']);
|
|
|
|
}
|
|
|
|
}
|