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