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