Version 1
[yaffs-website] / vendor / phpunit / phpunit / src / Runner / BaseTestRunner.php
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  * Base class for all test runners.
13  *
14  * @since Class available since Release 2.0.0
15  */
16 abstract class PHPUnit_Runner_BaseTestRunner
17 {
18     const STATUS_PASSED     = 0;
19     const STATUS_SKIPPED    = 1;
20     const STATUS_INCOMPLETE = 2;
21     const STATUS_FAILURE    = 3;
22     const STATUS_ERROR      = 4;
23     const STATUS_RISKY      = 5;
24     const SUITE_METHODNAME  = 'suite';
25
26     /**
27      * Returns the loader to be used.
28      *
29      * @return PHPUnit_Runner_TestSuiteLoader
30      */
31     public function getLoader()
32     {
33         return new PHPUnit_Runner_StandardTestSuiteLoader;
34     }
35
36     /**
37      * Returns the Test corresponding to the given suite.
38      * This is a template method, subclasses override
39      * the runFailed() and clearStatus() methods.
40      *
41      * @param string $suiteClassName
42      * @param string $suiteClassFile
43      * @param mixed  $suffixes
44      *
45      * @return PHPUnit_Framework_Test
46      */
47     public function getTest($suiteClassName, $suiteClassFile = '', $suffixes = '')
48     {
49         if (is_dir($suiteClassName) &&
50             !is_file($suiteClassName . '.php') && empty($suiteClassFile)) {
51             $facade = new File_Iterator_Facade;
52             $files  = $facade->getFilesAsArray(
53                 $suiteClassName,
54                 $suffixes
55             );
56
57             $suite = new PHPUnit_Framework_TestSuite($suiteClassName);
58             $suite->addTestFiles($files);
59
60             return $suite;
61         }
62
63         try {
64             $testClass = $this->loadSuiteClass(
65                 $suiteClassName,
66                 $suiteClassFile
67             );
68         } catch (PHPUnit_Framework_Exception $e) {
69             $this->runFailed($e->getMessage());
70
71             return;
72         }
73
74         try {
75             $suiteMethod = $testClass->getMethod(self::SUITE_METHODNAME);
76
77             if (!$suiteMethod->isStatic()) {
78                 $this->runFailed(
79                     'suite() method must be static.'
80                 );
81
82                 return;
83             }
84
85             try {
86                 $test = $suiteMethod->invoke(null, $testClass->getName());
87             } catch (ReflectionException $e) {
88                 $this->runFailed(
89                     sprintf(
90                         "Failed to invoke suite() method.\n%s",
91                         $e->getMessage()
92                     )
93                 );
94
95                 return;
96             }
97         } catch (ReflectionException $e) {
98             try {
99                 $test = new PHPUnit_Framework_TestSuite($testClass);
100             } catch (PHPUnit_Framework_Exception $e) {
101                 $test = new PHPUnit_Framework_TestSuite;
102                 $test->setName($suiteClassName);
103             }
104         }
105
106         $this->clearStatus();
107
108         return $test;
109     }
110
111     /**
112      * Returns the loaded ReflectionClass for a suite name.
113      *
114      * @param string $suiteClassName
115      * @param string $suiteClassFile
116      *
117      * @return ReflectionClass
118      */
119     protected function loadSuiteClass($suiteClassName, $suiteClassFile = '')
120     {
121         $loader = $this->getLoader();
122
123         return $loader->load($suiteClassName, $suiteClassFile);
124     }
125
126     /**
127      * Clears the status message.
128      */
129     protected function clearStatus()
130     {
131     }
132
133     /**
134      * Override to define how to handle a failed loading of
135      * a test suite.
136      *
137      * @param string $message
138      */
139     abstract protected function runFailed($message);
140 }