From c728569d33b13458a4ee61288999e80732328c94 Mon Sep 17 00:00:00 2001 From: Jesper Skytte Marcussen Date: Wed, 12 Oct 2022 17:25:28 +0200 Subject: [PATCH] Added new `LicensesCommandTest` class (#11105) --- .../Test/Command/LicensesCommandTest.php | 183 ++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 tests/Composer/Test/Command/LicensesCommandTest.php diff --git a/tests/Composer/Test/Command/LicensesCommandTest.php b/tests/Composer/Test/Command/LicensesCommandTest.php new file mode 100644 index 000000000..6043630b2 --- /dev/null +++ b/tests/Composer/Test/Command/LicensesCommandTest.php @@ -0,0 +1,183 @@ + + * Jordi Boggiano + * + * 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', + 'license' => 'MIT', + 'require' => [ + 'first/pkg' => '^2.0', + 'second/pkg' => '3.*', + 'third/pkg' => '^1.3', + ], + 'require-dev' => [ + 'dev/pkg' => '~2.0', + ], + ]); + + $first = $this->getPackage('first/pkg', '2.3.4'); + $first->setLicense(['MIT']); + + $second = $this->getPackage('second/pkg', '3.4.0'); + $second->setLicense(['LGPL-2.0-only']); + $second->setHomepage('https://example.org'); + + $third = $this->getPackage('third/pkg', '1.5.4'); + + $dev = $this->getPackage('dev/pkg', '2.3.4.5'); + $dev->setLicense(['MIT']); + + $this->createInstalledJson([$first, $second, $third], [$dev]); + $this->createComposerLock([$first, $second, $third], [$dev]); + } + + public function testBasicRun(): void + { + $appTester = $this->getApplicationTester(); + $this->assertSame(0, $appTester->run(['command' => 'license'])); + + $expected = [ + ["Name:", "test/pkg"], + ["Version:", "1.0.0+no-version-set"], + ["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; + } + + $this->assertMatchesRegularExpression("/" . implode("\s+", $expected[$i]) . "/", $line); + } + } + + public function testNoDev(): void + { + $appTester = $this->getApplicationTester(); + $this->assertSame(0, $appTester->run(['command' => 'license', '--no-dev' => true])); + + $expected = [ + ["Name:", "test/pkg"], + ["Version:", "1.0.0+no-version-set"], + ["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; + } + + $this->assertMatchesRegularExpression("/" . implode("\s+", $expected[$i]) . "/", $line); + } + } + + public function testFormatJson(): void + { + $appTester = $this->getApplicationTester(); + $this->assertSame(0, $appTester->run(['command' => 'license', '--format' => 'json'], ['capture_stderr_separately' => true])); + + $expected = [ + "name" => "test/pkg", + "version" => "1.0.0+no-version-set", + "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" => [] + ] + ] + ]; + + $this->assertSame($expected, json_decode($appTester->getDisplay(), true)); + } + + public function testFormatSummary(): void + { + $appTester = $this->getApplicationTester(); + $this->assertSame(0, $appTester->run(['command' => 'license', '--format' => 'summary'])); + + $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; + + $this->assertMatchesRegularExpression("/$key\s+$value/", $lines[$i]); + } + } + + public function testFormatUnknown(): void + { + $this->expectException(\RuntimeException::class); + + $appTester = $this->getApplicationTester(); + $appTester->run(['command' => 'license', '--format' => 'unknown']); + } +}