Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * CakeTestSuiteDispatcher controls dispatching TestSuite web based requests.
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.TestSuite
15
 * @since         CakePHP(tm) v 1.3
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
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
 * CakeTestSuiteDispatcher handles web requests to the test suite and runs the correct action.
26
 *
27
 * @package       Cake.TestSuite
28
 */
29
class CakeTestSuiteDispatcher {
30
 
31
/**
32
 * 'Request' parameters
33
 *
34
 * @var array
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
 * Baseurl for the request
51
 *
52
 * @var string
53
 */
54
	protected $_baseUrl;
55
 
56
/**
57
 * Base dir of the request. Used for accessing assets.
58
 *
59
 * @var string
60
 */
61
	protected $_baseDir;
62
 
63
/**
64
 * boolean to set auto parsing of params.
65
 *
66
 * @var bool
67
 */
68
	protected $_paramsParsed = false;
69
 
70
/**
71
 * reporter instance used for the request
72
 *
73
 * @var CakeBaseReporter
74
 */
75
	protected static $_Reporter = null;
76
 
77
/**
78
 * Constructor
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
 * Runs the actions required by the URL parameters.
88
 *
89
 * @return void
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
 * Static method to initialize the test runner, keeps global space clean
108
 *
109
 * @return void
110
 */
111
	public static function run() {
112
		$dispatcher = new CakeTestSuiteDispatcher();
113
		$dispatcher->dispatch();
114
	}
115
 
116
/**
117
 * Checks that PHPUnit is installed. Will exit if it doesn't
118
 *
119
 * @return void
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
 * Checks for the existence of the test framework files
132
 *
133
 * @return bool true if found, false otherwise
134
 */
135
	public function loadTestFramework() {
136
		if (class_exists('PHPUnit_Framework_TestCase')) {
137
			return true;
138
		}
139
		$phpunitPath = 'phpunit' . DS . 'phpunit';
140
		if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
141
			$composerGlobalDir[] = env('APPDATA') . DS . 'Composer' . DS . 'vendor' . DS;
142
		} else {
143
			$composerGlobalDir[] = env('HOME') . DS . '.composer' . DS . 'vendor' . DS;
144
		}
145
		$vendors = array_merge(App::path('vendors'), $composerGlobalDir);
146
		foreach ($vendors as $vendor) {
147
			$vendor = rtrim($vendor, DS);
148
			if (is_dir($vendor . DS . $phpunitPath)) {
149
				ini_set('include_path', $vendor . DS . $phpunitPath . PATH_SEPARATOR . ini_get('include_path'));
150
				break;
151
			} elseif (is_dir($vendor . DS . 'PHPUnit')) {
152
				ini_set('include_path', $vendor . PATH_SEPARATOR . ini_get('include_path'));
153
				break;
154
			}
155
		}
156
		include 'PHPUnit' . DS . 'Autoload.php';
157
		return class_exists('PHPUnit_Framework_TestCase');
158
	}
159
 
160
/**
161
 * Checks for the xdebug extension required to do code coverage. Displays an error
162
 * if xdebug isn't installed.
163
 *
164
 * @return void
165
 */
166
	protected function _checkXdebug() {
167
		if (!extension_loaded('xdebug')) {
168
			$baseDir = $this->_baseDir;
169
			include CAKE . 'TestSuite' . DS . 'templates' . DS . 'xdebug.php';
170
			exit();
171
		}
172
	}
173
 
174
/**
175
 * Generates a page containing the a list of test cases that could be run.
176
 *
177
 * @return void
178
 */
179
	protected function _testCaseList() {
180
		$command = new CakeTestSuiteCommand('', $this->params);
181
		$Reporter = $command->handleReporter($this->params['output']);
182
		$Reporter->paintDocumentStart();
183
		$Reporter->paintTestMenu();
184
		$Reporter->testCaseList();
185
		$Reporter->paintDocumentEnd();
186
	}
187
 
188
/**
189
 * Sets the params, calling this will bypass the auto parameter parsing.
190
 *
191
 * @param array $params Array of parameters for the dispatcher
192
 * @return void
193
 */
194
	public function setParams($params) {
195
		$this->params = $params;
196
		$this->_paramsParsed = true;
197
	}
198
 
199
/**
200
 * Parse URL params into a 'request'
201
 *
202
 * @return void
203
 */
204
	protected function _parseParams() {
205
		if (!$this->_paramsParsed) {
206
			if (!isset($_SERVER['SERVER_NAME'])) {
207
				$_SERVER['SERVER_NAME'] = '';
208
			}
209
			foreach ($this->params as $key => $value) {
210
				if (isset($_GET[$key])) {
211
					$this->params[$key] = $_GET[$key];
212
				}
213
			}
214
			if (isset($_GET['code_coverage'])) {
215
				$this->params['codeCoverage'] = true;
216
				$this->_checkXdebug();
217
			}
218
		}
219
		if (empty($this->params['plugin']) && empty($this->params['core'])) {
220
			$this->params['app'] = true;
221
		}
222
		$this->params['baseUrl'] = $this->_baseUrl;
223
		$this->params['baseDir'] = $this->_baseDir;
224
	}
225
 
226
/**
227
 * Runs a test case file.
228
 *
229
 * @return void
230
 */
231
	protected function _runTestCase() {
232
		$commandArgs = array(
233
			'case' => $this->params['case'],
234
			'core' => $this->params['core'],
235
			'app' => $this->params['app'],
236
			'plugin' => $this->params['plugin'],
237
			'codeCoverage' => $this->params['codeCoverage'],
238
			'showPasses' => !empty($this->params['show_passes']),
239
			'baseUrl' => $this->_baseUrl,
240
			'baseDir' => $this->_baseDir,
241
		);
242
 
243
		$options = array(
244
			'--filter', $this->params['filter'],
245
			'--output', $this->params['output'],
246
			'--fixture', $this->params['fixture']
247
		);
248
		restore_error_handler();
249
 
250
		try {
251
			self::time();
252
			$command = new CakeTestSuiteCommand('CakeTestLoader', $commandArgs);
253
			$command->run($options);
254
		} catch (MissingConnectionException $exception) {
255
			ob_end_clean();
256
			$baseDir = $this->_baseDir;
257
			include CAKE . 'TestSuite' . DS . 'templates' . DS . 'missing_connection.php';
258
			exit();
259
		}
260
	}
261
 
262
/**
263
 * Sets a static timestamp
264
 *
265
 * @param bool $reset to set new static timestamp.
266
 * @return int timestamp
267
 */
268
	public static function time($reset = false) {
269
		static $now;
270
		if ($reset || !$now) {
271
			$now = time();
272
		}
273
		return $now;
274
	}
275
 
276
/**
277
 * Returns formatted date string using static time
278
 * This method is being used as formatter for created, modified and updated fields in Model::save()
279
 *
280
 * @param string $format format to be used.
281
 * @return string formatted date
282
 */
283
	public static function date($format) {
284
		return date($format, self::time());
285
	}
286
 
287
}