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: public $params = array(
37: 'codeCoverage' => false,
38: 'case' => null,
39: 'core' => false,
40: 'app' => false,
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: 81:
82: function __construct() {
83: $this->_baseUrl = $_SERVER['PHP_SELF'];
84: $dir = rtrim(dirname($this->_baseUrl), '\\');
85: $this->_baseDir = ($dir === '/') ? $dir : $dir . '/';
86: }
87:
88: 89: 90: 91: 92:
93: public function dispatch() {
94: $this->_checkPHPUnit();
95: $this->_parseParams();
96:
97: if ($this->params['case']) {
98: $value = $this->_runTestCase();
99: } else {
100: $value = $this->_testCaseList();
101: }
102:
103: $output = ob_get_clean();
104: echo $output;
105: return $value;
106: }
107:
108: 109: 110: 111: 112:
113: public static function run() {
114: $dispatcher = new CakeTestSuiteDispatcher();
115: $dispatcher->dispatch();
116: }
117:
118: 119: 120: 121: 122:
123: protected function _checkPHPUnit() {
124: $found = $this->loadTestFramework();
125: if (!$found) {
126: $baseDir = $this->_baseDir;
127: include CAKE . 'TestSuite' . DS . 'templates' . DS . 'phpunit.php';
128: exit();
129: }
130: }
131:
132: 133: 134: 135: 136:
137: public function loadTestFramework() {
138: $found = $path = null;
139:
140: if (@include 'PHPUnit' . DS . 'Autoload.php') {
141: $found = true;
142: }
143:
144: if (!$found) {
145: foreach (App::path('vendors') as $vendor) {
146: if (is_dir($vendor . 'PHPUnit')) {
147: $path = $vendor;
148: }
149: }
150:
151: if ($path && ini_set('include_path', $path . PATH_SEPARATOR . ini_get('include_path'))) {
152: $found = include 'PHPUnit' . DS . 'Autoload.php';
153: }
154: }
155: return $found;
156: }
157:
158: 159: 160: 161: 162: 163:
164: function _checkXdebug() {
165: if (!extension_loaded('xdebug')) {
166: $baseDir = $this->_baseDir;
167: include CAKE . 'TestSuite' . DS . 'templates' . DS . 'xdebug.php';
168: exit();
169: }
170: }
171:
172: 173: 174: 175: 176:
177: function _testCaseList() {
178: $command = new CakeTestSuiteCommand('', $this->params);
179: $Reporter = $command->handleReporter($this->params['output']);
180: $Reporter->paintDocumentStart();
181: $Reporter->paintTestMenu();
182: $Reporter->testCaseList();
183: $Reporter->paintDocumentEnd();
184: }
185:
186: 187: 188: 189: 190: 191:
192: public function setParams($params) {
193: $this->params = $params;
194: $this->_paramsParsed = true;
195: }
196:
197: 198: 199: 200: 201:
202: function _parseParams() {
203: if (!$this->_paramsParsed) {
204: if (!isset($_SERVER['SERVER_NAME'])) {
205: $_SERVER['SERVER_NAME'] = '';
206: }
207: foreach ($this->params as $key => $value) {
208: if (isset($_GET[$key])) {
209: $this->params[$key] = $_GET[$key];
210: }
211: }
212: if (isset($_GET['code_coverage'])) {
213: $this->params['codeCoverage'] = true;
214: $this->_checkXdebug();
215: }
216: }
217: if (empty($this->params['plugin']) && empty($this->params['app'])) {
218: $this->params['core'] = true;
219: }
220: $this->params['baseUrl'] = $this->_baseUrl;
221: $this->params['baseDir'] = $this->_baseDir;
222: }
223:
224: 225: 226: 227: 228:
229: function _runTestCase() {
230: $commandArgs = array(
231: 'case' => $this->params['case'],
232: 'core' => $this->params['core'],
233: 'app' => $this->params['app'],
234: 'plugin' => $this->params['plugin'],
235: 'codeCoverage' => $this->params['codeCoverage'],
236: 'showPasses' => !empty($this->params['show_passes']),
237: 'baseUrl' => $this->_baseUrl,
238: 'baseDir' => $this->_baseDir,
239: );
240:
241: $options = array(
242: '--filter', $this->params['filter'],
243: '--output', $this->params['output'],
244: '--fixture', $this->params['fixture']
245: );
246: restore_error_handler();
247:
248: try {
249: $command = new CakeTestSuiteCommand('CakeTestLoader', $commandArgs);
250: $result = $command->run($options);
251: } catch (MissingConnectionException $exception) {
252: ob_end_clean();
253: $baseDir = $this->_baseDir;
254: include CAKE . 'TestSuite' . DS . 'templates' . DS . 'missing_connection.php';
255: exit();
256: }
257: }
258: }
259: