1: <?php
2: /**
3: * CakeTestCase file
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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14: * @package Cake.TestSuite
15: * @since CakePHP(tm) v 1.2.0.4667
16: * @license http://www.opensource.org/licenses/mit-license.php MIT License
17: */
18:
19: App::uses('CakeFixtureManager', 'TestSuite/Fixture');
20: App::uses('CakeTestFixture', 'TestSuite/Fixture');
21:
22: /**
23: * CakeTestCase class
24: *
25: * @package Cake.TestSuite
26: */
27: abstract class CakeTestCase extends PHPUnit_Framework_TestCase {
28:
29: /**
30: * The class responsible for managing the creation, loading and removing of fixtures
31: *
32: * @var CakeFixtureManager
33: */
34: public $fixtureManager = null;
35:
36: /**
37: * By default, all fixtures attached to this class will be truncated and reloaded after each test.
38: * Set this to false to handle manually
39: *
40: * @var array
41: */
42: public $autoFixtures = true;
43:
44: /**
45: * Control table create/drops on each test method.
46: *
47: * Set this to false to avoid tables to be dropped if they already exist
48: * between each test method. Tables will still be dropped at the
49: * end of each test runner execution.
50: *
51: * @var bool
52: */
53: public $dropTables = true;
54:
55: /**
56: * Configure values to restore at end of test.
57: *
58: * @var array
59: */
60: protected $_configure = array();
61:
62: /**
63: * Path settings to restore at the end of the test.
64: *
65: * @var array
66: */
67: protected $_pathRestore = array();
68:
69: /**
70: * Runs the test case and collects the results in a TestResult object.
71: * If no TestResult object is passed a new one will be created.
72: * This method is run for each test method in this class
73: *
74: * @param PHPUnit_Framework_TestResult $result The test result object
75: * @return PHPUnit_Framework_TestResult
76: * @throws InvalidArgumentException
77: */
78: public function run(PHPUnit_Framework_TestResult $result = null) {
79: if (!empty($this->fixtureManager)) {
80: $this->fixtureManager->load($this);
81: }
82: $result = parent::run($result);
83: if (!empty($this->fixtureManager)) {
84: $this->fixtureManager->unload($this);
85: }
86: return $result;
87: }
88:
89: /**
90: * Called when a test case method is about to start (to be overridden when needed.)
91: *
92: * @param string $method Test method about to get executed.
93: * @return void
94: */
95: public function startTest($method) {
96: }
97:
98: /**
99: * Called when a test case method has been executed (to be overridden when needed.)
100: *
101: * @param string $method Test method about that was executed.
102: * @return void
103: */
104: public function endTest($method) {
105: }
106:
107: /**
108: * Overrides SimpleTestCase::skipIf to provide a boolean return value
109: *
110: * @param bool $shouldSkip Whether or not the test should be skipped.
111: * @param string $message The message to display.
112: * @return bool
113: */
114: public function skipIf($shouldSkip, $message = '') {
115: if ($shouldSkip) {
116: $this->markTestSkipped($message);
117: }
118: return $shouldSkip;
119: }
120:
121: /**
122: * Setup the test case, backup the static object values so they can be restored.
123: * Specifically backs up the contents of Configure and paths in App if they have
124: * not already been backed up.
125: *
126: * @return void
127: */
128: public function setUp() {
129: parent::setUp();
130:
131: if (empty($this->_configure)) {
132: $this->_configure = Configure::read();
133: }
134: if (empty($this->_pathRestore)) {
135: $this->_pathRestore = App::paths();
136: }
137: if (class_exists('Router', false)) {
138: Router::reload();
139: }
140: }
141:
142: /**
143: * teardown any static object changes and restore them.
144: *
145: * @return void
146: */
147: public function tearDown() {
148: parent::tearDown();
149: App::build($this->_pathRestore, App::RESET);
150: if (class_exists('ClassRegistry', false)) {
151: ClassRegistry::flush();
152: }
153: if (!empty($this->_configure)) {
154: Configure::clear();
155: Configure::write($this->_configure);
156: }
157: if (isset($_GET['debug']) && $_GET['debug']) {
158: ob_flush();
159: }
160: }
161:
162: /**
163: * See CakeTestSuiteDispatcher::date()
164: *
165: * @param string $format format to be used.
166: * @return string
167: */
168: public static function date($format = 'Y-m-d H:i:s') {
169: return CakeTestSuiteDispatcher::date($format);
170: }
171:
172: // @codingStandardsIgnoreStart PHPUnit overrides don't match CakePHP
173:
174: /**
175: * Announces the start of a test.
176: *
177: * @return void
178: */
179: protected function assertPreConditions() {
180: parent::assertPreConditions();
181: $this->startTest($this->getName());
182: }
183:
184: /**
185: * Announces the end of a test.
186: *
187: * @return void
188: */
189: protected function assertPostConditions() {
190: parent::assertPostConditions();
191: $this->endTest($this->getName());
192: }
193:
194: // @codingStandardsIgnoreEnd
195:
196: /**
197: * Chooses which fixtures to load for a given test
198: *
199: * Each parameter is a model name that corresponds to a fixture, i.e. 'Post', 'Author', etc.
200: *
201: * @return void
202: * @see CakeTestCase::$autoFixtures
203: * @throws Exception when no fixture manager is available.
204: */
205: public function loadFixtures() {
206: if (empty($this->fixtureManager)) {
207: throw new Exception(__d('cake_dev', 'No fixture manager to load the test fixture'));
208: }
209: $args = func_get_args();
210: foreach ($args as $class) {
211: $this->fixtureManager->loadSingle($class, null, $this->dropTables);
212: }
213: }
214:
215: /**
216: * Assert text equality, ignoring differences in newlines.
217: * Helpful for doing cross platform tests of blocks of text.
218: *
219: * @param string $expected The expected value.
220: * @param string $result The actual value.
221: * @param string $message The message to use for failure.
222: * @return bool
223: */
224: public function assertTextNotEquals($expected, $result, $message = '') {
225: $expected = str_replace(array("\r\n", "\r"), "\n", $expected);
226: $result = str_replace(array("\r\n", "\r"), "\n", $result);
227: return $this->assertNotEquals($expected, $result, $message);
228: }
229:
230: /**
231: * Assert text equality, ignoring differences in newlines.
232: * Helpful for doing cross platform tests of blocks of text.
233: *
234: * @param string $expected The expected value.
235: * @param string $result The actual value.
236: * @param string $message message The message to use for failure.
237: * @return bool
238: */
239: public function assertTextEquals($expected, $result, $message = '') {
240: $expected = str_replace(array("\r\n", "\r"), "\n", $expected);
241: $result = str_replace(array("\r\n", "\r"), "\n", $result);
242: return $this->assertEquals($expected, $result, $message);
243: }
244:
245: /**
246: * Asserts that a string starts with a given prefix, ignoring differences in newlines.
247: * Helpful for doing cross platform tests of blocks of text.
248: *
249: * @param string $prefix The prefix to check for.
250: * @param string $string The string to search in.
251: * @param string $message The message to use for failure.
252: * @return bool
253: */
254: public function assertTextStartsWith($prefix, $string, $message = '') {
255: $prefix = str_replace(array("\r\n", "\r"), "\n", $prefix);
256: $string = str_replace(array("\r\n", "\r"), "\n", $string);
257: return $this->assertStringStartsWith($prefix, $string, $message);
258: }
259:
260: /**
261: * Asserts that a string starts not with a given prefix, ignoring differences in newlines.
262: * Helpful for doing cross platform tests of blocks of text.
263: *
264: * @param string $prefix The prefix to not find.
265: * @param string $string The string to search.
266: * @param string $message The message to use for failure.
267: * @return bool
268: */
269: public function assertTextStartsNotWith($prefix, $string, $message = '') {
270: $prefix = str_replace(array("\r\n", "\r"), "\n", $prefix);
271: $string = str_replace(array("\r\n", "\r"), "\n", $string);
272: return $this->assertStringStartsNotWith($prefix, $string, $message);
273: }
274:
275: /**
276: * Asserts that a string ends with a given prefix, ignoring differences in newlines.
277: * Helpful for doing cross platform tests of blocks of text.
278: *
279: * @param string $suffix The suffix to find.
280: * @param string $string The string to search.
281: * @param string $message The message to use for failure.
282: * @return bool
283: */
284: public function assertTextEndsWith($suffix, $string, $message = '') {
285: $suffix = str_replace(array("\r\n", "\r"), "\n", $suffix);
286: $string = str_replace(array("\r\n", "\r"), "\n", $string);
287: return $this->assertStringEndsWith($suffix, $string, $message);
288: }
289:
290: /**
291: * Asserts that a string ends not with a given prefix, ignoring differences in newlines.
292: * Helpful for doing cross platform tests of blocks of text.
293: *
294: * @param string $suffix The suffix to not find.
295: * @param string $string The string to search.
296: * @param string $message The message to use for failure.
297: * @return bool
298: */
299: public function assertTextEndsNotWith($suffix, $string, $message = '') {
300: $suffix = str_replace(array("\r\n", "\r"), "\n", $suffix);
301: $string = str_replace(array("\r\n", "\r"), "\n", $string);
302: return $this->assertStringEndsNotWith($suffix, $string, $message);
303: }
304:
305: /**
306: * Assert that a string contains another string, ignoring differences in newlines.
307: * Helpful for doing cross platform tests of blocks of text.
308: *
309: * @param string $needle The string to search for.
310: * @param string $haystack The string to search through.
311: * @param string $message The message to display on failure.
312: * @param bool $ignoreCase Whether or not the search should be case-sensitive.
313: * @return bool
314: */
315: public function assertTextContains($needle, $haystack, $message = '', $ignoreCase = false) {
316: $needle = str_replace(array("\r\n", "\r"), "\n", $needle);
317: $haystack = str_replace(array("\r\n", "\r"), "\n", $haystack);
318: return $this->assertContains($needle, $haystack, $message, $ignoreCase);
319: }
320:
321: /**
322: * Assert that a text doesn't contain another text, ignoring differences in newlines.
323: * Helpful for doing cross platform tests of blocks of text.
324: *
325: * @param string $needle The string to search for.
326: * @param string $haystack The string to search through.
327: * @param string $message The message to display on failure.
328: * @param bool $ignoreCase Whether or not the search should be case-sensitive.
329: * @return bool
330: */
331: public function assertTextNotContains($needle, $haystack, $message = '', $ignoreCase = false) {
332: $needle = str_replace(array("\r\n", "\r"), "\n", $needle);
333: $haystack = str_replace(array("\r\n", "\r"), "\n", $haystack);
334: return $this->assertNotContains($needle, $haystack, $message, $ignoreCase);
335: }
336:
337: /**
338: * Takes an array $expected and generates a regex from it to match the provided $string.
339: * Samples for $expected:
340: *
341: * Checks for an input tag with a name attribute (contains any non-empty value) and an id
342: * attribute that contains 'my-input':
343: *
344: * ```
345: * array('input' => array('name', 'id' => 'my-input'))
346: * ```
347: *
348: * Checks for two p elements with some text in them:
349: *
350: * ```
351: * array(
352: * array('p' => true),
353: * 'textA',
354: * '/p',
355: * array('p' => true),
356: * 'textB',
357: * '/p'
358: * )
359: * ```
360: *
361: * You can also specify a pattern expression as part of the attribute values, or the tag
362: * being defined, if you prepend the value with preg: and enclose it with slashes, like so:
363: *
364: * ```
365: * array(
366: * array('input' => array('name', 'id' => 'preg:/FieldName\d+/')),
367: * 'preg:/My\s+field/'
368: * )
369: * ```
370: *
371: * Important: This function is very forgiving about whitespace and also accepts any
372: * permutation of attribute order. It will also allow whitespace between specified tags.
373: *
374: * @param string $string An HTML/XHTML/XML string
375: * @param array $expected An array, see above
376: * @param string $fullDebug Whether or not more verbose output should be used.
377: * @return bool
378: */
379: public function assertTags($string, $expected, $fullDebug = false) {
380: $regex = array();
381: $normalized = array();
382: foreach ((array)$expected as $key => $val) {
383: if (!is_numeric($key)) {
384: $normalized[] = array($key => $val);
385: } else {
386: $normalized[] = $val;
387: }
388: }
389: $i = 0;
390: foreach ($normalized as $tags) {
391: if (!is_array($tags)) {
392: $tags = (string)$tags;
393: }
394: $i++;
395: if (is_string($tags) && $tags{0} === '<') {
396: $tags = array(substr($tags, 1) => array());
397: } elseif (is_string($tags)) {
398: $tagsTrimmed = preg_replace('/\s+/m', '', $tags);
399:
400: if (preg_match('/^\*?\//', $tags, $match) && $tagsTrimmed !== '//') {
401: $prefix = array(null, null);
402:
403: if ($match[0] === '*/') {
404: $prefix = array('Anything, ', '.*?');
405: }
406: $regex[] = array(
407: sprintf('%sClose %s tag', $prefix[0], substr($tags, strlen($match[0]))),
408: sprintf('%s<[\s]*\/[\s]*%s[\s]*>[\n\r]*', $prefix[1], substr($tags, strlen($match[0]))),
409: $i,
410: );
411: continue;
412: }
413: if (!empty($tags) && preg_match('/^preg\:\/(.+)\/$/i', $tags, $matches)) {
414: $tags = $matches[1];
415: $type = 'Regex matches';
416: } else {
417: $tags = preg_quote($tags, '/');
418: $type = 'Text equals';
419: }
420: $regex[] = array(
421: sprintf('%s "%s"', $type, $tags),
422: $tags,
423: $i,
424: );
425: continue;
426: }
427: foreach ($tags as $tag => $attributes) {
428: $regex[] = array(
429: sprintf('Open %s tag', $tag),
430: sprintf('[\s]*<%s', preg_quote($tag, '/')),
431: $i,
432: );
433: if ($attributes === true) {
434: $attributes = array();
435: }
436: $attrs = array();
437: $explanations = array();
438: $i = 1;
439: foreach ($attributes as $attr => $val) {
440: if (is_numeric($attr) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
441: $attrs[] = $matches[1];
442: $explanations[] = sprintf('Regex "%s" matches', $matches[1]);
443: continue;
444: } else {
445: $quotes = '["\']';
446: if (is_numeric($attr)) {
447: $attr = $val;
448: $val = '.+?';
449: $explanations[] = sprintf('Attribute "%s" present', $attr);
450: } elseif (!empty($val) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
451: $val = str_replace(
452: array('.*', '.+'),
453: array('.*?', '.+?'),
454: $matches[1]
455: );
456: $quotes = $val !== $matches[1] ? '["\']' : '["\']?';
457:
458: $explanations[] = sprintf('Attribute "%s" matches "%s"', $attr, $val);
459: } else {
460: $explanations[] = sprintf('Attribute "%s" == "%s"', $attr, $val);
461: $val = preg_quote($val, '/');
462: }
463: $attrs[] = '[\s]+' . preg_quote($attr, '/') . '=' . $quotes . $val . $quotes;
464: }
465: $i++;
466: }
467: if ($attrs) {
468: $regex[] = array(
469: 'explains' => $explanations,
470: 'attrs' => $attrs,
471: );
472: }
473: $regex[] = array(
474: sprintf('End %s tag', $tag),
475: '[\s]*\/?[\s]*>[\n\r]*',
476: $i,
477: );
478: }
479: }
480: foreach ($regex as $i => $assertion) {
481: $matches = false;
482: if (isset($assertion['attrs'])) {
483: $string = $this->_assertAttributes($assertion, $string);
484: continue;
485: }
486:
487: list($description, $expressions, $itemNum) = $assertion;
488: foreach ((array)$expressions as $expression) {
489: if (preg_match(sprintf('/^%s/s', $expression), $string, $match)) {
490: $matches = true;
491: $string = substr($string, strlen($match[0]));
492: break;
493: }
494: }
495: if (!$matches) {
496: $this->assertTrue(false, sprintf('Item #%d / regex #%d failed: %s', $itemNum, $i, $description));
497: if ($fullDebug) {
498: debug($string, true);
499: debug($regex, true);
500: }
501: return false;
502: }
503: }
504:
505: $this->assertTrue(true, '%s');
506: return true;
507: }
508:
509: /**
510: * Check the attributes as part of an assertTags() check.
511: *
512: * @param array $assertions Assertions to run.
513: * @param string $string The HTML string to check.
514: * @return void
515: */
516: protected function _assertAttributes($assertions, $string) {
517: $asserts = $assertions['attrs'];
518: $explains = $assertions['explains'];
519: $len = count($asserts);
520: do {
521: $matches = false;
522: foreach ($asserts as $j => $assert) {
523: if (preg_match(sprintf('/^%s/s', $assert), $string, $match)) {
524: $matches = true;
525: $string = substr($string, strlen($match[0]));
526: array_splice($asserts, $j, 1);
527: array_splice($explains, $j, 1);
528: break;
529: }
530: }
531: if ($matches === false) {
532: $this->assertTrue(false, 'Attribute did not match. Was expecting ' . $explains[$j]);
533: }
534: $len = count($asserts);
535: } while ($len > 0);
536: return $string;
537: }
538:
539: // @codingStandardsIgnoreStart
540:
541: /**
542: * Compatibility wrapper function for assertEquals
543: *
544: * @param mixed $result
545: * @param mixed $expected
546: * @param string $message the text to display if the assertion is not correct
547: * @deprecated 3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0
548: * @return void
549: */
550: protected static function assertEqual($result, $expected, $message = '') {
551: return static::assertEquals($expected, $result, $message);
552: }
553:
554: /**
555: * Compatibility wrapper function for assertNotEquals
556: *
557: * @param mixed $result
558: * @param mixed $expected
559: * @param string $message the text to display if the assertion is not correct
560: * @deprecated 3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0
561: * @return void
562: */
563: protected static function assertNotEqual($result, $expected, $message = '') {
564: return static::assertNotEquals($expected, $result, $message);
565: }
566:
567: /**
568: * Compatibility wrapper function for assertRegexp
569: *
570: * @param mixed $pattern a regular expression
571: * @param string $string the text to be matched
572: * @param string $message the text to display if the assertion is not correct
573: * @deprecated 3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0
574: * @return void
575: */
576: protected static function assertPattern($pattern, $string, $message = '') {
577: return static::assertRegExp($pattern, $string, $message);
578: }
579:
580: /**
581: * Compatibility wrapper function for assertEquals
582: *
583: * @param mixed $actual
584: * @param mixed $expected
585: * @param string $message the text to display if the assertion is not correct
586: * @deprecated 3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0
587: * @return void
588: */
589: protected static function assertIdentical($actual, $expected, $message = '') {
590: return static::assertSame($expected, $actual, $message);
591: }
592:
593: /**
594: * Compatibility wrapper function for assertNotEquals
595: *
596: * @param mixed $actual
597: * @param mixed $expected
598: * @param string $message the text to display if the assertion is not correct
599: * @deprecated 3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0
600: * @return void
601: */
602: protected static function assertNotIdentical($actual, $expected, $message = '') {
603: return static::assertNotSame($expected, $actual, $message);
604: }
605:
606: /**
607: * Compatibility wrapper function for assertNotRegExp
608: *
609: * @param mixed $pattern a regular expression
610: * @param string $string the text to be matched
611: * @param string $message the text to display if the assertion is not correct
612: * @deprecated 3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0
613: * @return void
614: */
615: protected static function assertNoPattern($pattern, $string, $message = '') {
616: return static::assertNotRegExp($pattern, $string, $message);
617: }
618:
619: /**
620: * assert no errors
621: *
622: * @deprecated 3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0
623: * @return void
624: */
625: protected function assertNoErrors() {
626: }
627:
628: /**
629: * Compatibility wrapper function for setExpectedException
630: *
631: * @param mixed $expected the name of the Exception or error
632: * @param string $message the text to display if the assertion is not correct
633: * @deprecated 3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0
634: * @return void
635: */
636: protected function expectError($expected = false, $message = '') {
637: if (!$expected) {
638: $expected = 'Exception';
639: }
640: $this->setExpectedException($expected, $message);
641: }
642:
643: /**
644: * Compatibility wrapper function for setExpectedException
645: *
646: * @param mixed $name The name of the expected Exception.
647: * @param string $message the text to display if the assertion is not correct
648: * @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0.
649: * @return void
650: */
651: protected function expectException($name = 'Exception', $message = '') {
652: $this->setExpectedException($name, $message);
653: }
654:
655: /**
656: * Compatibility wrapper function for assertSame
657: *
658: * @param mixed $first
659: * @param mixed $second
660: * @param string $message the text to display if the assertion is not correct
661: * @deprecated 3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0
662: * @return void
663: */
664: protected static function assertReference(&$first, &$second, $message = '') {
665: return static::assertSame($first, $second, $message);
666: }
667:
668: /**
669: * Compatibility wrapper for assertIsA
670: *
671: * @param string $object
672: * @param string $type
673: * @param string $message
674: * @deprecated 3.0.0 This is a compatiblity wrapper for 1.x. It will be removed in 3.0
675: * @return void
676: */
677: protected static function assertIsA($object, $type, $message = '') {
678: return static::assertInstanceOf($type, $object, $message);
679: }
680:
681: /**
682: * Compatibility function to test if value is between an acceptable range
683: *
684: * @param mixed $result
685: * @param mixed $expected
686: * @param mixed $margin the rage of acceptation
687: * @param string $message the text to display if the assertion is not correct
688: * @return void
689: */
690: protected static function assertWithinMargin($result, $expected, $margin, $message = '') {
691: $upper = $result + $margin;
692: $lower = $result - $margin;
693: return static::assertTrue((($expected <= $upper) && ($expected >= $lower)), $message);
694: }
695:
696: /**
697: * Compatibility function for skipping.
698: *
699: * @param bool $condition Condition to trigger skipping
700: * @param string $message Message for skip
701: * @return bool
702: */
703: protected function skipUnless($condition, $message = '') {
704: if (!$condition) {
705: $this->markTestSkipped($message);
706: }
707: return $condition;
708: }
709: // @codingStandardsIgnoreEnd
710:
711: /**
712: * Mock a model, maintain fixtures and table association
713: *
714: * @param string $model The model to get a mock for.
715: * @param mixed $methods The list of methods to mock
716: * @param array $config The config data for the mock's constructor.
717: * @throws MissingModelException
718: * @return Model
719: */
720: public function getMockForModel($model, $methods = array(), $config = array()) {
721: $config += ClassRegistry::config('Model');
722:
723: list($plugin, $name) = pluginSplit($model, true);
724: App::uses($name, $plugin . 'Model');
725:
726: $config = array_merge((array)$config, array('name' => $name));
727: unset($config['ds']);
728:
729: if (!class_exists($name)) {
730: throw new MissingModelException(array($model));
731: }
732: $mock = $this->getMock($name, $methods, array($config));
733:
734: $availableDs = array_keys(ConnectionManager::enumConnectionObjects());
735:
736: if ($mock->useDbConfig !== 'test' && in_array('test_' . $mock->useDbConfig, $availableDs)) {
737: $mock->setDataSource('test_' . $mock->useDbConfig);
738: } else {
739: $mock->useDbConfig = 'test';
740: $mock->setDataSource('test');
741: }
742:
743: ClassRegistry::removeObject($name);
744: ClassRegistry::addObject($name, $mock);
745: return $mock;
746: }
747:
748: }
749: