Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PHPUnit
4
 *
5
 * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 *   * Redistributions of source code must retain the above copyright
13
 *     notice, this list of conditions and the following disclaimer.
14
 *
15
 *   * Redistributions in binary form must reproduce the above copyright
16
 *     notice, this list of conditions and the following disclaimer in
17
 *     the documentation and/or other materials provided with the
18
 *     distribution.
19
 *
20
 *   * Neither the name of Sebastian Bergmann nor the names of his
21
 *     contributors may be used to endorse or promote products derived
22
 *     from this software without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @category   Testing
38
 * @package    PHPUnit
39
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @link       http://www.phpunit.de/
43
 * @since      File available since Release 3.0.0
44
 */
45
 
46
require_once 'PHPUnit/TextUI/TestRunner.php';
47
require_once 'PHPUnit/Util/Configuration.php';
48
require_once 'PHPUnit/Util/Fileloader.php';
49
require_once 'PHPUnit/Util/Filesystem.php';
50
require_once 'PHPUnit/Util/Filter.php';
51
require_once 'PHPUnit/Util/Getopt.php';
52
 
53
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
54
 
55
/**
56
 * A TestRunner for the Command Line Interface (CLI)
57
 * PHP SAPI Module.
58
 *
59
 * @category   Testing
60
 * @package    PHPUnit
61
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
62
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
63
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
64
 * @version    Release: 3.4.15
65
 * @link       http://www.phpunit.de/
66
 * @since      Class available since Release 3.0.0
67
 */
68
class PHPUnit_TextUI_Command
69
{
70
    /**
71
     * @var array
72
     */
73
    protected $arguments = array(
74
      'listGroups'              => FALSE,
75
      'loader'                  => NULL,
76
      'syntaxCheck'             => FALSE,
77
      'useDefaultConfiguration' => TRUE
78
    );
79
 
80
    /**
81
     * @var array
82
     */
83
    protected $options = array();
84
 
85
    /**
86
     * @var array
87
     */
88
    protected $longOptions = array(
89
      'ansi' => NULL,
90
      'colors' => NULL,
91
      'bootstrap=' => NULL,
92
      'configuration=' => NULL,
93
      'coverage-html=' => NULL,
94
      'coverage-clover=' => NULL,
95
      'coverage-source=' => NULL,
96
      'coverage-xml=' => NULL,
97
      'debug' => NULL,
98
      'exclude-group=' => NULL,
99
      'filter=' => NULL,
100
      'group=' => NULL,
101
      'help' => NULL,
102
      'include-path=' => NULL,
103
      'list-groups' => NULL,
104
      'loader=' => NULL,
105
      'log-graphviz=' => NULL,
106
      'log-json=' => NULL,
107
      'log-junit=' => NULL,
108
      'log-metrics=' => NULL,
109
      'log-pmd=' => NULL,
110
      'log-tap=' => NULL,
111
      'log-xml=' => NULL,
112
      'process-isolation' => NULL,
113
      'repeat=' => NULL,
114
      'report=' => NULL,
115
      'skeleton' => NULL,
116
      'skeleton-class' => NULL,
117
      'skeleton-test' => NULL,
118
      'stderr' => NULL,
119
      'stop-on-failure' => NULL,
120
      'story' => NULL,
121
      'story-html=' => NULL,
122
      'story-text=' => NULL,
123
      'syntax-check' => NULL,
124
      'tap' => NULL,
125
      'test-db-dsn=' => NULL,
126
      'test-db-log-rev=' => NULL,
127
      'test-db-log-prefix=' => NULL,
128
      'test-db-log-info=' => NULL,
129
      'testdox' => NULL,
130
      'testdox-html=' => NULL,
131
      'testdox-text=' => NULL,
132
      'no-configuration' => NULL,
133
      'no-globals-backup' => NULL,
134
      'static-backup' => NULL,
135
      'verbose' => NULL,
136
      'version' => NULL,
137
      'wait' => NULL
138
    );
139
 
140
    /**
141
     * @param boolean $exit
142
     */
143
    public static function main($exit = TRUE)
144
    {
145
        $command = new PHPUnit_TextUI_Command;
146
        $command->run($_SERVER['argv'], $exit);
147
    }
148
 
149
    /**
150
     * @param array   $argv
151
     * @param boolean $exit
152
     */
153
    public function run(array $argv, $exit = TRUE)
154
    {
155
        $this->handleArguments($argv);
156
 
157
        $runner = new PHPUnit_TextUI_TestRunner($this->arguments['loader']);
158
 
159
        if (is_object($this->arguments['test']) &&
160
            $this->arguments['test'] instanceof PHPUnit_Framework_Test) {
161
            $suite = $this->arguments['test'];
162
        } else {
163
            $suite = $runner->getTest(
164
              $this->arguments['test'],
165
              $this->arguments['testFile'],
166
              $this->arguments['syntaxCheck']
167
            );
168
        }
169
 
170
        if ($suite->testAt(0) instanceof PHPUnit_Framework_Warning &&
171
            strpos($suite->testAt(0)->getMessage(), 'No tests found in class') !== FALSE) {
172
            $message   = $suite->testAt(0)->getMessage();
173
            $start     = strpos($message, '"') + 1;
174
            $end       = strpos($message, '"', $start);
175
            $className = substr($message, $start, $end - $start);
176
 
177
            require_once 'PHPUnit/Util/Skeleton/Test.php';
178
 
179
            $skeleton = new PHPUnit_Util_Skeleton_Test(
180
              $className,
181
              $this->arguments['testFile']
182
            );
183
 
184
            $result = $skeleton->generate(TRUE);
185
 
186
            if (!$result['incomplete']) {
187
                eval(str_replace(array('<?php', '?>'), '', $result['code']));
188
                $suite = new PHPUnit_Framework_TestSuite(
189
                  $this->arguments['test'] . 'Test'
190
                );
191
            }
192
        }
193
 
194
        if ($this->arguments['listGroups']) {
195
            PHPUnit_TextUI_TestRunner::printVersionString();
196
 
197
            print "Available test group(s):\n";
198
 
199
            $groups = $suite->getGroups();
200
            sort($groups);
201
 
202
            foreach ($groups as $group) {
203
                print " - $group\n";
204
            }
205
 
206
            exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
207
        }
208
 
209
        unset($this->arguments['test']);
210
        unset($this->arguments['testFile']);
211
 
212
        try {
213
            $result = $runner->doRun($suite, $this->arguments);
214
        }
215
 
216
        catch (PHPUnit_Framework_Exception $e) {
217
            print $e->getMessage() . "\n";
218
        }
219
 
220
        if ($exit) {
221
            if (isset($result) && $result->wasSuccessful()) {
222
                exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
223
            }
224
 
225
            else if (!isset($result) || $result->errorCount() > 0) {
226
                exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
227
            }
228
 
229
            else {
230
                exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
231
            }
232
        }
233
    }
234
 
235
    /**
236
     * Handles the command-line arguments.
237
     *
238
     * A child class of PHPUnit_TextUI_Command can hook into the argument
239
     * parsing by adding the switch(es) to the $longOptions array and point to a
240
     * callback method that handles the switch(es) in the child class like this
241
     *
242
     * <code>
243
     * <?php
244
     * class MyCommand extends PHPUnit_TextUI_Command
245
     * {
246
     *     public function __construct()
247
     *     {
248
     *         $this->longOptions['--my-switch'] = 'myHandler';
249
     *     }
250
     *
251
     *     // --my-switch foo -> myHandler('foo')
252
     *     protected function myHandler($value)
253
     *     {
254
     *     }
255
     * }
256
     * </code>
257
     *
258
     * @param array $argv
259
     */
260
    protected function handleArguments(array $argv)
261
    {
262
        try {
263
            $this->options = PHPUnit_Util_Getopt::getopt(
264
              $argv,
265
              'd:',
266
              array_keys($this->longOptions)
267
            );
268
        }
269
 
270
        catch (RuntimeException $e) {
271
            PHPUnit_TextUI_TestRunner::showError($e->getMessage());
272
        }
273
 
274
        $skeletonClass = FALSE;
275
        $skeletonTest  = FALSE;
276
 
277
        foreach ($this->options[0] as $option) {
278
            switch ($option[0]) {
279
                case '--ansi': {
280
                    $this->showMessage(
281
                      'The --ansi option is deprecated, please use --colors ' .
282
                      'instead.',
283
                      FALSE
284
                    );
285
                }
286
 
287
                case '--colors': {
288
                    $this->arguments['colors'] = TRUE;
289
                }
290
                break;
291
 
292
                case '--bootstrap': {
293
                    $this->arguments['bootstrap'] = $option[1];
294
                }
295
                break;
296
 
297
                case '--configuration': {
298
                    $this->arguments['configuration'] = $option[1];
299
                }
300
                break;
301
 
302
                case '--coverage-xml': {
303
                    $this->showMessage(
304
                      'The --coverage-xml option is deprecated, please use ' .
305
                      '--coverage-clover instead.',
306
                      FALSE
307
                    );
308
                }
309
 
310
                case '--coverage-clover': {
311
                    if (extension_loaded('tokenizer') &&
312
                        extension_loaded('xdebug')) {
313
                        $this->arguments['coverageClover'] = $option[1];
314
                    } else {
315
                        if (!extension_loaded('tokenizer')) {
316
                            $this->showMessage(
317
                              'The tokenizer extension is not loaded.'
318
                            );
319
                        } else {
320
                            $this->showMessage(
321
                              'The Xdebug extension is not loaded.'
322
                            );
323
                        }
324
                    }
325
                }
326
                break;
327
 
328
                case '--coverage-source': {
329
                    if (extension_loaded('tokenizer') &&
330
                        extension_loaded('xdebug')) {
331
                        $this->arguments['coverageSource'] = $option[1];
332
                    } else {
333
                        if (!extension_loaded('tokenizer')) {
334
                            $this->showMessage(
335
                              'The tokenizer extension is not loaded.'
336
                            );
337
                        } else {
338
                            $this->showMessage(
339
                              'The Xdebug extension is not loaded.'
340
                            );
341
                        }
342
                    }
343
                }
344
                break;
345
 
346
                case '--report': {
347
                    $this->showMessage(
348
                      'The --report option is deprecated, please use ' .
349
                      '--coverage-html instead.',
350
                      FALSE
351
                    );
352
                }
353
 
354
                case '--coverage-html': {
355
                    if (extension_loaded('tokenizer') &&
356
                        extension_loaded('xdebug')) {
357
                        $this->arguments['reportDirectory'] = $option[1];
358
                    } else {
359
                        if (!extension_loaded('tokenizer')) {
360
                            $this->showMessage(
361
                              'The tokenizer extension is not loaded.'
362
                            );
363
                        } else {
364
                            $this->showMessage(
365
                              'The Xdebug extension is not loaded.'
366
                            );
367
                        }
368
                    }
369
                }
370
                break;
371
 
372
                case 'd': {
373
                    $ini = explode('=', $option[1]);
374
 
375
                    if (isset($ini[0])) {
376
                        if (isset($ini[1])) {
377
                            ini_set($ini[0], $ini[1]);
378
                        } else {
379
                            ini_set($ini[0], TRUE);
380
                        }
381
                    }
382
                }
383
                break;
384
 
385
                case '--debug': {
386
                    $this->arguments['debug'] = TRUE;
387
                }
388
                break;
389
 
390
                case '--help': {
391
                    $this->showHelp();
392
                    exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
393
                }
394
                break;
395
 
396
                case '--filter': {
397
                    $this->arguments['filter'] = $option[1];
398
                }
399
                break;
400
 
401
                case '--group': {
402
                    $this->arguments['groups'] = explode(',', $option[1]);
403
                }
404
                break;
405
 
406
                case '--exclude-group': {
407
                    $this->arguments['excludeGroups'] = explode(
408
                      ',', $option[1]
409
                    );
410
                }
411
                break;
412
 
413
                case '--include-path': {
414
                    $includePath = $option[1];
415
                }
416
                break;
417
 
418
                case '--list-groups': {
419
                    $this->arguments['listGroups'] = TRUE;
420
                }
421
                break;
422
 
423
                case '--loader': {
424
                    $this->arguments['loader'] = $option[1];
425
                }
426
                break;
427
 
428
                case '--log-json': {
429
                    $this->arguments['jsonLogfile'] = $option[1];
430
                }
431
                break;
432
 
433
                case '--log-xml': {
434
                    $this->showMessage(
435
                      'The --log-xml option is deprecated, please use ' .
436
                      '--log-junit instead.',
437
                      FALSE
438
                    );
439
                }
440
 
441
                case '--log-junit': {
442
                    $this->arguments['junitLogfile'] = $option[1];
443
                }
444
                break;
445
 
446
                case '--log-graphviz': {
447
                    $this->showMessage(
448
                      'The --log-graphviz functionality is deprecated and ' .
449
                      'will be removed in the future.',
450
                      FALSE
451
                    );
452
 
453
                    if (PHPUnit_Util_Filesystem::fileExistsInIncludePath('Image/GraphViz.php')) {
454
                        $this->arguments['graphvizLogfile'] = $option[1];
455
                    } else {
456
                        $this->showMessage(
457
                          'The Image_GraphViz package is not installed.'
458
                        );
459
                    }
460
                }
461
                break;
462
 
463
                case '--log-tap': {
464
                    $this->arguments['tapLogfile'] = $option[1];
465
                }
466
                break;
467
 
468
                case '--log-pmd': {
469
                    $this->showMessage(
470
                      'The --log-pmd functionality is deprecated and will be ' .
471
                      'removed in the future.',
472
                      FALSE
473
                    );
474
 
475
                    if (extension_loaded('tokenizer') &&
476
                        extension_loaded('xdebug')) {
477
                        $this->arguments['pmdXML'] = $option[1];
478
                    } else {
479
                        if (!extension_loaded('tokenizer')) {
480
                            $this->showMessage(
481
                              'The tokenizer extension is not loaded.'
482
                            );
483
                        } else {
484
                            $this->showMessage(
485
                              'The Xdebug extension is not loaded.'
486
                            );
487
                        }
488
                    }
489
                }
490
                break;
491
 
492
                case '--log-metrics': {
493
                    $this->showMessage(
494
                      'The --log-metrics functionality is deprecated and ' .
495
                      'will be removed in the future.',
496
                      FALSE
497
                    );
498
 
499
                    if (extension_loaded('tokenizer') &&
500
                        extension_loaded('xdebug')) {
501
                        $this->arguments['metricsXML'] = $option[1];
502
                    } else {
503
                        if (!extension_loaded('tokenizer')) {
504
                            $this->showMessage(
505
                              'The tokenizer extension is not loaded.'
506
                            );
507
                        } else {
508
                            $this->showMessage(
509
                              'The Xdebug extension is not loaded.'
510
                            );
511
                        }
512
                    }
513
                }
514
                break;
515
 
516
                case '--process-isolation': {
517
                    $this->arguments['processIsolation'] = TRUE;
518
                    $this->arguments['syntaxCheck']      = FALSE;
519
                }
520
                break;
521
 
522
                case '--repeat': {
523
                    $this->arguments['repeat'] = (int)$option[1];
524
                }
525
                break;
526
 
527
                case '--stderr': {
528
                    $this->arguments['printer'] = new PHPUnit_TextUI_ResultPrinter(
529
                      'php://stderr',
530
                      isset($this->arguments['verbose']) ? $this->arguments['verbose'] : FALSE
531
                    );
532
                }
533
                break;
534
 
535
                case '--stop-on-failure': {
536
                    $this->arguments['stopOnFailure'] = TRUE;
537
                }
538
                break;
539
 
540
                case '--test-db-dsn': {
541
                    $this->showMessage(
542
                      'The test database functionality is deprecated and ' .
543
                      'will be removed in the future.',
544
                      FALSE
545
                    );
546
 
547
                    if (extension_loaded('pdo')) {
548
                        $this->arguments['testDatabaseDSN'] = $option[1];
549
                    } else {
550
                        $this->showMessage('The PDO extension is not loaded.');
551
                    }
552
                }
553
                break;
554
 
555
                case '--test-db-log-rev': {
556
                    if (extension_loaded('pdo')) {
557
                        $this->arguments['testDatabaseLogRevision'] = $option[1];
558
                    } else {
559
                        $this->showMessage('The PDO extension is not loaded.');
560
                    }
561
                }
562
                break;
563
 
564
                case '--test-db-prefix': {
565
                    if (extension_loaded('pdo')) {
566
                        $this->arguments['testDatabasePrefix'] = $option[1];
567
                    } else {
568
                        $this->showMessage('The PDO extension is not loaded.');
569
                    }
570
                }
571
                break;
572
 
573
                case '--test-db-log-info': {
574
                    if (extension_loaded('pdo')) {
575
                        $this->arguments['testDatabaseLogInfo'] = $option[1];
576
                    } else {
577
                        $this->showMessage('The PDO extension is not loaded.');
578
                    }
579
                }
580
                break;
581
 
582
                case '--skeleton': {
583
                    $this->showMessage(
584
                      'The --skeleton option is deprecated, please use ' .
585
                      '--skeleton-test instead.',
586
                      FALSE
587
                    );
588
                }
589
 
590
                case '--skeleton-test': {
591
                    $skeletonTest  = TRUE;
592
                    $skeletonClass = FALSE;
593
                }
594
                break;
595
 
596
                case '--skeleton-class': {
597
                    $skeletonClass = TRUE;
598
                    $skeletonTest  = FALSE;
599
                }
600
                break;
601
 
602
                case '--tap': {
603
                    require_once 'PHPUnit/Util/Log/TAP.php';
604
 
605
                    $this->arguments['printer'] = new PHPUnit_Util_Log_TAP;
606
                }
607
                break;
608
 
609
                case '--story': {
610
                    require_once 'PHPUnit/Extensions/Story/ResultPrinter/Text.php';
611
 
612
                    $this->arguments['printer'] = new PHPUnit_Extensions_Story_ResultPrinter_Text;
613
                }
614
                break;
615
 
616
                case '--story-html': {
617
                    $this->arguments['storyHTMLFile'] = $option[1];
618
                }
619
                break;
620
 
621
                case '--story-text': {
622
                    $this->arguments['storyTextFile'] = $option[1];
623
                }
624
                break;
625
 
626
                case '--syntax-check': {
627
                    $this->arguments['syntaxCheck'] = TRUE;
628
                }
629
                break;
630
 
631
                case '--testdox': {
632
                    require_once 'PHPUnit/Util/TestDox/ResultPrinter/Text.php';
633
 
634
                    $this->arguments['printer'] = new PHPUnit_Util_TestDox_ResultPrinter_Text;
635
                }
636
                break;
637
 
638
                case '--testdox-html': {
639
                    $this->arguments['testdoxHTMLFile'] = $option[1];
640
                }
641
                break;
642
 
643
                case '--testdox-text': {
644
                    $this->arguments['testdoxTextFile'] = $option[1];
645
                }
646
                break;
647
 
648
                case '--no-configuration': {
649
                    $this->arguments['useDefaultConfiguration'] = FALSE;
650
                }
651
                break;
652
 
653
                case '--no-globals-backup': {
654
                    $this->arguments['backupGlobals'] = FALSE;
655
                }
656
                break;
657
 
658
                case '--static-backup': {
659
                    $this->arguments['backupStaticAttributes'] = TRUE;
660
                }
661
                break;
662
 
663
                case '--verbose': {
664
                    $this->arguments['verbose'] = TRUE;
665
                }
666
                break;
667
 
668
                case '--version': {
669
                    PHPUnit_TextUI_TestRunner::printVersionString();
670
                    exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
671
                }
672
                break;
673
 
674
                case '--wait': {
675
                    $this->arguments['wait'] = TRUE;
676
                }
677
                break;
678
 
679
                default: {
680
                    $optionName = str_replace('--', '', $option[0]);
681
 
682
                    if (isset($this->longOptions[$optionName])) {
683
                        $handler = $this->longOptions[$optionName];
684
                    }
685
 
686
                    else if (isset($this->longOptions[$optionName . '='])) {
687
                        $handler = $this->longOptions[$optionName . '='];
688
                    }
689
 
690
                    if (isset($handler) && is_callable(array($this, $handler))) {
691
                        $this->$handler($option[1]);
692
                    }
693
                }
694
            }
695
        }
696
 
697
        if (isset($this->arguments['printer']) &&
698
            $this->arguments['printer'] instanceof PHPUnit_Extensions_Story_ResultPrinter_Text &&
699
            isset($this->arguments['processIsolation']) &&
700
            $this->arguments['processIsolation']) {
701
            $this->showMessage(
702
              'The story result printer cannot be used in process isolation.'
703
            );
704
        }
705
 
706
        $this->handleCustomTestSuite();
707
 
708
        if (!isset($this->arguments['test'])) {
709
            if (isset($this->options[1][0])) {
710
                $this->arguments['test'] = $this->options[1][0];
711
            }
712
 
713
            if (isset($this->options[1][1])) {
714
                $this->arguments['testFile'] = $this->options[1][1];
715
            } else {
716
                $this->arguments['testFile'] = '';
717
            }
718
 
719
            if (isset($this->arguments['test']) && is_file($this->arguments['test'])) {
720
                $this->arguments['testFile'] = realpath($this->arguments['test']);
721
                $this->arguments['test']     = substr($this->arguments['test'], 0, strrpos($this->arguments['test'], '.'));
722
            }
723
        }
724
 
725
        if (isset($includePath)) {
726
            ini_set(
727
              'include_path',
728
              $includePath . PATH_SEPARATOR . ini_get('include_path')
729
            );
730
        }
731
 
732
        if (isset($this->arguments['bootstrap'])) {
733
            PHPUnit_Util_Fileloader::load($this->arguments['bootstrap']);
734
        }
735
 
736
        if ($this->arguments['loader'] !== NULL) {
737
            $this->arguments['loader'] = $this->handleLoader($this->arguments['loader']);
738
        }
739
 
740
        if (!isset($this->arguments['configuration']) && $this->arguments['useDefaultConfiguration']) {
741
            if (file_exists('phpunit.xml')) {
742
                $this->arguments['configuration'] = realpath('phpunit.xml');
743
            }
744
 
745
            else if (file_exists('phpunit.xml.dist')) {
746
                $this->arguments['configuration'] = realpath('phpunit.xml.dist');
747
            }
748
        }
749
 
750
        if (isset($this->arguments['configuration'])) {
751
            $configuration = PHPUnit_Util_Configuration::getInstance(
752
              $this->arguments['configuration']
753
            );
754
 
755
            $phpunit = $configuration->getPHPUnitConfiguration();
756
 
757
            if (isset($phpunit['syntaxCheck'])) {
758
                $this->arguments['syntaxCheck'] = $phpunit['syntaxCheck'];
759
            }
760
 
761
            if (isset($phpunit['testSuiteLoaderClass'])) {
762
                if (isset($phpunit['testSuiteLoaderFile'])) {
763
                    $file = $phpunit['testSuiteLoaderFile'];
764
                } else {
765
                    $file = '';
766
                }
767
 
768
                $this->arguments['loader'] = $this->handleLoader(
769
                  $phpunit['testSuiteLoaderClass'], $file
770
                );
771
            }
772
 
773
            $configuration->handlePHPConfiguration();
774
 
775
            if (!isset($this->arguments['bootstrap'])) {
776
                $phpunitConfiguration = $configuration->getPHPUnitConfiguration();
777
 
778
                if (isset($phpunitConfiguration['bootstrap'])) {
779
                    PHPUnit_Util_Fileloader::load($phpunitConfiguration['bootstrap']);
780
                }
781
            }
782
 
783
            $browsers = $configuration->getSeleniumBrowserConfiguration();
784
 
785
            if (!empty($browsers)) {
786
                require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
787
                PHPUnit_Extensions_SeleniumTestCase::$browsers = $browsers;
788
            }
789
 
790
            if (!isset($this->arguments['test'])) {
791
                $testSuite = $configuration->getTestSuiteConfiguration(
792
                  $this->arguments['syntaxCheck']
793
                );
794
 
795
                if ($testSuite !== NULL) {
796
                    $this->arguments['test'] = $testSuite;
797
                }
798
            }
799
        }
800
 
801
        if (isset($this->arguments['test']) && is_string($this->arguments['test']) && substr($this->arguments['test'], -5, 5) == '.phpt') {
802
            require_once 'PHPUnit/Extensions/PhptTestCase.php';
803
 
804
            $test = new PHPUnit_Extensions_PhptTestCase($this->arguments['test']);
805
 
806
            $this->arguments['test'] = new PHPUnit_Framework_TestSuite;
807
            $this->arguments['test']->addTest($test);
808
        }
809
 
810
        if (!isset($this->arguments['test']) ||
811
            (isset($this->arguments['testDatabaseLogRevision']) && !isset($this->arguments['testDatabaseDSN']))) {
812
            $this->showHelp();
813
            exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
814
        }
815
 
816
        if (!isset($this->arguments['syntaxCheck'])) {
817
            $this->arguments['syntaxCheck'] = FALSE;
818
        }
819
 
820
        if ($skeletonClass || $skeletonTest) {
821
            if (isset($this->arguments['test']) && $this->arguments['test'] !== FALSE) {
822
                PHPUnit_TextUI_TestRunner::printVersionString();
823
 
824
                if ($skeletonClass) {
825
                    require_once 'PHPUnit/Util/Skeleton/Class.php';
826
 
827
                    $class = 'PHPUnit_Util_Skeleton_Class';
828
                } else {
829
                    require_once 'PHPUnit/Util/Skeleton/Test.php';
830
 
831
                    $class = 'PHPUnit_Util_Skeleton_Test';
832
                }
833
 
834
                try {
835
                    $args      = array();
836
                    $reflector = new ReflectionClass($class);
837
 
838
                    for ($i = 0; $i <= 3; $i++) {
839
                        if (isset($this->options[1][$i])) {
840
                            $args[] = $this->options[1][$i];
841
                        }
842
                    }
843
 
844
                    $skeleton = $reflector->newInstanceArgs($args);
845
                    $skeleton->write();
846
                }
847
 
848
                catch (Exception $e) {
849
                    print $e->getMessage() . "\n";
850
                    exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
851
                }
852
 
853
                printf(
854
                  'Wrote skeleton for "%s" to "%s".' . "\n",
855
                  $skeleton->getOutClassName(),
856
                  $skeleton->getOutSourceFile()
857
                );
858
 
859
                exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
860
            } else {
861
                $this->showHelp();
862
                exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
863
            }
864
        }
865
    }
866
 
867
    /**
868
     * Handles the loading of the PHPUnit_Runner_TestSuiteLoader implementation.
869
     *
870
     * @param  string  $loaderClass
871
     * @param  string  $loaderFile
872
     */
873
    protected function handleLoader($loaderClass, $loaderFile = '')
874
    {
875
        if (!class_exists($loaderClass, FALSE)) {
876
            if ($loaderFile == '') {
877
                $loaderFile = PHPUnit_Util_Filesystem::classNameToFilename(
878
                  $loaderClass
879
                );
880
            }
881
 
882
            $loaderFile = PHPUnit_Util_Filesystem::fileExistsInIncludePath(
883
              $loaderFile
884
            );
885
 
886
            if ($loaderFile !== FALSE) {
887
                require $loaderFile;
888
            }
889
        }
890
 
891
        if (class_exists($loaderClass, FALSE)) {
892
            $class = new ReflectionClass($loaderClass);
893
 
894
            if ($class->implementsInterface('PHPUnit_Runner_TestSuiteLoader') &&
895
                $class->isInstantiable()) {
896
                $loader = $class->newInstance();
897
            }
898
        }
899
 
900
        if (!isset($loader)) {
901
            PHPUnit_TextUI_TestRunner::showError(
902
              sprintf(
903
                'Could not use "%s" as loader.',
904
 
905
                $loaderClass
906
              )
907
            );
908
        }
909
 
910
        return $loader;
911
    }
912
 
913
    /**
914
     * Shows a message.
915
     *
916
     * @param string  $message
917
     * @param boolean $exit
918
     */
919
    protected function showMessage($message, $exit = TRUE)
920
    {
921
        PHPUnit_TextUI_TestRunner::printVersionString();
922
        print $message . "\n";
923
 
924
        if ($exit) {
925
            exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
926
        } else {
927
            print "\n";
928
        }
929
    }
930
 
931
    /**
932
     * Show the help message.
933
     */
934
    protected function showHelp()
935
    {
936
        PHPUnit_TextUI_TestRunner::printVersionString();
937
 
938
        print <<<EOT
939
Usage: phpunit [switches] UnitTest [UnitTest.php]
940
       phpunit [switches] <directory>
941
 
942
  --log-junit <file>       Log test execution in JUnit XML format to file.
943
  --log-tap <file>         Log test execution in TAP format to file.
944
  --log-json <file>        Log test execution in JSON format.
945
 
946
  --coverage-html <dir>    Generate code coverage report in HTML format.
947
  --coverage-clover <file> Write code coverage data in Clover XML format.
948
  --coverage-source <dir>  Write code coverage / source data in XML format.
949
 
950
  --story-html <file>      Write Story/BDD results in HTML format to file.
951
  --story-text <file>      Write Story/BDD results in Text format to file.
952
 
953
  --testdox-html <file>    Write agile documentation in HTML format to file.
954
  --testdox-text <file>    Write agile documentation in Text format to file.
955
 
956
  --filter <pattern>       Filter which tests to run.
957
  --group ...              Only runs tests from the specified group(s).
958
  --exclude-group ...      Exclude tests from the specified group(s).
959
  --list-groups            List available test groups.
960
 
961
  --loader <loader>        TestSuiteLoader implementation to use.
962
  --repeat <times>         Runs the test(s) repeatedly.
963
 
964
  --story                  Report test execution progress in Story/BDD format.
965
  --tap                    Report test execution progress in TAP format.
966
  --testdox                Report test execution progress in TestDox format.
967
 
968
  --colors                 Use colors in output.
969
  --stderr                 Write to STDERR instead of STDOUT.
970
  --stop-on-failure        Stop execution upon first error or failure.
971
  --verbose                Output more verbose information.
972
  --wait                   Waits for a keystroke after each test.
973
 
974
  --skeleton-class         Generate Unit class for UnitTest in UnitTest.php.
975
  --skeleton-test          Generate UnitTest class for Unit in Unit.php.
976
 
977
  --process-isolation      Run each test in a separate PHP process.
978
  --no-globals-backup      Do not backup and restore \$GLOBALS for each test.
979
  --static-backup          Backup and restore static attributes for each test.
980
  --syntax-check           Try to check source files for syntax errors.
981
 
982
  --bootstrap <file>       A "bootstrap" PHP file that is run before the tests.
983
  --configuration <file>   Read configuration from XML file.
984
  --no-configuration       Ignore default configuration file (phpunit.xml).
985
  --include-path <path(s)> Prepend PHP's include_path with given path(s).
986
  -d key[=value]           Sets a php.ini value.
987
 
988
  --help                   Prints this usage information.
989
  --version                Prints the version and exits.
990
 
991
EOT;
992
    }
993
 
994
    /**
995
     * Custom callback for test suite discovery.
996
     */
997
    protected function handleCustomTestSuite()
998
    {
999
    }
1000
}
1001
?>