CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

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

Generated using CakePHP API Docs