Subversion-Projekte lars-tiefland.laravel_shop

Revision

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