Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 621 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\TextUI;
11
 
12
use const PHP_EOL;
13
use function count;
14
use function explode;
15
use function max;
16
use function preg_replace_callback;
17
use function str_pad;
18
use function str_repeat;
19
use function strlen;
20
use function wordwrap;
21
use PHPUnit\Util\Color;
22
use SebastianBergmann\Environment\Console;
23
 
24
/**
25
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
26
 */
27
final class Help
28
{
29
    private const LEFT_MARGIN = '  ';
991 lars 30
    private const HELP_TEXT   = [
621 lars 31
        'Usage' => [
148 lars 32
            ['text' => 'phpunit [options] UnitTest.php'],
33
            ['text' => 'phpunit [options] <directory>'],
34
        ],
35
 
621 lars 36
        'Code Coverage Options' => [
148 lars 37
            ['arg' => '--coverage-clover <file>', 'desc' => 'Generate code coverage report in Clover XML format'],
38
            ['arg' => '--coverage-cobertura <file>', 'desc' => 'Generate code coverage report in Cobertura XML format'],
39
            ['arg' => '--coverage-crap4j <file>', 'desc' => 'Generate code coverage report in Crap4J XML format'],
40
            ['arg' => '--coverage-html <dir>', 'desc' => 'Generate code coverage report in HTML format'],
41
            ['arg' => '--coverage-php <file>', 'desc' => 'Export PHP_CodeCoverage object to file'],
42
            ['arg' => '--coverage-text=<file>', 'desc' => 'Generate code coverage report in text format [default: standard output]'],
43
            ['arg' => '--coverage-xml <dir>', 'desc' => 'Generate code coverage report in PHPUnit XML format'],
44
            ['arg' => '--coverage-cache <dir>', 'desc' => 'Cache static analysis results'],
45
            ['arg' => '--warm-coverage-cache', 'desc' => 'Warm static analysis cache'],
46
            ['arg' => '--coverage-filter <dir>', 'desc' => 'Include <dir> in code coverage analysis'],
47
            ['arg' => '--path-coverage', 'desc' => 'Perform path coverage analysis'],
48
            ['arg' => '--disable-coverage-ignore', 'desc' => 'Disable annotations for ignoring code coverage'],
49
            ['arg' => '--no-coverage', 'desc' => 'Ignore code coverage configuration'],
50
        ],
51
 
621 lars 52
        'Logging Options' => [
148 lars 53
            ['arg' => '--log-junit <file>', 'desc' => 'Log test execution in JUnit XML format to file'],
54
            ['arg' => '--log-teamcity <file>', 'desc' => 'Log test execution in TeamCity format to file'],
55
            ['arg' => '--testdox-html <file>', 'desc' => 'Write agile documentation in HTML format to file'],
56
            ['arg' => '--testdox-text <file>', 'desc' => 'Write agile documentation in Text format to file'],
57
            ['arg' => '--testdox-xml <file>', 'desc' => 'Write agile documentation in XML format to file'],
58
            ['arg' => '--reverse-list', 'desc' => 'Print defects in reverse order'],
59
            ['arg' => '--no-logging', 'desc' => 'Ignore logging configuration'],
60
        ],
61
 
62
        'Test Selection Options' => [
63
            ['arg' => '--list-suites', 'desc' => 'List available test suites'],
64
            ['arg' => '--testsuite <name>', 'desc' => 'Filter which testsuite to run'],
65
            ['arg' => '--list-groups', 'desc' => 'List available test groups'],
66
            ['arg' => '--group <name>', 'desc' => 'Only runs tests from the specified group(s)'],
67
            ['arg' => '--exclude-group <name>', 'desc' => 'Exclude tests from the specified group(s)'],
68
            ['arg' => '--covers <name>', 'desc' => 'Only runs tests annotated with "@covers <name>"'],
69
            ['arg' => '--uses <name>', 'desc' => 'Only runs tests annotated with "@uses <name>"'],
70
            ['arg' => '--list-tests', 'desc' => 'List available tests'],
71
            ['arg' => '--list-tests-xml <file>', 'desc' => 'List available tests in XML format'],
72
            ['arg' => '--filter <pattern>', 'desc' => 'Filter which tests to run'],
73
            ['arg' => '--test-suffix <suffixes>', 'desc' => 'Only search for test in files with specified suffix(es). Default: Test.php,.phpt'],
74
        ],
75
 
76
        'Test Execution Options' => [
77
            ['arg' => '--dont-report-useless-tests', 'desc' => 'Do not report tests that do not test anything'],
78
            ['arg'    => '--strict-coverage', 'desc' => 'Be strict about @covers annotation usage'],
79
            ['arg'    => '--strict-global-state', 'desc' => 'Be strict about changes to global state'],
80
            ['arg'    => '--disallow-test-output', 'desc' => 'Be strict about output during tests'],
81
            ['arg'    => '--disallow-resource-usage', 'desc' => 'Be strict about resource usage during small tests'],
82
            ['arg'    => '--enforce-time-limit', 'desc' => 'Enforce time limit based on test size'],
83
            ['arg'    => '--default-time-limit <sec>', 'desc' => 'Timeout in seconds for tests without @small, @medium or @large'],
84
            ['arg'    => '--disallow-todo-tests', 'desc' => 'Disallow @todo-annotated tests'],
85
            ['spacer' => ''],
86
 
87
            ['arg'    => '--process-isolation', 'desc' => 'Run each test in a separate PHP process'],
88
            ['arg'    => '--globals-backup', 'desc' => 'Backup and restore $GLOBALS for each test'],
89
            ['arg'    => '--static-backup', 'desc' => 'Backup and restore static attributes for each test'],
90
            ['spacer' => ''],
91
 
92
            ['arg'    => '--colors <flag>', 'desc' => 'Use colors in output ("never", "auto" or "always")'],
93
            ['arg'    => '--columns <n>', 'desc' => 'Number of columns to use for progress output'],
94
            ['arg'    => '--columns max', 'desc' => 'Use maximum number of columns for progress output'],
95
            ['arg'    => '--stderr', 'desc' => 'Write to STDERR instead of STDOUT'],
96
            ['arg'    => '--stop-on-defect', 'desc' => 'Stop execution upon first not-passed test'],
97
            ['arg'    => '--stop-on-error', 'desc' => 'Stop execution upon first error'],
98
            ['arg'    => '--stop-on-failure', 'desc' => 'Stop execution upon first error or failure'],
99
            ['arg'    => '--stop-on-warning', 'desc' => 'Stop execution upon first warning'],
100
            ['arg'    => '--stop-on-risky', 'desc' => 'Stop execution upon first risky test'],
101
            ['arg'    => '--stop-on-skipped', 'desc' => 'Stop execution upon first skipped test'],
102
            ['arg'    => '--stop-on-incomplete', 'desc' => 'Stop execution upon first incomplete test'],
103
            ['arg'    => '--fail-on-incomplete', 'desc' => 'Treat incomplete tests as failures'],
104
            ['arg'    => '--fail-on-risky', 'desc' => 'Treat risky tests as failures'],
105
            ['arg'    => '--fail-on-skipped', 'desc' => 'Treat skipped tests as failures'],
106
            ['arg'    => '--fail-on-warning', 'desc' => 'Treat tests with warnings as failures'],
107
            ['arg'    => '-v|--verbose', 'desc' => 'Output more verbose information'],
108
            ['arg'    => '--debug', 'desc' => 'Display debugging information'],
109
            ['spacer' => ''],
110
 
111
            ['arg'    => '--repeat <times>', 'desc' => 'Runs the test(s) repeatedly'],
112
            ['arg'    => '--teamcity', 'desc' => 'Report test execution progress in TeamCity format'],
113
            ['arg'    => '--testdox', 'desc' => 'Report test execution progress in TestDox format'],
114
            ['arg'    => '--testdox-group', 'desc' => 'Only include tests from the specified group(s)'],
115
            ['arg'    => '--testdox-exclude-group', 'desc' => 'Exclude tests from the specified group(s)'],
116
            ['arg'    => '--no-interaction', 'desc' => 'Disable TestDox progress animation'],
117
            ['arg'    => '--printer <printer>', 'desc' => 'TestListener implementation to use'],
118
            ['spacer' => ''],
119
 
621 lars 120
            ['arg' => '--order-by <order>', 'desc' => 'Run tests in order: default|defects|duration|no-depends|random|reverse|size'],
121
            ['arg' => '--random-order-seed <N>', 'desc' => 'Use a specific random seed <N> for random order'],
122
            ['arg' => '--cache-result', 'desc' => 'Write test results to cache file'],
123
            ['arg' => '--do-not-cache-result', 'desc' => 'Do not write test results to cache file'],
148 lars 124
        ],
125
 
621 lars 126
        'Configuration Options' => [
148 lars 127
            ['arg' => '--prepend <file>', 'desc' => 'A PHP script that is included as early as possible'],
128
            ['arg' => '--bootstrap <file>', 'desc' => 'A PHP script that is included before the tests run'],
129
            ['arg' => '-c|--configuration <file>', 'desc' => 'Read configuration from XML file'],
130
            ['arg' => '--no-configuration', 'desc' => 'Ignore default configuration file (phpunit.xml)'],
131
            ['arg' => '--extensions <extensions>', 'desc' => 'A comma separated list of PHPUnit extensions to load'],
132
            ['arg' => '--no-extensions', 'desc' => 'Do not load PHPUnit extensions'],
133
            ['arg' => '--include-path <path(s)>', 'desc' => 'Prepend PHP\'s include_path with given path(s)'],
134
            ['arg' => '-d <key[=value]>', 'desc' => 'Sets a php.ini value'],
135
            ['arg' => '--cache-result-file <file>', 'desc' => 'Specify result cache path and filename'],
136
            ['arg' => '--generate-configuration', 'desc' => 'Generate configuration file with suggested settings'],
137
            ['arg' => '--migrate-configuration', 'desc' => 'Migrate configuration file to current format'],
138
        ],
139
 
621 lars 140
        'Miscellaneous Options' => [
148 lars 141
            ['arg' => '-h|--help', 'desc' => 'Prints this usage information'],
142
            ['arg' => '--version', 'desc' => 'Prints the version and exits'],
143
            ['arg' => '--atleast-version <min>', 'desc' => 'Checks that version is greater than min and exits'],
144
            ['arg' => '--check-version', 'desc' => 'Check whether PHPUnit is the latest version'],
145
        ],
146
 
147
    ];
148
 
149
    /**
150
     * @var int Number of columns required to write the longest option name to the console
151
     */
152
    private $maxArgLength = 0;
153
 
154
    /**
155
     * @var int Number of columns left for the description field after padding and option
156
     */
157
    private $maxDescLength;
158
 
159
    /**
160
     * @var bool Use color highlights for sections, options and parameters
161
     */
162
    private $hasColor = false;
163
 
164
    public function __construct(?int $width = null, ?bool $withColor = null)
165
    {
166
        if ($width === null) {
167
            $width = (new Console)->getNumberOfColumns();
168
        }
169
 
170
        if ($withColor === null) {
171
            $this->hasColor = (new Console)->hasColorSupport();
172
        } else {
173
            $this->hasColor = $withColor;
174
        }
175
 
176
        foreach (self::HELP_TEXT as $options) {
177
            foreach ($options as $option) {
178
                if (isset($option['arg'])) {
179
                    $this->maxArgLength = max($this->maxArgLength, isset($option['arg']) ? strlen($option['arg']) : 0);
180
                }
181
            }
182
        }
183
 
184
        $this->maxDescLength = $width - $this->maxArgLength - 4;
185
    }
186
 
187
    /**
188
     * Write the help file to the CLI, adapting width and colors to the console.
189
     */
190
    public function writeToConsole(): void
191
    {
192
        if ($this->hasColor) {
193
            $this->writeWithColor();
194
        } else {
195
            $this->writePlaintext();
196
        }
197
    }
198
 
199
    private function writePlaintext(): void
200
    {
201
        foreach (self::HELP_TEXT as $section => $options) {
202
            print "{$section}:" . PHP_EOL;
203
 
204
            if ($section !== 'Usage') {
205
                print PHP_EOL;
206
            }
207
 
208
            foreach ($options as $option) {
209
                if (isset($option['spacer'])) {
210
                    print PHP_EOL;
211
                }
212
 
213
                if (isset($option['text'])) {
214
                    print self::LEFT_MARGIN . $option['text'] . PHP_EOL;
215
                }
216
 
217
                if (isset($option['arg'])) {
218
                    $arg = str_pad($option['arg'], $this->maxArgLength);
219
                    print self::LEFT_MARGIN . $arg . ' ' . $option['desc'] . PHP_EOL;
220
                }
221
            }
222
 
223
            print PHP_EOL;
224
        }
225
    }
226
 
227
    private function writeWithColor(): void
228
    {
229
        foreach (self::HELP_TEXT as $section => $options) {
230
            print Color::colorize('fg-yellow', "{$section}:") . PHP_EOL;
231
 
232
            foreach ($options as $option) {
233
                if (isset($option['spacer'])) {
234
                    print PHP_EOL;
235
                }
236
 
237
                if (isset($option['text'])) {
238
                    print self::LEFT_MARGIN . $option['text'] . PHP_EOL;
239
                }
240
 
241
                if (isset($option['arg'])) {
242
                    $arg = Color::colorize('fg-green', str_pad($option['arg'], $this->maxArgLength));
243
                    $arg = preg_replace_callback(
244
                        '/(<[^>]+>)/',
245
                        static function ($matches)
246
                        {
247
                            return Color::colorize('fg-cyan', $matches[0]);
248
                        },
249
                        $arg
250
                    );
251
                    $desc = explode(PHP_EOL, wordwrap($option['desc'], $this->maxDescLength, PHP_EOL));
252
 
253
                    print self::LEFT_MARGIN . $arg . ' ' . $desc[0] . PHP_EOL;
254
 
255
                    for ($i = 1; $i < count($desc); $i++) {
256
                        print str_repeat(' ', $this->maxArgLength + 3) . $desc[$i] . PHP_EOL;
257
                    }
258
                }
259
            }
260
 
261
            print PHP_EOL;
262
        }
263
    }
264
}