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