Base for a static organization website

CakeTestCase.php 22KB

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