From 74a045783bbec3e63467676ca25e6dcd1f16976d Mon Sep 17 00:00:00 2001 From: Ion Bazan Date: Tue, 16 Aug 2022 16:52:01 +0800 Subject: [PATCH] Add SearchCommand tests (#10982) Signed-off-by: Ion Bazan --- .../Test/Command/SearchCommandTest.php | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 tests/Composer/Test/Command/SearchCommandTest.php diff --git a/tests/Composer/Test/Command/SearchCommandTest.php b/tests/Composer/Test/Command/SearchCommandTest.php new file mode 100644 index 000000000..01f7a58be --- /dev/null +++ b/tests/Composer/Test/Command/SearchCommandTest.php @@ -0,0 +1,129 @@ + + * 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; +use InvalidArgumentException; + +class SearchCommandTest extends TestCase +{ + /** + * @dataProvider provideSearch + * @param array $command + */ + public function testSearch(array $command, string $expected = ''): void + { + $this->initTempComposer([ + 'repositories' => [ + 'packages' => [ + 'type' => 'package', + 'package' => [ + ['name' => 'vendor-1/package-1', 'description' => 'generic description', 'version' => '1.0.0'], + ['name' => 'foo/bar', 'description' => 'generic description', 'version' => '1.0.0'], + ['name' => 'bar/baz', 'description' => 'fancy baz', 'version' => '1.0.0', 'abandoned' => true], + ['name' => 'vendor-2/fancy-package', 'fancy description', 'version' => '1.0.0', 'type' => 'foo'], + ], + ], + ], + ]); + + $appTester = $this->getApplicationTester(); + $appTester->run(array_merge(['command' => 'search'], $command)); + self::assertSame(trim($expected), trim($appTester->getDisplay(true))); + } + + public function testInvalidFormat(): void + { + $this->initTempComposer([ + 'repositories' => [ + 'packagist.org' => false, + ], + ]); + + $appTester = $this->getApplicationTester(); + $result = $appTester->run(['command' => 'search', '--format' => 'test-format', 'tokens' => ['test']]); + self::assertSame(1, $result); + self::assertSame('Unsupported format "test-format". See help for supported formats.', trim($appTester->getDisplay(true))); + } + + public function testInvalidFlags(): void + { + $this->initTempComposer([ + 'repositories' => [ + 'packagist.org' => false, + ], + ]); + + $appTester = $this->getApplicationTester(); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('--only-name and --only-vendor cannot be used together'); + $appTester->run(['command' => 'search', '--only-vendor' => true, '--only-name' => true, 'tokens' => ['test']]); + } + + public function provideSearch(): \Generator + { + yield 'by name and description' => [ + ['tokens' => ['fancy']], + <<! Abandoned ! fancy baz +vendor-2/fancy-package +OUTPUT + ]; + + yield 'by name and description with multiple tokens' => [ + ['tokens' => ['fancy', 'vendor']], + <<! Abandoned ! fancy baz +vendor-2/fancy-package +OUTPUT + ]; + + yield 'by name only' => [ + ['tokens' => ['fancy'], '--only-name' => true], + << [ + ['tokens' => ['bar'], '--only-vendor' => true], + << [ + ['tokens' => ['vendor'], '--type' => 'foo'], + << [ + ['tokens' => ['vendor-2/fancy'], '--format' => 'json'], + << [ + ['tokens' => ['invalid-package-name']], + ]; + } +}