File: SearchCommandTest.php

package info (click to toggle)
composer 2.8.8-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,716 kB
  • sloc: php: 77,384; makefile: 59; xml: 39
file content (130 lines) | stat: -rw-r--r-- 3,967 bytes parent folder | download
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
<?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;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;

class SearchCommandTest extends TestCase
{
    /**
     * @param array<mixed> $command
     */
    #[DataProvider('provideSearch')]
    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 static function provideSearch(): \Generator
    {
        yield 'by name and description' => [
            ['tokens' => ['fancy']],
            <<<OUTPUT
bar/baz                <warning>! Abandoned !</warning> fancy baz
vendor-2/fancy-package
OUTPUT
        ];

        yield 'by name and description with multiple tokens' => [
            ['tokens' => ['fancy', 'vendor']],
            <<<OUTPUT
vendor-1/package-1     generic description
bar/baz                <warning>! Abandoned !</warning> fancy baz
vendor-2/fancy-package
OUTPUT
        ];

        yield 'by name only' => [
            ['tokens' => ['fancy'], '--only-name' => true],
            <<<OUTPUT
vendor-2/fancy-package
OUTPUT
        ];

        yield 'by vendor only' => [
            ['tokens' => ['bar'], '--only-vendor' => true],
            <<<OUTPUT
bar
OUTPUT
        ];

        yield 'by type' => [
            ['tokens' => ['vendor'], '--type' => 'foo'],
            <<<OUTPUT
vendor-2/fancy-package
OUTPUT
        ];

        yield 'json format' => [
            ['tokens' => ['vendor-2/fancy'], '--format' => 'json'],
            <<<OUTPUT
[
    {
        "name": "vendor-2/fancy-package",
        "description": null
    }
]
OUTPUT
        ];

        yield 'no results' => [
            ['tokens' => ['invalid-package-name']],
        ];
    }
}