1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: define('CORE_TEST_CASES', CAKE . 'Test' . DS . 'Case');
20: define('APP_TEST_CASES', TESTS . 'Case');
21:
22: App::uses('CakeTestSuiteCommand', 'TestSuite');
23:
24: 25: 26: 27: 28:
29: class CakeTestSuiteDispatcher {
30:
31: 32: 33: 34: 35:
36: public $params = array(
37: 'codeCoverage' => false,
38: 'case' => null,
39: 'core' => false,
40: 'app' => true,
41: 'plugin' => null,
42: 'output' => 'html',
43: 'show' => 'groups',
44: 'show_passes' => false,
45: 'filter' => false,
46: 'fixture' => null
47: );
48:
49: 50: 51: 52: 53:
54: protected $_baseUrl;
55:
56: 57: 58: 59: 60:
61: protected $_baseDir;
62:
63: 64: 65: 66: 67:
68: protected $_paramsParsed = false;
69:
70: 71: 72: 73: 74:
75: protected static $_Reporter = null;
76:
77: 78: 79:
80: public function __construct() {
81: $this->_baseUrl = $_SERVER['PHP_SELF'];
82: $dir = rtrim(dirname($this->_baseUrl), '\\');
83: $this->_baseDir = ($dir === '/') ? $dir : $dir . '/';
84: }
85:
86: 87: 88: 89: 90:
91: public function dispatch() {
92: $this->_checkPHPUnit();
93: $this->_parseParams();
94:
95: if ($this->params['case']) {
96: $value = $this->_runTestCase();
97: } else {
98: $value = $this->_testCaseList();
99: }
100:
101: $output = ob_get_clean();
102: echo $output;
103: return $value;
104: }
105:
106: 107: 108: 109: 110:
111: public static function run() {
112: $dispatcher = new CakeTestSuiteDispatcher();
113: $dispatcher->dispatch();
114: }
115:
116: 117: 118: 119: 120:
121: protected function _checkPHPUnit() {
122: $found = $this->loadTestFramework();
123: if (!$found) {
124: $baseDir = $this->_baseDir;
125: include CAKE . 'TestSuite' . DS . 'templates' . DS . 'phpunit.php';
126: exit();
127: }
128: }
129:
130: 131: 132: 133: 134:
135: public function loadTestFramework() {
136: if (class_exists('PHPUnit_Framework_TestCase')) {
137: return true;
138: }
139: foreach (App::path('vendors') as $vendor) {
140: $vendor = rtrim($vendor, DS);
141: if (is_dir($vendor . DS . 'PHPUnit')) {
142: ini_set('include_path', $vendor . PATH_SEPARATOR . ini_get('include_path'));
143: break;
144: }
145: }
146: include 'PHPUnit' . DS . 'Autoload.php';
147: return class_exists('PHPUnit_Framework_TestCase');
148: }
149:
150: 151: 152: 153: 154: 155:
156: protected function _checkXdebug() {
157: if (!extension_loaded('xdebug')) {
158: $baseDir = $this->_baseDir;
159: include CAKE . 'TestSuite' . DS . 'templates' . DS . 'xdebug.php';
160: exit();
161: }
162: }
163:
164: 165: 166: 167: 168:
169: protected function _testCaseList() {
170: $command = new CakeTestSuiteCommand('', $this->params);
171: $Reporter = $command->handleReporter($this->params['output']);
172: $Reporter->paintDocumentStart();
173: $Reporter->paintTestMenu();
174: $Reporter->testCaseList();
175: $Reporter->paintDocumentEnd();
176: }
177:
178: 179: 180: 181: 182: 183:
184: public function setParams($params) {
185: $this->params = $params;
186: $this->_paramsParsed = true;
187: }
188:
189: 190: 191: 192: 193:
194: protected function _parseParams() {
195: if (!$this->_paramsParsed) {
196: if (!isset($_SERVER['SERVER_NAME'])) {
197: $_SERVER['SERVER_NAME'] = '';
198: }
199: foreach ($this->params as $key => $value) {
200: if (isset($_GET[$key])) {
201: $this->params[$key] = $_GET[$key];
202: }
203: }
204: if (isset($_GET['code_coverage'])) {
205: $this->params['codeCoverage'] = true;
206: $this->_checkXdebug();
207: }
208: }
209: if (empty($this->params['plugin']) && empty($this->params['core'])) {
210: $this->params['app'] = true;
211: }
212: $this->params['baseUrl'] = $this->_baseUrl;
213: $this->params['baseDir'] = $this->_baseDir;
214: }
215:
216: 217: 218: 219: 220:
221: protected function _runTestCase() {
222: $commandArgs = array(
223: 'case' => $this->params['case'],
224: 'core' => $this->params['core'],
225: 'app' => $this->params['app'],
226: 'plugin' => $this->params['plugin'],
227: 'codeCoverage' => $this->params['codeCoverage'],
228: 'showPasses' => !empty($this->params['show_passes']),
229: 'baseUrl' => $this->_baseUrl,
230: 'baseDir' => $this->_baseDir,
231: );
232:
233: $options = array(
234: '--filter', $this->params['filter'],
235: '--output', $this->params['output'],
236: '--fixture', $this->params['fixture']
237: );
238: restore_error_handler();
239:
240: try {
241: self::time();
242: $command = new CakeTestSuiteCommand('CakeTestLoader', $commandArgs);
243: $command->run($options);
244: } catch (MissingConnectionException $exception) {
245: ob_end_clean();
246: $baseDir = $this->_baseDir;
247: include CAKE . 'TestSuite' . DS . 'templates' . DS . 'missing_connection.php';
248: exit();
249: }
250: }
251:
252: 253: 254: 255: 256: 257:
258: public static function time($reset = false) {
259: static $now;
260: if ($reset || !$now) {
261: $now = time();
262: }
263: return $now;
264: }
265:
266: 267: 268: 269: 270: 271: 272:
273: public static function date($format) {
274: return date($format, self::time());
275: }
276:
277: }
278: