File: CompletionFunctionalTest.php

package info (click to toggle)
composer 2.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,528 kB
  • sloc: php: 83,030; makefile: 59; xml: 39
file content (154 lines) | stat: -rw-r--r-- 6,207 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?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;

use Composer\Console\Application;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use PHPUnit\Framework\Attributes\DataProvider;

/**
 * Validate autocompletion for all commands.
 *
 * @author Jérôme Tamarelle <jerome@tamarelle.net>
 */
class CompletionFunctionalTest extends TestCase
{
    /**
     * @return iterable<array<string|string[]|null>>
     */
    public static function getCommandSuggestions(): iterable
    {
        $randomVendor = 'a/';
        $installedPackages = ['composer/semver', 'psr/log'];
        $preferInstall = ['dist', 'source', 'auto'];

        yield ['archive ', [$randomVendor]];
        yield ['archive symfony/http-', ['symfony/http-kernel', 'symfony/http-foundation']];
        yield ['archive --format ', ['tar', 'zip']];

        yield ['create-project ', [$randomVendor]];
        yield ['create-project symfony/skeleton --prefer-install ', $preferInstall];

        yield ['depends ', $installedPackages];
        yield ['why ', $installedPackages];

        yield ['exec ', ['composer', 'jsonlint', 'phpstan', 'phpstan.phar', 'simple-phpunit', 'validate-json']];

        yield ['browse ', $installedPackages];
        yield ['home -H ', $installedPackages];

        yield ['init --require ', [$randomVendor]];
        yield ['init --require-dev foo/bar --require-dev ', [$randomVendor]];

        yield ['install --prefer-install ', $preferInstall];
        yield ['install ', null];

        yield ['outdated ', $installedPackages];

        yield ['prohibits ', [$randomVendor]];
        yield ['why-not symfony/http-ker', ['symfony/http-kernel']];

        yield ['reinstall --prefer-install ', $preferInstall];
        yield ['reinstall ', $installedPackages];

        yield ['remove ', $installedPackages];

        yield ['require --prefer-install ', $preferInstall];
        yield ['require ', [$randomVendor]];
        yield ['require --dev symfony/http-', ['symfony/http-kernel', 'symfony/http-foundation']];

        yield ['run-script ', ['compile', 'test', 'phpstan']];
        yield ['run-script test ', null];

        yield ['search --format ', ['text', 'json']];

        yield ['show --format ', ['text', 'json']];
        yield ['info ', $installedPackages];

        yield ['suggests ', $installedPackages];

        yield ['update --prefer-install ', $preferInstall];
        yield ['update ', $installedPackages];

        yield ['config --list ', null];
        yield ['config --editor ', null];
        yield ['config --auth ', null];

        yield ['config ', ['bin-compat', 'extra', 'extra.branch-alias', 'home', 'name', 'repositories', 'repositories.packagist.org', 'suggest', 'suggest.ext-zip', 'type', 'version']];
        yield ['config bin', ['bin-dir']]; // global setting
        yield ['config nam', ['name']];    // existing package-property
        yield ['config ver', ['version']]; // non-existing package-property
        yield ['config repo', ['repositories', 'repositories.packagist.org']];
        yield ['config repositories.', ['repositories.packagist.org']];
        yield ['config sug', ['suggest', 'suggest.ext-zip']];
        yield ['config suggest.ext-', ['suggest.ext-zip']];
        yield ['config ext', ['extra', 'extra.branch-alias', 'extra.branch-alias.dev-main']];

        // as this test does not use a fixture (yet?), the completion
        // of setting authentication settings can have varying results
        // yield ['config http-basic.', […]];

        yield ['config --unset ', ['extra', 'extra.branch-alias', 'extra.branch-alias.dev-main', 'name', 'suggest', 'suggest.ext-zip', 'type']];
        yield ['config --unset bin-dir', null]; // global setting
        yield ['config --unset nam', ['name']]; // existing package-property
        yield ['config --unset version', null]; // non-existing package-property
        yield ['config --unset extra.', ['extra.branch-alias', 'extra.branch-alias.dev-main']];

        // as this test does not use a fixture (yet?), the completion
        // of unsetting authentication settings can have varying results
        // yield ['config --unset http-basic.', […]];

        yield ['config --global ', ['bin-compat', 'home', 'repositories', 'repositories.packagist.org']];
        yield ['config --global repo', ['repositories', 'repositories.packagist.org']];
        yield ['config --global repositories.', ['repositories.packagist.org']];

        // as this test does not use a fixture (yet?), the completion
        // of unsetting global settings can have varying results
        // yield ['config --global --unset ', null];

        // as this test does not use a fixture (yet?), the completion of
        // unsetting global authentication settings can have varying results
        // yield ['config --global --unset http-basic.', […]];
    }

    /**
     * @param string $input The command that is typed
     * @param string[]|null $expectedSuggestions Sample expected suggestions. Null if nothing is expected.
     */
    #[DataProvider('getCommandSuggestions')]
    #[Group('remote')]
    public function testComplete(string $input, ?array $expectedSuggestions): void
    {
        $input = explode(' ', $input);
        $commandName = array_shift($input);
        $command = $this->getApplication()->get($commandName);

        $tester = new CommandCompletionTester($command);
        $suggestions = $tester->complete($input);

        if (null === $expectedSuggestions) {
            self::assertEmpty($suggestions);

            return;
        }

        $diff = array_diff($expectedSuggestions, $suggestions);
        self::assertEmpty($diff, sprintf('Suggestions must contain "%s". Got "%s".', implode('", "', $diff), implode('", "', $suggestions)));
    }

    private function getApplication(): Application
    {
        return new Application();
    }
}