| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: cake_test_case.php 8004 2009-01-16 20:15:21Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Short description for file.
|
|
|
5 |
*
|
|
|
6 |
* Long description for file
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The Open Group Test Suite License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.tests.libs
|
|
|
21 |
* @since CakePHP(tm) v 1.2.0.4667
|
|
|
22 |
* @version $Revision: 8004 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2009-01-16 12:15:21 -0800 (Fri, 16 Jan 2009) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
|
|
26 |
*/
|
|
|
27 |
if (!class_exists('dispatcher')) {
|
|
|
28 |
require CAKE . 'dispatcher.php';
|
|
|
29 |
}
|
|
|
30 |
require_once CAKE_TESTS_LIB . 'cake_test_model.php';
|
|
|
31 |
require_once CAKE_TESTS_LIB . 'cake_test_fixture.php';
|
|
|
32 |
App::import('Vendor', 'simpletest' . DS . 'unit_tester');
|
|
|
33 |
/**
|
|
|
34 |
* Short description for class.
|
|
|
35 |
*
|
|
|
36 |
* @package cake
|
|
|
37 |
* @subpackage cake.cake.tests.lib
|
|
|
38 |
*/
|
|
|
39 |
class CakeTestDispatcher extends Dispatcher {
|
|
|
40 |
var $controller;
|
|
|
41 |
var $testCase;
|
|
|
42 |
|
|
|
43 |
function testCase(&$testCase) {
|
|
|
44 |
$this->testCase =& $testCase;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
function _invoke (&$controller, $params, $missingAction = false) {
|
|
|
48 |
$this->controller =& $controller;
|
|
|
49 |
|
|
|
50 |
if (isset($this->testCase) && method_exists($this->testCase, 'startController')) {
|
|
|
51 |
$this->testCase->startController($this->controller, $params);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
$result = parent::_invoke($this->controller, $params, $missingAction);
|
|
|
55 |
|
|
|
56 |
if (isset($this->testCase) && method_exists($this->testCase, 'endController')) {
|
|
|
57 |
$this->testCase->endController($this->controller, $params);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
return $result;
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
/**
|
|
|
64 |
* Short description for class.
|
|
|
65 |
*
|
|
|
66 |
* @package cake
|
|
|
67 |
* @subpackage cake.cake.tests.lib
|
|
|
68 |
*/
|
|
|
69 |
class CakeTestCase extends UnitTestCase {
|
|
|
70 |
/**
|
|
|
71 |
* Methods used internally.
|
|
|
72 |
*
|
|
|
73 |
* @var array
|
|
|
74 |
* @access private
|
|
|
75 |
*/
|
|
|
76 |
var $methods = array('start', 'end', 'startcase', 'endcase', 'starttest', 'endtest');
|
|
|
77 |
var $__truncated = true;
|
|
|
78 |
var $__savedGetData = array();
|
|
|
79 |
/**
|
|
|
80 |
* By default, all fixtures attached to this class will be truncated and reloaded after each test.
|
|
|
81 |
* Set this to false to handle manually
|
|
|
82 |
*
|
|
|
83 |
* @var array
|
|
|
84 |
* @access public
|
|
|
85 |
*/
|
|
|
86 |
var $autoFixtures = true;
|
|
|
87 |
/**
|
|
|
88 |
* Set this to false to avoid tables to be dropped if they already exist
|
|
|
89 |
*
|
|
|
90 |
* @var boolean
|
|
|
91 |
* @access public
|
|
|
92 |
*/
|
|
|
93 |
var $dropTables = true;
|
|
|
94 |
/**
|
|
|
95 |
* Maps fixture class names to fixture identifiers as included in CakeTestCase::$fixtures
|
|
|
96 |
*
|
|
|
97 |
* @var array
|
|
|
98 |
* @access protected
|
|
|
99 |
*/
|
|
|
100 |
var $_fixtureClassMap = array();
|
|
|
101 |
/**
|
|
|
102 |
* Called when a test case (group of methods) is about to start (to be overriden when needed.)
|
|
|
103 |
*
|
|
|
104 |
* @param string $method Test method about to get executed.
|
|
|
105 |
*
|
|
|
106 |
* @access protected
|
|
|
107 |
*/
|
|
|
108 |
function startCase() {
|
|
|
109 |
}
|
|
|
110 |
/**
|
|
|
111 |
* Called when a test case (group of methods) has been executed (to be overriden when needed.)
|
|
|
112 |
*
|
|
|
113 |
* @param string $method Test method about that was executed.
|
|
|
114 |
*
|
|
|
115 |
* @access protected
|
|
|
116 |
*/
|
|
|
117 |
function endCase() {
|
|
|
118 |
}
|
|
|
119 |
/**
|
|
|
120 |
* Called when a test case method is about to start (to be overriden when needed.)
|
|
|
121 |
*
|
|
|
122 |
* @param string $method Test method about to get executed.
|
|
|
123 |
*
|
|
|
124 |
* @access protected
|
|
|
125 |
*/
|
|
|
126 |
function startTest($method) {
|
|
|
127 |
}
|
|
|
128 |
/**
|
|
|
129 |
* Called when a test case method has been executed (to be overriden when needed.)
|
|
|
130 |
*
|
|
|
131 |
* @param string $method Test method about that was executed.
|
|
|
132 |
*
|
|
|
133 |
* @access protected
|
|
|
134 |
*/
|
|
|
135 |
function endTest($method) {
|
|
|
136 |
}
|
|
|
137 |
/**
|
|
|
138 |
* Overrides SimpleTestCase::assert to enable calling of skipIf() from within tests
|
|
|
139 |
*/
|
|
|
140 |
function assert(&$expectation, $compare, $message = '%s') {
|
|
|
141 |
if ($this->_should_skip) {
|
|
|
142 |
return;
|
|
|
143 |
}
|
|
|
144 |
return parent::assert($expectation, $compare, $message);
|
|
|
145 |
}
|
|
|
146 |
/**
|
|
|
147 |
* Overrides SimpleTestCase::skipIf to provide a boolean return value
|
|
|
148 |
*/
|
|
|
149 |
function skipIf($shouldSkip, $message = '%s') {
|
|
|
150 |
parent::skipIf($shouldSkip, $message);
|
|
|
151 |
return $shouldSkip;
|
|
|
152 |
}
|
|
|
153 |
/**
|
|
|
154 |
* Callback issued when a controller's action is about to be invoked through testAction().
|
|
|
155 |
*
|
|
|
156 |
* @param Controller $controller Controller that's about to be invoked.
|
|
|
157 |
* @param array $params Additional parameters as sent by testAction().
|
|
|
158 |
*/
|
|
|
159 |
function startController(&$controller, $params = array()) {
|
|
|
160 |
if (isset($params['fixturize']) && ((is_array($params['fixturize']) && !empty($params['fixturize'])) || $params['fixturize'] === true)) {
|
|
|
161 |
if (!isset($this->db)) {
|
|
|
162 |
$this->_initDb();
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
if ($controller->uses === false) {
|
|
|
166 |
$list = array($controller->modelClass);
|
|
|
167 |
} else {
|
|
|
168 |
$list = is_array($controller->uses) ? $controller->uses : array($controller->uses);
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
$models = array();
|
|
|
172 |
ClassRegistry::config(array('ds' => $params['connection']));
|
|
|
173 |
|
|
|
174 |
foreach ($list as $name) {
|
|
|
175 |
if ((is_array($params['fixturize']) && in_array($name, $params['fixturize'])) || $params['fixturize'] === true) {
|
|
|
176 |
if (class_exists($name) || App::import('Model', $name)) {
|
|
|
177 |
$object =& ClassRegistry::init($name);
|
|
|
178 |
//switch back to specified datasource.
|
|
|
179 |
$object->setDataSource($params['connection']);
|
|
|
180 |
$db =& ConnectionManager::getDataSource($object->useDbConfig);
|
|
|
181 |
$db->cacheSources = false;
|
|
|
182 |
|
|
|
183 |
$models[$object->alias] = array(
|
|
|
184 |
'table' => $object->table,
|
|
|
185 |
'model' => $object->alias,
|
|
|
186 |
'key' => strtolower($name),
|
|
|
187 |
);
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
}
|
|
|
191 |
ClassRegistry::config(array('ds' => 'test_suite'));
|
|
|
192 |
|
|
|
193 |
if (!empty($models) && isset($this->db)) {
|
|
|
194 |
$this->_actionFixtures = array();
|
|
|
195 |
|
|
|
196 |
foreach ($models as $model) {
|
|
|
197 |
$fixture =& new CakeTestFixture($this->db);
|
|
|
198 |
|
|
|
199 |
$fixture->name = $model['model'] . 'Test';
|
|
|
200 |
$fixture->table = $model['table'];
|
|
|
201 |
$fixture->import = array('model' => $model['model'], 'records' => true);
|
|
|
202 |
$fixture->init();
|
|
|
203 |
|
|
|
204 |
$fixture->create($this->db);
|
|
|
205 |
$fixture->insert($this->db);
|
|
|
206 |
$this->_actionFixtures[] =& $fixture;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
foreach ($models as $model) {
|
|
|
210 |
$object =& ClassRegistry::getObject($model['key']);
|
|
|
211 |
if ($object !== false) {
|
|
|
212 |
$object->setDataSource('test_suite');
|
|
|
213 |
$object->cacheSources = false;
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
/**
|
|
|
220 |
* Callback issued when a controller's action has been invoked through testAction().
|
|
|
221 |
*
|
|
|
222 |
* @param Controller $controller Controller that has been invoked.
|
|
|
223 |
* * @param array $params Additional parameters as sent by testAction().
|
|
|
224 |
*/
|
|
|
225 |
function endController(&$controller, $params = array()) {
|
|
|
226 |
if (isset($this->db) && isset($this->_actionFixtures) && !empty($this->_actionFixtures)) {
|
|
|
227 |
foreach ($this->_actionFixtures as $fixture) {
|
|
|
228 |
$fixture->drop($this->db);
|
|
|
229 |
}
|
|
|
230 |
}
|
|
|
231 |
}
|
|
|
232 |
/**
|
|
|
233 |
* Executes a Cake URL, and can get (depending on the $params['return'] value):
|
|
|
234 |
*
|
|
|
235 |
* 1. 'result': Whatever the action returns (and also specifies $this->params['requested'] for controller)
|
|
|
236 |
* 2. 'view': The rendered view, without the layout
|
|
|
237 |
* 3. 'contents': The rendered view, within the layout.
|
|
|
238 |
* 4. 'vars': the view vars
|
|
|
239 |
*
|
|
|
240 |
* @param string $url Cake URL to execute (e.g: /articles/view/455)
|
|
|
241 |
* @param array $params Parameters, or simply a string of what to return
|
|
|
242 |
* @return mixed Whatever is returned depending of requested result
|
|
|
243 |
* @access public
|
|
|
244 |
*/
|
|
|
245 |
function testAction($url, $params = array()) {
|
|
|
246 |
$default = array(
|
|
|
247 |
'return' => 'result',
|
|
|
248 |
'fixturize' => false,
|
|
|
249 |
'data' => array(),
|
|
|
250 |
'method' => 'post',
|
|
|
251 |
'connection' => 'default'
|
|
|
252 |
);
|
|
|
253 |
|
|
|
254 |
if (is_string($params)) {
|
|
|
255 |
$params = array('return' => $params);
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
$params = array_merge($default, $params);
|
|
|
259 |
|
|
|
260 |
$toSave = array(
|
|
|
261 |
'case' => null,
|
|
|
262 |
'group' => null,
|
|
|
263 |
'app' => null,
|
|
|
264 |
'output' => null,
|
|
|
265 |
'show' => null,
|
|
|
266 |
'plugin' => null
|
|
|
267 |
);
|
|
|
268 |
$this->__savedGetData = (empty($this->__savedGetData))
|
|
|
269 |
? array_intersect_key($_GET, $toSave)
|
|
|
270 |
: $this->__savedGetData;
|
|
|
271 |
|
|
|
272 |
$data = (!empty($params['data'])) ? $params['data'] : array();
|
|
|
273 |
|
|
|
274 |
if (strtolower($params['method']) == 'get') {
|
|
|
275 |
$_GET = array_merge($this->__savedGetData, $data);
|
|
|
276 |
$_POST = array();
|
|
|
277 |
} else {
|
|
|
278 |
$_POST = array('data' => $data);
|
|
|
279 |
$_GET = $this->__savedGetData;
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
$return = $params['return'];
|
|
|
283 |
$params = array_diff_key($params, array('data' => null, 'method' => null, 'return' => null));
|
|
|
284 |
|
|
|
285 |
$dispatcher =& new CakeTestDispatcher();
|
|
|
286 |
$dispatcher->testCase($this);
|
|
|
287 |
|
|
|
288 |
if ($return != 'result') {
|
|
|
289 |
if ($return != 'contents') {
|
|
|
290 |
$params['layout'] = false;
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
ob_start();
|
|
|
294 |
@$dispatcher->dispatch($url, $params);
|
|
|
295 |
$result = ob_get_clean();
|
|
|
296 |
|
|
|
297 |
if ($return == 'vars') {
|
|
|
298 |
$view =& ClassRegistry::getObject('view');
|
|
|
299 |
$viewVars = $view->getVars();
|
|
|
300 |
|
|
|
301 |
$result = array();
|
|
|
302 |
|
|
|
303 |
foreach ($viewVars as $var) {
|
|
|
304 |
$result[$var] = $view->getVar($var);
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
if (!empty($view->pageTitle)) {
|
|
|
308 |
$result = array_merge($result, array('title' => $view->pageTitle));
|
|
|
309 |
}
|
|
|
310 |
}
|
|
|
311 |
} else {
|
|
|
312 |
$params['return'] = 1;
|
|
|
313 |
$params['bare'] = 1;
|
|
|
314 |
$params['requested'] = 1;
|
|
|
315 |
|
|
|
316 |
$result = @$dispatcher->dispatch($url, $params);
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
if (isset($this->_actionFixtures)) {
|
|
|
320 |
unset($this->_actionFixtures);
|
|
|
321 |
}
|
|
|
322 |
ClassRegistry::flush();
|
|
|
323 |
|
|
|
324 |
return $result;
|
|
|
325 |
}
|
|
|
326 |
/**
|
|
|
327 |
* Announces the start of a test.
|
|
|
328 |
*
|
|
|
329 |
* @param string $method Test method just started.
|
|
|
330 |
*
|
|
|
331 |
* @access public
|
|
|
332 |
*/
|
|
|
333 |
function before($method) {
|
|
|
334 |
parent::before($method);
|
|
|
335 |
|
|
|
336 |
if (isset($this->fixtures) && (!is_array($this->fixtures) || empty($this->fixtures))) {
|
|
|
337 |
unset($this->fixtures);
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
// Set up DB connection
|
|
|
341 |
if (isset($this->fixtures) && strtolower($method) == 'start') {
|
|
|
342 |
$this->_initDb();
|
|
|
343 |
$this->_loadFixtures();
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
// Create records
|
|
|
347 |
if (isset($this->_fixtures) && isset($this->db) && !in_array(strtolower($method), array('start', 'end')) && $this->__truncated && $this->autoFixtures == true) {
|
|
|
348 |
foreach ($this->_fixtures as $fixture) {
|
|
|
349 |
$inserts = $fixture->insert($this->db);
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
if (!in_array(strtolower($method), $this->methods)) {
|
|
|
354 |
$this->startTest($method);
|
|
|
355 |
}
|
|
|
356 |
}
|
|
|
357 |
/**
|
|
|
358 |
* Runs as first test to create tables.
|
|
|
359 |
*
|
|
|
360 |
* @access public
|
|
|
361 |
*/
|
|
|
362 |
function start() {
|
|
|
363 |
if (isset($this->_fixtures) && isset($this->db)) {
|
|
|
364 |
Configure::write('Cache.disable', true);
|
|
|
365 |
$cacheSources = $this->db->cacheSources;
|
|
|
366 |
$this->db->cacheSources = false;
|
|
|
367 |
$sources = $this->db->listSources();
|
|
|
368 |
$this->db->cacheSources = $cacheSources;
|
|
|
369 |
|
|
|
370 |
if (!$this->dropTables) {
|
|
|
371 |
return;
|
|
|
372 |
}
|
|
|
373 |
foreach ($this->_fixtures as $fixture) {
|
|
|
374 |
if (in_array($fixture->table, $sources)) {
|
|
|
375 |
$fixture->drop($this->db);
|
|
|
376 |
$fixture->create($this->db);
|
|
|
377 |
} elseif (!in_array($fixture->table, $sources)) {
|
|
|
378 |
$fixture->create($this->db);
|
|
|
379 |
}
|
|
|
380 |
}
|
|
|
381 |
}
|
|
|
382 |
}
|
|
|
383 |
/**
|
|
|
384 |
* Runs as last test to drop tables.
|
|
|
385 |
*
|
|
|
386 |
* @access public
|
|
|
387 |
*/
|
|
|
388 |
function end() {
|
|
|
389 |
if (isset($this->_fixtures) && isset($this->db)) {
|
|
|
390 |
if ($this->dropTables) {
|
|
|
391 |
foreach (array_reverse($this->_fixtures) as $fixture) {
|
|
|
392 |
$fixture->drop($this->db);
|
|
|
393 |
}
|
|
|
394 |
}
|
|
|
395 |
$this->db->sources(true);
|
|
|
396 |
Configure::write('Cache.disable', false);
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
if (class_exists('ClassRegistry')) {
|
|
|
400 |
ClassRegistry::flush();
|
|
|
401 |
}
|
|
|
402 |
}
|
|
|
403 |
/**
|
|
|
404 |
* Announces the end of a test.
|
|
|
405 |
*
|
|
|
406 |
* @param string $method Test method just finished.
|
|
|
407 |
*
|
|
|
408 |
* @access public
|
|
|
409 |
*/
|
|
|
410 |
function after($method) {
|
|
|
411 |
if (isset($this->_fixtures) && isset($this->db) && !in_array(strtolower($method), array('start', 'end'))) {
|
|
|
412 |
foreach ($this->_fixtures as $fixture) {
|
|
|
413 |
$fixture->truncate($this->db);
|
|
|
414 |
}
|
|
|
415 |
$this->__truncated = true;
|
|
|
416 |
} else {
|
|
|
417 |
$this->__truncated = false;
|
|
|
418 |
}
|
|
|
419 |
|
|
|
420 |
if (!in_array(strtolower($method), $this->methods)) {
|
|
|
421 |
$this->endTest($method);
|
|
|
422 |
}
|
|
|
423 |
$this->_should_skip = false;
|
|
|
424 |
|
|
|
425 |
parent::after($method);
|
|
|
426 |
}
|
|
|
427 |
/**
|
|
|
428 |
* Gets a list of test names. Normally that will be all internal methods that start with the
|
|
|
429 |
* name "test". This method should be overridden if you want a different rule.
|
|
|
430 |
*
|
|
|
431 |
* @return array List of test names.
|
|
|
432 |
*
|
|
|
433 |
* @access public
|
|
|
434 |
*/
|
|
|
435 |
function getTests() {
|
|
|
436 |
$methods = array_diff(parent::getTests(), array('testAction', 'testaction'));
|
|
|
437 |
$methods = array_merge(array_merge(array('start', 'startCase'), $methods), array('endCase', 'end'));
|
|
|
438 |
return $methods;
|
|
|
439 |
}
|
|
|
440 |
/**
|
|
|
441 |
* Chooses which fixtures to load for a given test
|
|
|
442 |
*
|
|
|
443 |
* @param string $fixture Each parameter is a model name that corresponds to a fixture, i.e. 'Post', 'Author', etc.
|
|
|
444 |
* @access public
|
|
|
445 |
* @see CakeTestCase::$autoFixtures
|
|
|
446 |
*/
|
|
|
447 |
function loadFixtures() {
|
|
|
448 |
$args = func_get_args();
|
|
|
449 |
foreach ($args as $class) {
|
|
|
450 |
if (isset($this->_fixtureClassMap[$class])) {
|
|
|
451 |
$fixture = $this->_fixtures[$this->_fixtureClassMap[$class]];
|
|
|
452 |
|
|
|
453 |
$fixture->truncate($this->db);
|
|
|
454 |
$fixture->insert($this->db);
|
|
|
455 |
} else {
|
|
|
456 |
trigger_error("Non-existent fixture class {$class} referenced in test", E_USER_WARNING);
|
|
|
457 |
}
|
|
|
458 |
}
|
|
|
459 |
}
|
|
|
460 |
/**
|
|
|
461 |
* Takes an array $expected and generates a regex from it to match the provided $string. Samples for $expected:
|
|
|
462 |
*
|
|
|
463 |
* Checks for an input tag with a name attribute (contains any non-empty value) and an id attribute that contains 'my-input':
|
|
|
464 |
* array('input' => array('name', 'id' => 'my-input'))
|
|
|
465 |
*
|
|
|
466 |
* Checks for two p elements with some text in them:
|
|
|
467 |
* array(
|
|
|
468 |
* array('p' => true),
|
|
|
469 |
* 'textA',
|
|
|
470 |
* '/p',
|
|
|
471 |
* array('p' => true),
|
|
|
472 |
* 'textB',
|
|
|
473 |
* '/p'
|
|
|
474 |
* )
|
|
|
475 |
*
|
|
|
476 |
* You can also specify a pattern expression as part of the attribute values, or the tag being defined,
|
|
|
477 |
* if you prepend the value with preg: and enclose it with slashes, like so:
|
|
|
478 |
* array(
|
|
|
479 |
* array('input' => array('name', 'id' => 'preg:/FieldName\d+/')),
|
|
|
480 |
* 'preg:/My\s+field/'
|
|
|
481 |
* )
|
|
|
482 |
*
|
|
|
483 |
* Important: This function is very forgiving about whitespace and also accepts any permutation of attribute order.
|
|
|
484 |
* It will also allow whitespaces between specified tags.
|
|
|
485 |
*
|
|
|
486 |
* @param string $string An HTML/XHTML/XML string
|
|
|
487 |
* @param array $expected An array, see above
|
|
|
488 |
* @param string $message SimpleTest failure output string
|
|
|
489 |
* @access public
|
|
|
490 |
*/
|
|
|
491 |
function assertTags($string, $expected, $fullDebug = false) {
|
|
|
492 |
$regex = array();
|
|
|
493 |
$normalized = array();
|
|
|
494 |
foreach ((array) $expected as $key => $val) {
|
|
|
495 |
if (!is_numeric($key)) {
|
|
|
496 |
$normalized[] = array($key => $val);
|
|
|
497 |
} else {
|
|
|
498 |
$normalized[] = $val;
|
|
|
499 |
}
|
|
|
500 |
}
|
|
|
501 |
$i = 0;
|
|
|
502 |
foreach ($normalized as $tags) {
|
|
|
503 |
$i++;
|
|
|
504 |
if (is_string($tags) && $tags{0} == '<') {
|
|
|
505 |
$tags = array(substr($tags, 1) => array());
|
|
|
506 |
} elseif (is_string($tags)) {
|
|
|
507 |
if (preg_match('/^\*?\//', $tags, $match)) {
|
|
|
508 |
$prefix = array(null, null);
|
|
|
509 |
if ($match[0] == '*/') {
|
|
|
510 |
$prefix = array('Anything, ', '.*?');
|
|
|
511 |
}
|
|
|
512 |
$regex[] = array(
|
|
|
513 |
sprintf('%sClose %s tag', $prefix[0], substr($tags, strlen($match[0]))),
|
|
|
514 |
sprintf('%s<[\s]*\/[\s]*%s[\s]*>[\n\r]*', $prefix[1], substr($tags, strlen($match[0]))),
|
|
|
515 |
$i,
|
|
|
516 |
);
|
|
|
517 |
continue;
|
|
|
518 |
}
|
|
|
519 |
if (!empty($tags) && preg_match('/^preg\:\/(.+)\/$/i', $tags, $matches)) {
|
|
|
520 |
$tags = $matches[1];
|
|
|
521 |
$type = 'Regex matches';
|
|
|
522 |
} else {
|
|
|
523 |
$tags = preg_quote($tags, '/');
|
|
|
524 |
$type = 'Text equals';
|
|
|
525 |
}
|
|
|
526 |
$regex[] = array(
|
|
|
527 |
sprintf('%s "%s"', $type, $tags),
|
|
|
528 |
$tags,
|
|
|
529 |
$i,
|
|
|
530 |
);
|
|
|
531 |
continue;
|
|
|
532 |
}
|
|
|
533 |
foreach ($tags as $tag => $attributes) {
|
|
|
534 |
$regex[] = array(
|
|
|
535 |
sprintf('Open %s tag', $tag),
|
|
|
536 |
sprintf('[\s]*<%s', preg_quote($tag, '/')),
|
|
|
537 |
$i,
|
|
|
538 |
);
|
|
|
539 |
if ($attributes === true) {
|
|
|
540 |
$attributes = array();
|
|
|
541 |
}
|
|
|
542 |
$attrs = array();
|
|
|
543 |
$explanations = array();
|
|
|
544 |
foreach ($attributes as $attr => $val) {
|
|
|
545 |
if (is_numeric($attr) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
|
|
|
546 |
$attrs[] = $matches[1];
|
|
|
547 |
$explanations[] = sprintf('Regex "%s" matches', $matches[1]);
|
|
|
548 |
continue;
|
|
|
549 |
} else {
|
|
|
550 |
$quotes = '"';
|
|
|
551 |
if (is_numeric($attr)) {
|
|
|
552 |
$attr = $val;
|
|
|
553 |
$val = '.+?';
|
|
|
554 |
$explanations[] = sprintf('Attribute "%s" present', $attr);
|
|
|
555 |
} else if (!empty($val) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
|
|
|
556 |
$quotes = '"?';
|
|
|
557 |
$val = $matches[1];
|
|
|
558 |
$explanations[] = sprintf('Attribute "%s" matches "%s"', $attr, $val);
|
|
|
559 |
} else {
|
|
|
560 |
$explanations[] = sprintf('Attribute "%s" == "%s"', $attr, $val);
|
|
|
561 |
$val = preg_quote($val, '/');
|
|
|
562 |
}
|
|
|
563 |
$attrs[] = '[\s]+'.preg_quote($attr, '/').'='.$quotes.$val.$quotes;
|
|
|
564 |
}
|
|
|
565 |
}
|
|
|
566 |
if ($attrs) {
|
|
|
567 |
$permutations = $this->__array_permute($attrs);
|
|
|
568 |
$permutationTokens = array();
|
|
|
569 |
foreach ($permutations as $permutation) {
|
|
|
570 |
$permutationTokens[] = join('', $permutation);
|
|
|
571 |
}
|
|
|
572 |
$regex[] = array(
|
|
|
573 |
sprintf('%s', join(', ', $explanations)),
|
|
|
574 |
$permutationTokens,
|
|
|
575 |
$i,
|
|
|
576 |
);
|
|
|
577 |
}
|
|
|
578 |
$regex[] = array(
|
|
|
579 |
sprintf('End %s tag', $tag),
|
|
|
580 |
'[\s]*\/?[\s]*>[\n\r]*',
|
|
|
581 |
$i,
|
|
|
582 |
);
|
|
|
583 |
}
|
|
|
584 |
}
|
|
|
585 |
foreach ($regex as $i => $assertation) {
|
|
|
586 |
list($description, $expressions, $itemNum) = $assertation;
|
|
|
587 |
$matches = false;
|
|
|
588 |
foreach ((array)$expressions as $expression) {
|
|
|
589 |
if (preg_match(sprintf('/^%s/s', $expression), $string, $match)) {
|
|
|
590 |
$matches = true;
|
|
|
591 |
$string = substr($string, strlen($match[0]));
|
|
|
592 |
break;
|
|
|
593 |
}
|
|
|
594 |
}
|
|
|
595 |
if (!$matches) {
|
|
|
596 |
$this->assert(new TrueExpectation(), false, sprintf('Item #%d / regex #%d failed: %s', $itemNum, $i, $description));
|
|
|
597 |
if ($fullDebug) {
|
|
|
598 |
debug($string, true);
|
|
|
599 |
debug($regex, true);
|
|
|
600 |
}
|
|
|
601 |
return false;
|
|
|
602 |
}
|
|
|
603 |
}
|
|
|
604 |
return $this->assert(new TrueExpectation(), true, '%s');
|
|
|
605 |
}
|
|
|
606 |
/**
|
|
|
607 |
* Generates all permutation of an array $items and returns them in a new array.
|
|
|
608 |
*
|
|
|
609 |
* @param array $items An array of items
|
|
|
610 |
* @return array
|
|
|
611 |
* @access public
|
|
|
612 |
*/
|
|
|
613 |
function __array_permute($items, $perms = array()) {
|
|
|
614 |
static $permuted;
|
|
|
615 |
if (empty($perms)) {
|
|
|
616 |
$permuted = array();
|
|
|
617 |
}
|
|
|
618 |
|
|
|
619 |
if (empty($items)) {
|
|
|
620 |
$permuted[] = $perms;
|
|
|
621 |
} else {
|
|
|
622 |
$numItems = count($items) - 1;
|
|
|
623 |
for ($i = $numItems; $i >= 0; --$i) {
|
|
|
624 |
$newItems = $items;
|
|
|
625 |
$newPerms = $perms;
|
|
|
626 |
list($tmp) = array_splice($newItems, $i, 1);
|
|
|
627 |
array_unshift($newPerms, $tmp);
|
|
|
628 |
$this->__array_permute($newItems, $newPerms);
|
|
|
629 |
}
|
|
|
630 |
return $permuted;
|
|
|
631 |
}
|
|
|
632 |
}
|
|
|
633 |
/**
|
|
|
634 |
* Initialize DB connection.
|
|
|
635 |
*
|
|
|
636 |
* @access protected
|
|
|
637 |
*/
|
|
|
638 |
function _initDb() {
|
|
|
639 |
$testDbAvailable = in_array('test', array_keys(ConnectionManager::enumConnectionObjects()));
|
|
|
640 |
|
|
|
641 |
$_prefix = null;
|
|
|
642 |
|
|
|
643 |
if ($testDbAvailable) {
|
|
|
644 |
// Try for test DB
|
|
|
645 |
restore_error_handler();
|
|
|
646 |
@$db =& ConnectionManager::getDataSource('test');
|
|
|
647 |
set_error_handler('simpleTestErrorHandler');
|
|
|
648 |
$testDbAvailable = $db->isConnected();
|
|
|
649 |
}
|
|
|
650 |
|
|
|
651 |
// Try for default DB
|
|
|
652 |
if (!$testDbAvailable) {
|
|
|
653 |
$db =& ConnectionManager::getDataSource('default');
|
|
|
654 |
$_prefix = $db->config['prefix'];
|
|
|
655 |
$db->config['prefix'] = 'test_suite_';
|
|
|
656 |
}
|
|
|
657 |
|
|
|
658 |
ConnectionManager::create('test_suite', $db->config);
|
|
|
659 |
$db->config['prefix'] = $_prefix;
|
|
|
660 |
|
|
|
661 |
// Get db connection
|
|
|
662 |
$this->db =& ConnectionManager::getDataSource('test_suite');
|
|
|
663 |
$this->db->cacheSources = false;
|
|
|
664 |
|
|
|
665 |
ClassRegistry::config(array('ds' => 'test_suite'));
|
|
|
666 |
}
|
|
|
667 |
/**
|
|
|
668 |
* Load fixtures specified in var $fixtures.
|
|
|
669 |
*
|
|
|
670 |
* @access private
|
|
|
671 |
*/
|
|
|
672 |
function _loadFixtures() {
|
|
|
673 |
if (!isset($this->fixtures) || empty($this->fixtures)) {
|
|
|
674 |
return;
|
|
|
675 |
}
|
|
|
676 |
|
|
|
677 |
if (!is_array($this->fixtures)) {
|
|
|
678 |
$this->fixtures = array_map('trim', explode(',', $this->fixtures));
|
|
|
679 |
}
|
|
|
680 |
|
|
|
681 |
$this->_fixtures = array();
|
|
|
682 |
|
|
|
683 |
foreach ($this->fixtures as $index => $fixture) {
|
|
|
684 |
$fixtureFile = null;
|
|
|
685 |
|
|
|
686 |
if (strpos($fixture, 'core.') === 0) {
|
|
|
687 |
$fixture = substr($fixture, strlen('core.'));
|
|
|
688 |
foreach (Configure::corePaths('cake') as $key => $path) {
|
|
|
689 |
$fixturePaths[] = $path . 'tests' . DS . 'fixtures';
|
|
|
690 |
}
|
|
|
691 |
} elseif (strpos($fixture, 'app.') === 0) {
|
|
|
692 |
$fixture = substr($fixture, strlen('app.'));
|
|
|
693 |
$fixturePaths = array(
|
|
|
694 |
TESTS . 'fixtures',
|
|
|
695 |
VENDORS . 'tests' . DS . 'fixtures'
|
|
|
696 |
);
|
|
|
697 |
} elseif (strpos($fixture, 'plugin.') === 0) {
|
|
|
698 |
$parts = explode('.', $fixture, 3);
|
|
|
699 |
$pluginName = $parts[1];
|
|
|
700 |
$fixture = $parts[2];
|
|
|
701 |
$fixturePaths = array(
|
|
|
702 |
APP . 'plugins' . DS . $pluginName . DS . 'tests' . DS . 'fixtures',
|
|
|
703 |
TESTS . 'fixtures',
|
|
|
704 |
VENDORS . 'tests' . DS . 'fixtures'
|
|
|
705 |
);
|
|
|
706 |
$pluginPaths = Configure::read('pluginPaths');
|
|
|
707 |
foreach ($pluginPaths as $path) {
|
|
|
708 |
if (file_exists($path . $pluginName . DS . 'tests' . DS. 'fixtures')) {
|
|
|
709 |
$fixturePaths[0] = $path . $pluginName . DS . 'tests' . DS. 'fixtures';
|
|
|
710 |
break;
|
|
|
711 |
}
|
|
|
712 |
}
|
|
|
713 |
} else {
|
|
|
714 |
$fixturePaths = array(
|
|
|
715 |
TESTS . 'fixtures',
|
|
|
716 |
VENDORS . 'tests' . DS . 'fixtures',
|
|
|
717 |
TEST_CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures'
|
|
|
718 |
);
|
|
|
719 |
}
|
|
|
720 |
|
|
|
721 |
foreach ($fixturePaths as $path) {
|
|
|
722 |
if (is_readable($path . DS . $fixture . '_fixture.php')) {
|
|
|
723 |
$fixtureFile = $path . DS . $fixture . '_fixture.php';
|
|
|
724 |
break;
|
|
|
725 |
}
|
|
|
726 |
}
|
|
|
727 |
|
|
|
728 |
if (isset($fixtureFile)) {
|
|
|
729 |
require_once($fixtureFile);
|
|
|
730 |
$fixtureClass = Inflector::camelize($fixture) . 'Fixture';
|
|
|
731 |
$this->_fixtures[$this->fixtures[$index]] =& new $fixtureClass($this->db);
|
|
|
732 |
$this->_fixtureClassMap[Inflector::camelize($fixture)] = $this->fixtures[$index];
|
|
|
733 |
}
|
|
|
734 |
}
|
|
|
735 |
|
|
|
736 |
if (empty($this->_fixtures)) {
|
|
|
737 |
unset($this->_fixtures);
|
|
|
738 |
}
|
|
|
739 |
}
|
|
|
740 |
}
|
|
|
741 |
?>
|