Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 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 boolean
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 boolean true if found, false otherwise
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
 * Checks for the xdebug extension required to do code coverage. Displays an error
152
 * if xdebug isn't installed.
153
 *
154
 * @return void
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
 * Generates a page containing the a list of test cases that could be run.
166
 *
167
 * @return void
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
 * Sets the params, calling this will bypass the auto parameter parsing.
180
 *
181
 * @param array $params Array of parameters for the dispatcher
182
 * @return void
183
 */
184
	public function setParams($params) {
185
		$this->params = $params;
186
		$this->_paramsParsed = true;
187
	}
188
 
189
/**
190
 * Parse URL params into a 'request'
191
 *
192
 * @return void
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
 * Runs a test case file.
218
 *
219
 * @return void
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
 * Sets a static timestamp
254
 *
255
 * @param boolean $reset to set new static timestamp.
256
 * @return integer timestamp
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
 * Returns formatted date string using static time
268
 * This method is being used as formatter for created, modified and updated fields in Model::save()
269
 *
270
 * @param string $format format to be used.
271
 * @return string formatted date
272
 */
273
	public static function date($format) {
274
		return date($format, self::time());
275
	}
276
 
277
}