~ballot/wordpress/openstack-objectstorage-bis

« back to all changes in this revision

Viewing changes to vendor/phpunit/phpunit/src/TextUI/Command.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
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
 
 
11
/**
 
12
 * A TestRunner for the Command Line Interface (CLI)
 
13
 * PHP SAPI Module.
 
14
 *
 
15
 * @package    PHPUnit
 
16
 * @subpackage TextUI
 
17
 * @author     Sebastian Bergmann <sebastian@phpunit.de>
 
18
 * @copyright  Sebastian Bergmann <sebastian@phpunit.de>
 
19
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 
20
 * @link       http://www.phpunit.de/
 
21
 * @since      Class available since Release 3.0.0
 
22
 */
 
23
class PHPUnit_TextUI_Command
 
24
{
 
25
    /**
 
26
     * @var array
 
27
     */
 
28
    protected $arguments = array(
 
29
      'listGroups'              => false,
 
30
      'loader'                  => null,
 
31
      'useDefaultConfiguration' => true
 
32
    );
 
33
 
 
34
    /**
 
35
     * @var array
 
36
     */
 
37
    protected $options = array();
 
38
 
 
39
    /**
 
40
     * @var array
 
41
     */
 
42
    protected $longOptions = array(
 
43
      'colors' => null,
 
44
      'bootstrap=' => null,
 
45
      'configuration=' => null,
 
46
      'coverage-clover=' => null,
 
47
      'coverage-crap4j=' => null,
 
48
      'coverage-html=' => null,
 
49
      'coverage-php=' => null,
 
50
      'coverage-text==' => null,
 
51
      'coverage-xml=' => null,
 
52
      'debug' => null,
 
53
      'exclude-group=' => null,
 
54
      'filter=' => null,
 
55
      'testsuite=' => null,
 
56
      'group=' => null,
 
57
      'help' => null,
 
58
      'include-path=' => null,
 
59
      'list-groups' => null,
 
60
      'loader=' => null,
 
61
      'log-json=' => null,
 
62
      'log-junit=' => null,
 
63
      'log-tap=' => null,
 
64
      'process-isolation' => null,
 
65
      'repeat=' => null,
 
66
      'stderr' => null,
 
67
      'stop-on-error' => null,
 
68
      'stop-on-failure' => null,
 
69
      'stop-on-incomplete' => null,
 
70
      'stop-on-risky' => null,
 
71
      'stop-on-skipped' => null,
 
72
      'report-useless-tests' => null,
 
73
      'strict-coverage' => null,
 
74
      'disallow-test-output' => null,
 
75
      'enforce-time-limit' => null,
 
76
      'disallow-todo-tests' => null,
 
77
      'strict' => null,
 
78
      'tap' => null,
 
79
      'testdox' => null,
 
80
      'testdox-html=' => null,
 
81
      'testdox-text=' => null,
 
82
      'test-suffix=' => null,
 
83
      'no-configuration' => null,
 
84
      'no-globals-backup' => null,
 
85
      'printer=' => null,
 
86
      'static-backup' => null,
 
87
      'verbose' => null,
 
88
      'version' => null
 
89
    );
 
90
 
 
91
    /**
 
92
     * @var boolean
 
93
     */
 
94
    private $versionStringPrinted = false;
 
95
 
 
96
    /**
 
97
     * @param boolean $exit
 
98
     */
 
99
    public static function main($exit = true)
 
100
    {
 
101
        $command = new static;
 
102
 
 
103
        return $command->run($_SERVER['argv'], $exit);
 
104
    }
 
105
 
 
106
    /**
 
107
     * @param  array $argv
 
108
     * @param  boolean $exit
 
109
     * @return integer
 
110
     */
 
111
    public function run(array $argv, $exit = true)
 
112
    {
 
113
        $this->handleArguments($argv);
 
114
 
 
115
        $runner = $this->createRunner();
 
116
 
 
117
        if (is_object($this->arguments['test']) &&
 
118
            $this->arguments['test'] instanceof PHPUnit_Framework_Test) {
 
119
            $suite = $this->arguments['test'];
 
120
        } else {
 
121
            $suite = $runner->getTest(
 
122
                $this->arguments['test'],
 
123
                $this->arguments['testFile'],
 
124
                $this->arguments['testSuffixes']
 
125
            );
 
126
        }
 
127
 
 
128
        if ($this->arguments['listGroups']) {
 
129
            $this->printVersionString();
 
130
 
 
131
            print "Available test group(s):\n";
 
132
 
 
133
            $groups = $suite->getGroups();
 
134
            sort($groups);
 
135
 
 
136
            foreach ($groups as $group) {
 
137
                print " - $group\n";
 
138
            }
 
139
 
 
140
            if ($exit) {
 
141
                exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
 
142
            } else {
 
143
                return PHPUnit_TextUI_TestRunner::SUCCESS_EXIT;
 
144
            }
 
145
        }
 
146
 
 
147
        unset($this->arguments['test']);
 
148
        unset($this->arguments['testFile']);
 
149
 
 
150
        try {
 
151
            $result = $runner->doRun($suite, $this->arguments);
 
152
        } catch (PHPUnit_Framework_Exception $e) {
 
153
            print $e->getMessage() . "\n";
 
154
        }
 
155
 
 
156
        $ret = PHPUnit_TextUI_TestRunner::FAILURE_EXIT;
 
157
 
 
158
        if (isset($result) && $result->wasSuccessful()) {
 
159
            $ret = PHPUnit_TextUI_TestRunner::SUCCESS_EXIT;
 
160
        } elseif (!isset($result) || $result->errorCount() > 0) {
 
161
            $ret = PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT;
 
162
        }
 
163
 
 
164
        if ($exit) {
 
165
            exit($ret);
 
166
        } else {
 
167
            return $ret;
 
168
        }
 
169
    }
 
170
 
 
171
    /**
 
172
     * Create a TestRunner, override in subclasses.
 
173
     *
 
174
     * @return PHPUnit_TextUI_TestRunner
 
175
     * @since  Method available since Release 3.6.0
 
176
     */
 
177
    protected function createRunner()
 
178
    {
 
179
        return new PHPUnit_TextUI_TestRunner($this->arguments['loader']);
 
180
    }
 
181
 
 
182
    /**
 
183
     * Handles the command-line arguments.
 
184
     *
 
185
     * A child class of PHPUnit_TextUI_Command can hook into the argument
 
186
     * parsing by adding the switch(es) to the $longOptions array and point to a
 
187
     * callback method that handles the switch(es) in the child class like this
 
188
     *
 
189
     * <code>
 
190
     * <?php
 
191
     * class MyCommand extends PHPUnit_TextUI_Command
 
192
     * {
 
193
     *     public function __construct()
 
194
     *     {
 
195
     *         // my-switch won't accept a value, it's an on/off
 
196
     *         $this->longOptions['my-switch'] = 'myHandler';
 
197
     *         // my-secondswitch will accept a value - note the equals sign
 
198
     *         $this->longOptions['my-secondswitch='] = 'myOtherHandler';
 
199
     *     }
 
200
     *
 
201
     *     // --my-switch  -> myHandler()
 
202
     *     protected function myHandler()
 
203
     *     {
 
204
     *     }
 
205
     *
 
206
     *     // --my-secondswitch foo -> myOtherHandler('foo')
 
207
     *     protected function myOtherHandler ($value)
 
208
     *     {
 
209
     *     }
 
210
     *
 
211
     *     // You will also need this - the static keyword in the
 
212
     *     // PHPUnit_TextUI_Command will mean that it'll be
 
213
     *     // PHPUnit_TextUI_Command that gets instantiated,
 
214
     *     // not MyCommand
 
215
     *     public static function main($exit = true)
 
216
     *     {
 
217
     *         $command = new static;
 
218
     *
 
219
     *         return $command->run($_SERVER['argv'], $exit);
 
220
     *     }
 
221
     *
 
222
     * }
 
223
     * </code>
 
224
     *
 
225
     * @param array $argv
 
226
     */
 
227
    protected function handleArguments(array $argv)
 
228
    {
 
229
        if (defined('__PHPUNIT_PHAR__')) {
 
230
            $this->longOptions['selfupdate']  = null;
 
231
            $this->longOptions['self-update'] = null;
 
232
        }
 
233
 
 
234
        try {
 
235
            $this->options = PHPUnit_Util_Getopt::getopt(
 
236
                $argv,
 
237
                'd:c:hv',
 
238
                array_keys($this->longOptions)
 
239
            );
 
240
        } catch (PHPUnit_Framework_Exception $e) {
 
241
            $this->showError($e->getMessage());
 
242
        }
 
243
 
 
244
        foreach ($this->options[0] as $option) {
 
245
            switch ($option[0]) {
 
246
                case '--colors': {
 
247
                    $this->arguments['colors'] = true;
 
248
                    }
 
249
                break;
 
250
 
 
251
                case '--bootstrap': {
 
252
                    $this->arguments['bootstrap'] = $option[1];
 
253
                    }
 
254
                break;
 
255
 
 
256
                case 'c':
 
257
                case '--configuration': {
 
258
                    $this->arguments['configuration'] = $option[1];
 
259
                    }
 
260
                break;
 
261
 
 
262
                case '--coverage-clover': {
 
263
                    $this->arguments['coverageClover'] = $option[1];
 
264
                    }
 
265
                break;
 
266
 
 
267
                case '--coverage-crap4j': {
 
268
                    $this->arguments['coverageCrap4J'] = $option[1];
 
269
                    }
 
270
                break;
 
271
 
 
272
                case '--coverage-html': {
 
273
                    $this->arguments['coverageHtml'] = $option[1];
 
274
                    }
 
275
                break;
 
276
 
 
277
                case '--coverage-php': {
 
278
                    $this->arguments['coveragePHP'] = $option[1];
 
279
                    }
 
280
                break;
 
281
 
 
282
                case '--coverage-text': {
 
283
                    if ($option[1] === null) {
 
284
                        $option[1] = 'php://stdout';
 
285
                    }
 
286
 
 
287
                    $this->arguments['coverageText'] = $option[1];
 
288
                    $this->arguments['coverageTextShowUncoveredFiles'] = false;
 
289
                    $this->arguments['coverageTextShowOnlySummary'] = false;
 
290
                    }
 
291
                break;
 
292
 
 
293
                case '--coverage-xml': {
 
294
                    $this->arguments['coverageXml'] = $option[1];
 
295
                    }
 
296
                break;
 
297
 
 
298
                case 'd': {
 
299
                    $ini = explode('=', $option[1]);
 
300
 
 
301
                    if (isset($ini[0])) {
 
302
                        if (isset($ini[1])) {
 
303
                            ini_set($ini[0], $ini[1]);
 
304
                        } else {
 
305
                            ini_set($ini[0], true);
 
306
                        }
 
307
                    }
 
308
                    }
 
309
                break;
 
310
 
 
311
                case '--debug': {
 
312
                    $this->arguments['debug'] = true;
 
313
                    }
 
314
                break;
 
315
 
 
316
                case 'h':
 
317
                case '--help': {
 
318
                    $this->showHelp();
 
319
                    exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
 
320
                    }
 
321
                break;
 
322
 
 
323
                case '--filter': {
 
324
                    $this->arguments['filter'] = $option[1];
 
325
                    }
 
326
                break;
 
327
 
 
328
                case '--testsuite': {
 
329
                    $this->arguments['testsuite'] = $option[1];
 
330
                    }
 
331
                break;
 
332
 
 
333
                case '--group': {
 
334
                    $this->arguments['groups'] = explode(',', $option[1]);
 
335
                    }
 
336
                break;
 
337
 
 
338
                case '--exclude-group': {
 
339
                    $this->arguments['excludeGroups'] = explode(
 
340
                        ',', $option[1]
 
341
                    );
 
342
                    }
 
343
                break;
 
344
 
 
345
                case '--test-suffix': {
 
346
                    $this->arguments['testSuffixes'] = explode(
 
347
                        ',', $option[1]
 
348
                    );
 
349
                    }
 
350
                break;
 
351
 
 
352
                case '--include-path': {
 
353
                    $includePath = $option[1];
 
354
                    }
 
355
                break;
 
356
 
 
357
                case '--list-groups': {
 
358
                    $this->arguments['listGroups'] = true;
 
359
                    }
 
360
                break;
 
361
 
 
362
                case '--printer': {
 
363
                    $this->arguments['printer'] = $option[1];
 
364
                    }
 
365
                break;
 
366
 
 
367
                case '--loader': {
 
368
                    $this->arguments['loader'] = $option[1];
 
369
                    }
 
370
                break;
 
371
 
 
372
                case '--log-json': {
 
373
                    $this->arguments['jsonLogfile'] = $option[1];
 
374
                    }
 
375
                break;
 
376
 
 
377
                case '--log-junit': {
 
378
                    $this->arguments['junitLogfile'] = $option[1];
 
379
                    }
 
380
                break;
 
381
 
 
382
                case '--log-tap': {
 
383
                    $this->arguments['tapLogfile'] = $option[1];
 
384
                    }
 
385
                break;
 
386
 
 
387
                case '--process-isolation': {
 
388
                    $this->arguments['processIsolation'] = true;
 
389
                    }
 
390
                break;
 
391
 
 
392
                case '--repeat': {
 
393
                    $this->arguments['repeat'] = (int) $option[1];
 
394
                    }
 
395
                break;
 
396
 
 
397
                case '--stderr': {
 
398
                    $this->arguments['stderr'] = true;
 
399
                    }
 
400
                break;
 
401
 
 
402
                case '--stop-on-error': {
 
403
                    $this->arguments['stopOnError'] = true;
 
404
                    }
 
405
                break;
 
406
 
 
407
                case '--stop-on-failure': {
 
408
                    $this->arguments['stopOnFailure'] = true;
 
409
                    }
 
410
                break;
 
411
 
 
412
                case '--stop-on-incomplete': {
 
413
                    $this->arguments['stopOnIncomplete'] = true;
 
414
                    }
 
415
                break;
 
416
 
 
417
                case '--stop-on-risky': {
 
418
                    $this->arguments['stopOnRisky'] = true;
 
419
                    }
 
420
                break;
 
421
 
 
422
                case '--stop-on-skipped': {
 
423
                    $this->arguments['stopOnSkipped'] = true;
 
424
                    }
 
425
                break;
 
426
 
 
427
                case '--tap': {
 
428
                    $this->arguments['printer'] = 'PHPUnit_Util_Log_TAP';
 
429
                    }
 
430
                break;
 
431
 
 
432
                case '--testdox': {
 
433
                    $this->arguments['printer'] = 'PHPUnit_Util_TestDox_ResultPrinter_Text';
 
434
                    }
 
435
                break;
 
436
 
 
437
                case '--testdox-html': {
 
438
                    $this->arguments['testdoxHTMLFile'] = $option[1];
 
439
                    }
 
440
                break;
 
441
 
 
442
                case '--testdox-text': {
 
443
                    $this->arguments['testdoxTextFile'] = $option[1];
 
444
                    }
 
445
                break;
 
446
 
 
447
                case '--no-configuration': {
 
448
                    $this->arguments['useDefaultConfiguration'] = false;
 
449
                    }
 
450
                break;
 
451
 
 
452
                case '--no-globals-backup': {
 
453
                    $this->arguments['backupGlobals'] = false;
 
454
                    }
 
455
                break;
 
456
 
 
457
                case '--static-backup': {
 
458
                    $this->arguments['backupStaticAttributes'] = true;
 
459
                    }
 
460
                break;
 
461
 
 
462
                case 'v':
 
463
                case '--verbose': {
 
464
                    $this->arguments['verbose'] = true;
 
465
                    }
 
466
                break;
 
467
 
 
468
                case '--version': {
 
469
                    $this->printVersionString();
 
470
                    exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
 
471
                    }
 
472
                break;
 
473
 
 
474
                case '--report-useless-tests': {
 
475
                    $this->arguments['reportUselessTests'] = true;
 
476
                    }
 
477
                break;
 
478
 
 
479
                case '--strict-coverage': {
 
480
                    $this->arguments['strictCoverage'] = true;
 
481
                    }
 
482
                break;
 
483
 
 
484
                case '--disallow-test-output': {
 
485
                    $this->arguments['disallowTestOutput'] = true;
 
486
                    }
 
487
                break;
 
488
 
 
489
                case '--enforce-time-limit': {
 
490
                    $this->arguments['enforceTimeLimit'] = true;
 
491
                    }
 
492
                break;
 
493
 
 
494
                case '--disallow-todo-tests': {
 
495
                    $this->arguments['disallowTodoAnnotatedTests'] = true;
 
496
                    }
 
497
                break;
 
498
 
 
499
                case '--strict': {
 
500
                    $this->arguments['reportUselessTests']         = true;
 
501
                    $this->arguments['strictCoverage']             = true;
 
502
                    $this->arguments['disallowTestOutput']         = true;
 
503
                    $this->arguments['enforceTimeLimit']           = true;
 
504
                    $this->arguments['disallowTodoAnnotatedTests'] = true;
 
505
                    }
 
506
                break;
 
507
 
 
508
                case '--selfupdate':
 
509
                case '--self-update': {
 
510
                    $this->handleSelfUpdate();
 
511
                    }
 
512
                break;
 
513
 
 
514
                default: {
 
515
                    $optionName = str_replace('--', '', $option[0]);
 
516
 
 
517
                    if (isset($this->longOptions[$optionName])) {
 
518
                        $handler = $this->longOptions[$optionName];
 
519
                    } elseif (isset($this->longOptions[$optionName . '='])) {
 
520
                        $handler = $this->longOptions[$optionName . '='];
 
521
                    }
 
522
 
 
523
                    if (isset($handler) && is_callable(array($this, $handler))) {
 
524
                        $this->$handler($option[1]);
 
525
                    }
 
526
                    }
 
527
            }
 
528
        }
 
529
 
 
530
        $this->handleCustomTestSuite();
 
531
 
 
532
        if (!isset($this->arguments['test'])) {
 
533
            if (isset($this->options[1][0])) {
 
534
                $this->arguments['test'] = $this->options[1][0];
 
535
            }
 
536
 
 
537
            if (isset($this->options[1][1])) {
 
538
                $this->arguments['testFile'] = realpath($this->options[1][1]);
 
539
            } else {
 
540
                $this->arguments['testFile'] = '';
 
541
            }
 
542
 
 
543
            if (isset($this->arguments['test']) &&
 
544
                is_file($this->arguments['test']) &&
 
545
                substr($this->arguments['test'], -5, 5) != '.phpt') {
 
546
                $this->arguments['testFile'] = realpath($this->arguments['test']);
 
547
                $this->arguments['test']     = substr($this->arguments['test'], 0, strrpos($this->arguments['test'], '.'));
 
548
            }
 
549
        }
 
550
 
 
551
        if (!isset($this->arguments['testSuffixes'])) {
 
552
            $this->arguments['testSuffixes'] = array('Test.php', '.phpt');
 
553
        }
 
554
 
 
555
        if (isset($includePath)) {
 
556
            ini_set(
 
557
                'include_path',
 
558
                $includePath . PATH_SEPARATOR . ini_get('include_path')
 
559
            );
 
560
        }
 
561
 
 
562
        if ($this->arguments['loader'] !== null) {
 
563
            $this->arguments['loader'] = $this->handleLoader($this->arguments['loader']);
 
564
        }
 
565
 
 
566
        if (isset($this->arguments['configuration']) &&
 
567
            is_dir($this->arguments['configuration'])) {
 
568
            $configurationFile = $this->arguments['configuration'] .
 
569
                                 '/phpunit.xml';
 
570
 
 
571
            if (file_exists($configurationFile)) {
 
572
                $this->arguments['configuration'] = realpath(
 
573
                    $configurationFile
 
574
                );
 
575
            } elseif (file_exists($configurationFile . '.dist')) {
 
576
                $this->arguments['configuration'] = realpath(
 
577
                    $configurationFile . '.dist'
 
578
                );
 
579
            }
 
580
        } elseif (!isset($this->arguments['configuration']) &&
 
581
                 $this->arguments['useDefaultConfiguration']) {
 
582
            if (file_exists('phpunit.xml')) {
 
583
                $this->arguments['configuration'] = realpath('phpunit.xml');
 
584
            } elseif (file_exists('phpunit.xml.dist')) {
 
585
                $this->arguments['configuration'] = realpath(
 
586
                    'phpunit.xml.dist'
 
587
                );
 
588
            }
 
589
        }
 
590
 
 
591
        if (isset($this->arguments['configuration'])) {
 
592
            try {
 
593
                $configuration = PHPUnit_Util_Configuration::getInstance(
 
594
                    $this->arguments['configuration']
 
595
                );
 
596
            } catch (Exception $e) {
 
597
                print $e->getMessage() . "\n";
 
598
                exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
 
599
            }
 
600
 
 
601
            $phpunit = $configuration->getPHPUnitConfiguration();
 
602
 
 
603
            $configuration->handlePHPConfiguration();
 
604
 
 
605
            /**
 
606
             * Issue #1216
 
607
             */
 
608
            if (isset($this->arguments['bootstrap'])) {
 
609
                $this->handleBootstrap($this->arguments['bootstrap']);
 
610
            } elseif (isset($phpunit['bootstrap'])) {
 
611
                $this->handleBootstrap($phpunit['bootstrap']);
 
612
            }
 
613
 
 
614
            /**
 
615
             * Issue #657
 
616
             */
 
617
            if (isset($phpunit['stderr']) && ! isset($this->arguments['stderr'])) {
 
618
                $this->arguments['stderr'] = $phpunit['stderr'];
 
619
            }
 
620
 
 
621
            if (isset($phpunit['printerClass'])) {
 
622
                if (isset($phpunit['printerFile'])) {
 
623
                    $file = $phpunit['printerFile'];
 
624
                } else {
 
625
                    $file = '';
 
626
                }
 
627
 
 
628
                $this->arguments['printer'] = $this->handlePrinter(
 
629
                    $phpunit['printerClass'], $file
 
630
                );
 
631
            }
 
632
 
 
633
            if (isset($phpunit['testSuiteLoaderClass'])) {
 
634
                if (isset($phpunit['testSuiteLoaderFile'])) {
 
635
                    $file = $phpunit['testSuiteLoaderFile'];
 
636
                } else {
 
637
                    $file = '';
 
638
                }
 
639
 
 
640
                $this->arguments['loader'] = $this->handleLoader(
 
641
                    $phpunit['testSuiteLoaderClass'], $file
 
642
                );
 
643
            }
 
644
 
 
645
            $browsers = $configuration->getSeleniumBrowserConfiguration();
 
646
 
 
647
            if (!empty($browsers) &&
 
648
                class_exists('PHPUnit_Extensions_SeleniumTestCase')) {
 
649
                PHPUnit_Extensions_SeleniumTestCase::$browsers = $browsers;
 
650
            }
 
651
 
 
652
            if (!isset($this->arguments['test'])) {
 
653
                $testSuite = $configuration->getTestSuiteConfiguration(isset($this->arguments['testsuite']) ? $this->arguments['testsuite'] : null);
 
654
 
 
655
                if ($testSuite !== null) {
 
656
                    $this->arguments['test'] = $testSuite;
 
657
                }
 
658
            }
 
659
        } elseif (isset($this->arguments['bootstrap'])) {
 
660
            $this->handleBootstrap($this->arguments['bootstrap']);
 
661
        }
 
662
 
 
663
        if (isset($this->arguments['printer']) &&
 
664
            is_string($this->arguments['printer'])) {
 
665
            $this->arguments['printer'] = $this->handlePrinter($this->arguments['printer']);
 
666
        }
 
667
 
 
668
        if (isset($this->arguments['test']) && is_string($this->arguments['test']) && substr($this->arguments['test'], -5, 5) == '.phpt') {
 
669
            $test = new PHPUnit_Extensions_PhptTestCase($this->arguments['test']);
 
670
 
 
671
            $this->arguments['test'] = new PHPUnit_Framework_TestSuite;
 
672
            $this->arguments['test']->addTest($test);
 
673
        }
 
674
 
 
675
        if (!isset($this->arguments['test']) ||
 
676
            (isset($this->arguments['testDatabaseLogRevision']) && !isset($this->arguments['testDatabaseDSN']))) {
 
677
            $this->showHelp();
 
678
            exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
 
679
        }
 
680
    }
 
681
 
 
682
    /**
 
683
     * Handles the loading of the PHPUnit_Runner_TestSuiteLoader implementation.
 
684
     *
 
685
     * @param  string                         $loaderClass
 
686
     * @param  string                         $loaderFile
 
687
     * @return PHPUnit_Runner_TestSuiteLoader
 
688
     */
 
689
    protected function handleLoader($loaderClass, $loaderFile = '')
 
690
    {
 
691
        if (!class_exists($loaderClass, false)) {
 
692
            if ($loaderFile == '') {
 
693
                $loaderFile = PHPUnit_Util_Filesystem::classNameToFilename(
 
694
                    $loaderClass
 
695
                );
 
696
            }
 
697
 
 
698
            $loaderFile = stream_resolve_include_path($loaderFile);
 
699
 
 
700
            if ($loaderFile) {
 
701
                require $loaderFile;
 
702
            }
 
703
        }
 
704
 
 
705
        if (class_exists($loaderClass, false)) {
 
706
            $class = new ReflectionClass($loaderClass);
 
707
 
 
708
            if ($class->implementsInterface('PHPUnit_Runner_TestSuiteLoader') &&
 
709
                $class->isInstantiable()) {
 
710
                return $class->newInstance();
 
711
            }
 
712
        }
 
713
 
 
714
        if ($loaderClass == 'PHPUnit_Runner_StandardTestSuiteLoader') {
 
715
            return;
 
716
        }
 
717
 
 
718
        $this->showError(
 
719
            sprintf(
 
720
                'Could not use "%s" as loader.',
 
721
                $loaderClass
 
722
            )
 
723
        );
 
724
    }
 
725
 
 
726
    /**
 
727
     * Handles the loading of the PHPUnit_Util_Printer implementation.
 
728
     *
 
729
     * @param  string               $printerClass
 
730
     * @param  string               $printerFile
 
731
     * @return PHPUnit_Util_Printer
 
732
     */
 
733
    protected function handlePrinter($printerClass, $printerFile = '')
 
734
    {
 
735
        if (!class_exists($printerClass, false)) {
 
736
            if ($printerFile == '') {
 
737
                $printerFile = PHPUnit_Util_Filesystem::classNameToFilename(
 
738
                    $printerClass
 
739
                );
 
740
            }
 
741
 
 
742
            $printerFile = stream_resolve_include_path($printerFile);
 
743
 
 
744
            if ($printerFile) {
 
745
                require $printerFile;
 
746
            }
 
747
        }
 
748
 
 
749
        if (class_exists($printerClass)) {
 
750
            $class = new ReflectionClass($printerClass);
 
751
 
 
752
            if ($class->implementsInterface('PHPUnit_Framework_TestListener') &&
 
753
                $class->isSubclassOf('PHPUnit_Util_Printer') &&
 
754
                $class->isInstantiable()) {
 
755
                if ($class->isSubclassOf('PHPUnit_TextUI_ResultPrinter')) {
 
756
                    return $printerClass;
 
757
                }
 
758
 
 
759
                $outputStream = isset($this->arguments['stderr']) ? 'php://stderr' : null;
 
760
 
 
761
                return $class->newInstance($outputStream);
 
762
            }
 
763
        }
 
764
 
 
765
        $this->showError(
 
766
            sprintf(
 
767
                'Could not use "%s" as printer.',
 
768
                $printerClass
 
769
            )
 
770
        );
 
771
    }
 
772
 
 
773
    /**
 
774
     * Loads a bootstrap file.
 
775
     *
 
776
     * @param string $filename
 
777
     */
 
778
    protected function handleBootstrap($filename)
 
779
    {
 
780
        try {
 
781
            PHPUnit_Util_Fileloader::checkAndLoad($filename);
 
782
        } catch (PHPUnit_Framework_Exception $e) {
 
783
            $this->showError($e->getMessage());
 
784
        }
 
785
    }
 
786
 
 
787
    /**
 
788
     * @since Method available since Release 4.0.0
 
789
     */
 
790
    protected function handleSelfUpdate()
 
791
    {
 
792
        $this->printVersionString();
 
793
 
 
794
        if (!extension_loaded('openssl')) {
 
795
            print "The OpenSSL extension is not loaded.\n";
 
796
            exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
 
797
        }
 
798
 
 
799
        $remoteFilename = sprintf(
 
800
            'https://phar.phpunit.de/phpunit%s.phar',
 
801
            PHPUnit_Runner_Version::getReleaseChannel()
 
802
        );
 
803
 
 
804
        $localFilename = realpath($_SERVER['argv'][0]);
 
805
        $tempFilename  = basename($localFilename, '.phar') . '-temp.phar';
 
806
 
 
807
        // Workaround for https://bugs.php.net/bug.php?id=65538
 
808
        $caFile = dirname($tempFilename) . '/ca.pem';
 
809
        copy(__PHPUNIT_PHAR_ROOT__ . '/ca.pem', $caFile);
 
810
 
 
811
        print 'Updating the PHPUnit PHAR ... ';
 
812
 
 
813
        $options = array(
 
814
            'ssl' => array(
 
815
                'allow_self_signed' => false,
 
816
                'cafile' => $caFile,
 
817
                'verify_peer' => true
 
818
            )
 
819
        );
 
820
 
 
821
        if (PHP_VERSION_ID < 50600) {
 
822
            $options['ssl']['CN_match']        = 'phar.phpunit.de';
 
823
            $options['ssl']['SNI_server_name'] = 'phar.phpunit.de';
 
824
        }
 
825
 
 
826
        file_put_contents(
 
827
            $tempFilename,
 
828
            file_get_contents(
 
829
                $remoteFilename,
 
830
                false,
 
831
                stream_context_create($options)
 
832
            )
 
833
        );
 
834
 
 
835
        chmod($tempFilename, 0777 & ~umask());
 
836
 
 
837
        try {
 
838
            $phar = new Phar($tempFilename);
 
839
            unset($phar);
 
840
            rename($tempFilename, $localFilename);
 
841
            unlink($caFile);
 
842
        } catch (Exception $e) {
 
843
            unlink($caFile);
 
844
            unlink($tempFilename);
 
845
            print " done\n\n" . $e->getMessage() . "\n";
 
846
            exit(2);
 
847
        }
 
848
 
 
849
        print " done\n";
 
850
        exit(0);
 
851
    }
 
852
 
 
853
    /**
 
854
     * Show the help message.
 
855
     */
 
856
    protected function showHelp()
 
857
    {
 
858
        $this->printVersionString();
 
859
 
 
860
        print <<<EOT
 
861
Usage: phpunit [options] UnitTest [UnitTest.php]
 
862
       phpunit [options] <directory>
 
863
 
 
864
Code Coverage Options:
 
865
 
 
866
  --coverage-clover <file>  Generate code coverage report in Clover XML format.
 
867
  --coverage-crap4j <file>  Generate code coverage report in Crap4J XML format.
 
868
  --coverage-html <dir>     Generate code coverage report in HTML format.
 
869
  --coverage-php <file>     Export PHP_CodeCoverage object to file.
 
870
  --coverage-text=<file>    Generate code coverage report in text format.
 
871
                            Default: Standard output.
 
872
  --coverage-xml <dir>      Generate code coverage report in PHPUnit XML format.
 
873
 
 
874
Logging Options:
 
875
 
 
876
  --log-junit <file>        Log test execution in JUnit XML format to file.
 
877
  --log-tap <file>          Log test execution in TAP format to file.
 
878
  --log-json <file>         Log test execution in JSON format.
 
879
  --testdox-html <file>     Write agile documentation in HTML format to file.
 
880
  --testdox-text <file>     Write agile documentation in Text format to file.
 
881
 
 
882
Test Selection Options:
 
883
 
 
884
  --filter <pattern>        Filter which tests to run.
 
885
  --testsuite <pattern>     Filter which testsuite to run.
 
886
  --group ...               Only runs tests from the specified group(s).
 
887
  --exclude-group ...       Exclude tests from the specified group(s).
 
888
  --list-groups             List available test groups.
 
889
  --test-suffix ...         Only search for test in files with specified
 
890
                            suffix(es). Default: Test.php,.phpt
 
891
 
 
892
Test Execution Options:
 
893
 
 
894
  --report-useless-tests    Be strict about tests that do not test anything.
 
895
  --strict-coverage         Be strict about unintentionally covered code.
 
896
  --disallow-test-output    Be strict about output during tests.
 
897
  --enforce-time-limit      Enforce time limit based on test size.
 
898
  --disallow-todo-tests     Disallow @todo-annotated tests.
 
899
  --strict                  Run tests in strict mode (enables all of the above).
 
900
 
 
901
  --process-isolation       Run each test in a separate PHP process.
 
902
  --no-globals-backup       Do not backup and restore \$GLOBALS for each test.
 
903
  --static-backup           Backup and restore static attributes for each test.
 
904
 
 
905
  --colors                  Use colors in output.
 
906
  --stderr                  Write to STDERR instead of STDOUT.
 
907
  --stop-on-error           Stop execution upon first error.
 
908
  --stop-on-failure         Stop execution upon first error or failure.
 
909
  --stop-on-risky           Stop execution upon first risky test.
 
910
  --stop-on-skipped         Stop execution upon first skipped test.
 
911
  --stop-on-incomplete      Stop execution upon first incomplete test.
 
912
  -v|--verbose              Output more verbose information.
 
913
  --debug                   Display debugging information during test execution.
 
914
 
 
915
  --loader <loader>         TestSuiteLoader implementation to use.
 
916
  --repeat <times>          Runs the test(s) repeatedly.
 
917
  --tap                     Report test execution progress in TAP format.
 
918
  --testdox                 Report test execution progress in TestDox format.
 
919
  --printer <printer>       TestListener implementation to use.
 
920
 
 
921
Configuration Options:
 
922
 
 
923
  --bootstrap <file>        A "bootstrap" PHP file that is run before the tests.
 
924
  -c|--configuration <file> Read configuration from XML file.
 
925
  --no-configuration        Ignore default configuration file (phpunit.xml).
 
926
  --include-path <path(s)>  Prepend PHP's include_path with given path(s).
 
927
  -d key[=value]            Sets a php.ini value.
 
928
 
 
929
Miscellaneous Options:
 
930
 
 
931
  -h|--help                 Prints this usage information.
 
932
  --version                 Prints the version and exits.
 
933
 
 
934
EOT;
 
935
 
 
936
        if (defined('__PHPUNIT_PHAR__')) {
 
937
            print "\n  --self-update             Update PHPUnit to the latest version.\n";
 
938
        }
 
939
    }
 
940
 
 
941
    /**
 
942
     * Custom callback for test suite discovery.
 
943
     */
 
944
    protected function handleCustomTestSuite()
 
945
    {
 
946
    }
 
947
 
 
948
    private function printVersionString()
 
949
    {
 
950
        if ($this->versionStringPrinted) {
 
951
            return;
 
952
        }
 
953
 
 
954
        print PHPUnit_Runner_Version::getVersionString() . "\n\n";
 
955
 
 
956
        $this->versionStringPrinted = true;
 
957
    }
 
958
 
 
959
    /**
 
960
     */
 
961
    private function showError($message)
 
962
    {
 
963
        $this->printVersionString();
 
964
 
 
965
        print $message . "\n";
 
966
 
 
967
        exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
 
968
    }
 
969
}