2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-21 23:55:39 +00:00
|
|
|
* CakeTestCase file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4667
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2010-12-08 06:19:36 +00:00
|
|
|
App::uses('CakeFixtureManager', 'TestSuite/Fixture');
|
|
|
|
App::uses('CakeTestFixture', 'TestSuite/Fixture');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-21 23:55:39 +00:00
|
|
|
* CakeTestCase class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-11 05:47:55 +00:00
|
|
|
abstract class CakeTestCase extends PHPUnit_Framework_TestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-28 04:07:56 +00:00
|
|
|
/**
|
2011-02-14 04:26:41 +00:00
|
|
|
* The class responsible for managing the creation, loading and removing of fixtures
|
2010-09-28 04:07:56 +00:00
|
|
|
*
|
|
|
|
* @var CakeFixtureManager
|
|
|
|
*/
|
|
|
|
public $fixtureManager = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* By default, all fixtures attached to this class will be truncated and reloaded after each test.
|
|
|
|
* Set this to false to handle manually
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $autoFixtures = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-09 21:49:49 +00:00
|
|
|
/**
|
2013-03-12 13:34:20 +00:00
|
|
|
* Control table create/drops on each test method.
|
|
|
|
*
|
2008-09-09 22:18:49 +00:00
|
|
|
* Set this to false to avoid tables to be dropped if they already exist
|
2013-03-12 13:34:20 +00:00
|
|
|
* between each test method. Tables will still be dropped at the
|
|
|
|
* end of each test runner execution.
|
2008-09-09 21:49:49 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-09-09 21:49:49 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $dropTables = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-26 01:36:49 +00:00
|
|
|
/**
|
|
|
|
* Configure values to restore at end of test.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_configure = array();
|
|
|
|
|
2011-08-30 02:07:23 +00:00
|
|
|
/**
|
|
|
|
* Path settings to restore at the end of the test.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_pathRestore = array();
|
2011-04-21 22:26:03 +00:00
|
|
|
|
2010-06-01 03:37:21 +00:00
|
|
|
/**
|
2012-03-05 02:51:44 +00:00
|
|
|
* Runs the test case and collects the results in a TestResult object.
|
|
|
|
* If no TestResult object is passed a new one will be created.
|
|
|
|
* This method is run for each test method in this class
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param PHPUnit_Framework_TestResult $result The test result object
|
2012-03-05 02:51:44 +00:00
|
|
|
* @return PHPUnit_Framework_TestResult
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function run(PHPUnit_Framework_TestResult $result = null) {
|
2017-03-25 11:18:12 +00:00
|
|
|
$level = ob_get_level();
|
|
|
|
|
2010-09-28 04:07:56 +00:00
|
|
|
if (!empty($this->fixtureManager)) {
|
|
|
|
$this->fixtureManager->load($this);
|
2010-06-01 03:37:21 +00:00
|
|
|
}
|
|
|
|
$result = parent::run($result);
|
2010-09-28 04:07:56 +00:00
|
|
|
if (!empty($this->fixtureManager)) {
|
|
|
|
$this->fixtureManager->unload($this);
|
2018-08-14 10:31:58 +00:00
|
|
|
unset($this->fixtureManager, $this->db);
|
2010-06-01 03:37:21 +00:00
|
|
|
}
|
2017-03-25 11:18:12 +00:00
|
|
|
|
|
|
|
for ($i = ob_get_level(); $i < $level; ++$i) {
|
|
|
|
ob_start();
|
|
|
|
}
|
|
|
|
|
2010-06-01 03:37:21 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-12-02 05:58:09 +00:00
|
|
|
* Called when a test case method is about to start (to be overridden when needed.)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @param string $method Test method about to get executed.
|
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function startTest($method) {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-12-02 05:58:09 +00:00
|
|
|
* Called when a test case method has been executed (to be overridden when needed.)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @param string $method Test method about that was executed.
|
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function endTest($method) {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Overrides SimpleTestCase::skipIf to provide a boolean return value
|
2009-03-21 23:55:39 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $shouldSkip Whether or not the test should be skipped.
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $message The message to display.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-04 18:17:08 +00:00
|
|
|
public function skipIf($shouldSkip, $message = '') {
|
|
|
|
if ($shouldSkip) {
|
|
|
|
$this->markTestSkipped($message);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $shouldSkip;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-26 01:36:49 +00:00
|
|
|
/**
|
2011-08-30 02:07:23 +00:00
|
|
|
* Setup the test case, backup the static object values so they can be restored.
|
|
|
|
* Specifically backs up the contents of Configure and paths in App if they have
|
|
|
|
* not already been backed up.
|
2010-09-26 01:36:49 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2011-08-30 02:07:23 +00:00
|
|
|
|
|
|
|
if (empty($this->_configure)) {
|
|
|
|
$this->_configure = Configure::read();
|
|
|
|
}
|
|
|
|
if (empty($this->_pathRestore)) {
|
|
|
|
$this->_pathRestore = App::paths();
|
|
|
|
}
|
2010-09-26 01:36:49 +00:00
|
|
|
if (class_exists('Router', false)) {
|
|
|
|
Router::reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* teardown any static object changes and restore them.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
2011-08-30 02:07:23 +00:00
|
|
|
App::build($this->_pathRestore, App::RESET);
|
2010-09-26 01:36:49 +00:00
|
|
|
if (class_exists('ClassRegistry', false)) {
|
|
|
|
ClassRegistry::flush();
|
|
|
|
}
|
2012-04-16 02:20:34 +00:00
|
|
|
if (!empty($this->_configure)) {
|
|
|
|
Configure::clear();
|
|
|
|
Configure::write($this->_configure);
|
|
|
|
}
|
2011-12-15 06:48:41 +00:00
|
|
|
if (isset($_GET['debug']) && $_GET['debug']) {
|
|
|
|
ob_flush();
|
|
|
|
}
|
2018-08-14 10:31:58 +00:00
|
|
|
unset($this->_configure, $this->_pathRestore);
|
2010-09-26 01:36:49 +00:00
|
|
|
}
|
|
|
|
|
2012-03-28 15:49:41 +00:00
|
|
|
/**
|
|
|
|
* See CakeTestSuiteDispatcher::date()
|
|
|
|
*
|
|
|
|
* @param string $format format to be used.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function date($format = 'Y-m-d H:i:s') {
|
|
|
|
return CakeTestSuiteDispatcher::date($format);
|
|
|
|
}
|
|
|
|
|
2012-03-05 02:51:44 +00:00
|
|
|
// @codingStandardsIgnoreStart PHPUnit overrides don't match CakePHP
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Announces the start of a test.
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-07 22:37:02 +00:00
|
|
|
protected function assertPreConditions() {
|
|
|
|
parent::assertPreConditions();
|
2010-06-11 01:58:22 +00:00
|
|
|
$this->startTest($this->getName());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Announces the end of a test.
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-07 22:37:02 +00:00
|
|
|
protected function assertPostConditions() {
|
|
|
|
parent::assertPostConditions();
|
2010-06-11 01:58:22 +00:00
|
|
|
$this->endTest($this->getName());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-03-05 02:51:44 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Chooses which fixtures to load for a given test
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* Each parameter is a model name that corresponds to a fixture, i.e. 'Post', 'Author', etc.
|
|
|
|
*
|
2009-03-21 23:55:39 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see CakeTestCase::$autoFixtures
|
2012-03-05 02:51:44 +00:00
|
|
|
* @throws Exception when no fixture manager is available.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function loadFixtures() {
|
2010-09-28 04:07:56 +00:00
|
|
|
if (empty($this->fixtureManager)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new Exception(__d('cake_dev', 'No fixture manager to load the test fixture'));
|
2010-05-08 20:25:16 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
foreach ($args as $class) {
|
2013-03-12 13:34:20 +00:00
|
|
|
$this->fixtureManager->loadSingle($class, null, $this->dropTables);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-01-25 02:08:56 +00:00
|
|
|
/**
|
|
|
|
* Assert text equality, ignoring differences in newlines.
|
|
|
|
* Helpful for doing cross platform tests of blocks of text.
|
|
|
|
*
|
|
|
|
* @param string $expected The expected value.
|
|
|
|
* @param string $result The actual value.
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $message The message to use for failure.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-01-25 02:08:56 +00:00
|
|
|
*/
|
|
|
|
public function assertTextNotEquals($expected, $result, $message = '') {
|
|
|
|
$expected = str_replace(array("\r\n", "\r"), "\n", $expected);
|
|
|
|
$result = str_replace(array("\r\n", "\r"), "\n", $result);
|
|
|
|
return $this->assertNotEquals($expected, $result, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert text equality, ignoring differences in newlines.
|
|
|
|
* Helpful for doing cross platform tests of blocks of text.
|
|
|
|
*
|
|
|
|
* @param string $expected The expected value.
|
|
|
|
* @param string $result The actual value.
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $message message The message to use for failure.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-01-25 02:08:56 +00:00
|
|
|
*/
|
|
|
|
public function assertTextEquals($expected, $result, $message = '') {
|
|
|
|
$expected = str_replace(array("\r\n", "\r"), "\n", $expected);
|
|
|
|
$result = str_replace(array("\r\n", "\r"), "\n", $result);
|
|
|
|
return $this->assertEquals($expected, $result, $message);
|
|
|
|
}
|
|
|
|
|
2012-02-17 22:36:57 +00:00
|
|
|
/**
|
|
|
|
* Asserts that a string starts with a given prefix, ignoring differences in newlines.
|
|
|
|
* Helpful for doing cross platform tests of blocks of text.
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $prefix The prefix to check for.
|
|
|
|
* @param string $string The string to search in.
|
|
|
|
* @param string $message The message to use for failure.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-03-05 02:51:44 +00:00
|
|
|
*/
|
2012-02-17 22:36:57 +00:00
|
|
|
public function assertTextStartsWith($prefix, $string, $message = '') {
|
|
|
|
$prefix = str_replace(array("\r\n", "\r"), "\n", $prefix);
|
|
|
|
$string = str_replace(array("\r\n", "\r"), "\n", $string);
|
|
|
|
return $this->assertStringStartsWith($prefix, $string, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that a string starts not with a given prefix, ignoring differences in newlines.
|
|
|
|
* Helpful for doing cross platform tests of blocks of text.
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $prefix The prefix to not find.
|
|
|
|
* @param string $string The string to search.
|
|
|
|
* @param string $message The message to use for failure.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-03-05 02:51:44 +00:00
|
|
|
*/
|
2012-02-17 22:36:57 +00:00
|
|
|
public function assertTextStartsNotWith($prefix, $string, $message = '') {
|
|
|
|
$prefix = str_replace(array("\r\n", "\r"), "\n", $prefix);
|
|
|
|
$string = str_replace(array("\r\n", "\r"), "\n", $string);
|
|
|
|
return $this->assertStringStartsNotWith($prefix, $string, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that a string ends with a given prefix, ignoring differences in newlines.
|
|
|
|
* Helpful for doing cross platform tests of blocks of text.
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $suffix The suffix to find.
|
|
|
|
* @param string $string The string to search.
|
|
|
|
* @param string $message The message to use for failure.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-03-05 02:51:44 +00:00
|
|
|
*/
|
2012-02-17 22:36:57 +00:00
|
|
|
public function assertTextEndsWith($suffix, $string, $message = '') {
|
|
|
|
$suffix = str_replace(array("\r\n", "\r"), "\n", $suffix);
|
|
|
|
$string = str_replace(array("\r\n", "\r"), "\n", $string);
|
|
|
|
return $this->assertStringEndsWith($suffix, $string, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that a string ends not with a given prefix, ignoring differences in newlines.
|
|
|
|
* Helpful for doing cross platform tests of blocks of text.
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $suffix The suffix to not find.
|
|
|
|
* @param string $string The string to search.
|
|
|
|
* @param string $message The message to use for failure.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-02-17 22:36:57 +00:00
|
|
|
*/
|
|
|
|
public function assertTextEndsNotWith($suffix, $string, $message = '') {
|
|
|
|
$suffix = str_replace(array("\r\n", "\r"), "\n", $suffix);
|
|
|
|
$string = str_replace(array("\r\n", "\r"), "\n", $string);
|
|
|
|
return $this->assertStringEndsNotWith($suffix, $string, $message);
|
2012-03-05 02:51:44 +00:00
|
|
|
}
|
2012-02-17 22:36:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a string contains another string, ignoring differences in newlines.
|
|
|
|
* Helpful for doing cross platform tests of blocks of text.
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $needle The string to search for.
|
|
|
|
* @param string $haystack The string to search through.
|
|
|
|
* @param string $message The message to display on failure.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $ignoreCase Whether or not the search should be case-sensitive.
|
|
|
|
* @return bool
|
2012-02-17 22:36:57 +00:00
|
|
|
*/
|
|
|
|
public function assertTextContains($needle, $haystack, $message = '', $ignoreCase = false) {
|
|
|
|
$needle = str_replace(array("\r\n", "\r"), "\n", $needle);
|
|
|
|
$haystack = str_replace(array("\r\n", "\r"), "\n", $haystack);
|
|
|
|
return $this->assertContains($needle, $haystack, $message, $ignoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a text doesn't contain another text, ignoring differences in newlines.
|
|
|
|
* Helpful for doing cross platform tests of blocks of text.
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $needle The string to search for.
|
|
|
|
* @param string $haystack The string to search through.
|
|
|
|
* @param string $message The message to display on failure.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $ignoreCase Whether or not the search should be case-sensitive.
|
|
|
|
* @return bool
|
2012-02-17 22:36:57 +00:00
|
|
|
*/
|
|
|
|
public function assertTextNotContains($needle, $haystack, $message = '', $ignoreCase = false) {
|
|
|
|
$needle = str_replace(array("\r\n", "\r"), "\n", $needle);
|
|
|
|
$haystack = str_replace(array("\r\n", "\r"), "\n", $haystack);
|
|
|
|
return $this->assertNotContains($needle, $haystack, $message, $ignoreCase);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-02-13 06:22:34 +00:00
|
|
|
* Takes an array $expected and generates a regex from it to match the provided $string.
|
|
|
|
* Samples for $expected:
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-02-13 06:22:34 +00:00
|
|
|
* Checks for an input tag with a name attribute (contains any non-empty value) and an id
|
|
|
|
* attribute that contains 'my-input':
|
2014-03-21 03:17:46 +00:00
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2014-03-21 03:17:46 +00:00
|
|
|
* array('input' => array('name', 'id' => 'my-input'))
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Checks for two p elements with some text in them:
|
2014-03-21 03:17:46 +00:00
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2014-03-21 03:17:46 +00:00
|
|
|
* array(
|
|
|
|
* array('p' => true),
|
|
|
|
* 'textA',
|
|
|
|
* '/p',
|
|
|
|
* array('p' => true),
|
|
|
|
* 'textB',
|
|
|
|
* '/p'
|
|
|
|
* )
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-02-13 06:22:34 +00:00
|
|
|
* You can also specify a pattern expression as part of the attribute values, or the tag
|
|
|
|
* being defined, if you prepend the value with preg: and enclose it with slashes, like so:
|
2014-03-21 03:17:46 +00:00
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2014-03-21 03:17:46 +00:00
|
|
|
* array(
|
|
|
|
* array('input' => array('name', 'id' => 'preg:/FieldName\d+/')),
|
|
|
|
* 'preg:/My\s+field/'
|
|
|
|
* )
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-02-13 06:22:34 +00:00
|
|
|
* Important: This function is very forgiving about whitespace and also accepts any
|
2011-12-02 05:58:09 +00:00
|
|
|
* permutation of attribute order. It will also allow whitespace between specified tags.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $string An HTML/XHTML/XML string
|
|
|
|
* @param array $expected An array, see above
|
2014-06-06 17:57:48 +00:00
|
|
|
* @param string $fullDebug Whether or not more verbose output should be used.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function assertTags($string, $expected, $fullDebug = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$regex = array();
|
|
|
|
$normalized = array();
|
2012-03-05 02:51:44 +00:00
|
|
|
foreach ((array)$expected as $key => $val) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_numeric($key)) {
|
|
|
|
$normalized[] = array($key => $val);
|
|
|
|
} else {
|
|
|
|
$normalized[] = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$i = 0;
|
|
|
|
foreach ($normalized as $tags) {
|
2009-10-28 18:14:50 +00:00
|
|
|
if (!is_array($tags)) {
|
|
|
|
$tags = (string)$tags;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$i++;
|
2020-03-20 20:10:58 +00:00
|
|
|
if (is_string($tags) && $tags[0] === '<') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$tags = array(substr($tags, 1) => array());
|
|
|
|
} elseif (is_string($tags)) {
|
2009-02-13 06:22:34 +00:00
|
|
|
$tagsTrimmed = preg_replace('/\s+/m', '', $tags);
|
|
|
|
|
|
|
|
if (preg_match('/^\*?\//', $tags, $match) && $tagsTrimmed !== '//') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$prefix = array(null, null);
|
2009-02-13 06:22:34 +00:00
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($match[0] === '*/') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$prefix = array('Anything, ', '.*?');
|
|
|
|
}
|
|
|
|
$regex[] = array(
|
|
|
|
sprintf('%sClose %s tag', $prefix[0], substr($tags, strlen($match[0]))),
|
2012-11-21 14:39:03 +00:00
|
|
|
sprintf('%s<[\s]*\/[\s]*%s[\s]*>[\n\r]*', $prefix[1], substr($tags, strlen($match[0]))),
|
2008-05-30 11:40:08 +00:00
|
|
|
$i,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!empty($tags) && preg_match('/^preg\:\/(.+)\/$/i', $tags, $matches)) {
|
|
|
|
$tags = $matches[1];
|
|
|
|
$type = 'Regex matches';
|
|
|
|
} else {
|
|
|
|
$tags = preg_quote($tags, '/');
|
|
|
|
$type = 'Text equals';
|
|
|
|
}
|
|
|
|
$regex[] = array(
|
|
|
|
sprintf('%s "%s"', $type, $tags),
|
|
|
|
$tags,
|
|
|
|
$i,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($tags as $tag => $attributes) {
|
|
|
|
$regex[] = array(
|
|
|
|
sprintf('Open %s tag', $tag),
|
|
|
|
sprintf('[\s]*<%s', preg_quote($tag, '/')),
|
|
|
|
$i,
|
|
|
|
);
|
|
|
|
if ($attributes === true) {
|
|
|
|
$attributes = array();
|
|
|
|
}
|
|
|
|
$attrs = array();
|
|
|
|
$explanations = array();
|
2010-04-05 01:43:29 +00:00
|
|
|
$i = 1;
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($attributes as $attr => $val) {
|
|
|
|
if (is_numeric($attr) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
|
|
|
|
$attrs[] = $matches[1];
|
|
|
|
$explanations[] = sprintf('Regex "%s" matches', $matches[1]);
|
|
|
|
continue;
|
|
|
|
} else {
|
2010-04-05 01:43:29 +00:00
|
|
|
$quotes = '["\']';
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_numeric($attr)) {
|
|
|
|
$attr = $val;
|
|
|
|
$val = '.+?';
|
|
|
|
$explanations[] = sprintf('Attribute "%s" present', $attr);
|
2009-02-13 06:22:34 +00:00
|
|
|
} elseif (!empty($val) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
|
2014-03-22 01:24:10 +00:00
|
|
|
$val = str_replace(
|
|
|
|
array('.*', '.+'),
|
|
|
|
array('.*?', '.+?'),
|
|
|
|
$matches[1]
|
|
|
|
);
|
2014-03-22 02:53:25 +00:00
|
|
|
$quotes = $val !== $matches[1] ? '["\']' : '["\']?';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$explanations[] = sprintf('Attribute "%s" matches "%s"', $attr, $val);
|
|
|
|
} else {
|
|
|
|
$explanations[] = sprintf('Attribute "%s" == "%s"', $attr, $val);
|
|
|
|
$val = preg_quote($val, '/');
|
|
|
|
}
|
2010-04-05 01:43:29 +00:00
|
|
|
$attrs[] = '[\s]+' . preg_quote($attr, '/') . '=' . $quotes . $val . $quotes;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-04-05 01:43:29 +00:00
|
|
|
$i++;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($attrs) {
|
|
|
|
$regex[] = array(
|
2014-03-22 01:24:10 +00:00
|
|
|
'explains' => $explanations,
|
|
|
|
'attrs' => $attrs,
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
$regex[] = array(
|
|
|
|
sprintf('End %s tag', $tag),
|
|
|
|
'[\s]*\/?[\s]*>[\n\r]*',
|
|
|
|
$i,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 01:24:10 +00:00
|
|
|
foreach ($regex as $i => $assertion) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$matches = false;
|
2014-03-22 01:24:10 +00:00
|
|
|
if (isset($assertion['attrs'])) {
|
|
|
|
$string = $this->_assertAttributes($assertion, $string);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($description, $expressions, $itemNum) = $assertion;
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ((array)$expressions as $expression) {
|
|
|
|
if (preg_match(sprintf('/^%s/s', $expression), $string, $match)) {
|
|
|
|
$matches = true;
|
|
|
|
$string = substr($string, strlen($match[0]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$matches) {
|
2010-05-08 05:49:45 +00:00
|
|
|
$this->assertTrue(false, sprintf('Item #%d / regex #%d failed: %s', $itemNum, $i, $description));
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($fullDebug) {
|
|
|
|
debug($string, true);
|
|
|
|
debug($regex, true);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-08 05:49:45 +00:00
|
|
|
$this->assertTrue(true, '%s');
|
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-21 23:55:39 +00:00
|
|
|
/**
|
2014-03-22 01:24:10 +00:00
|
|
|
* Check the attributes as part of an assertTags() check.
|
2009-03-21 23:55:39 +00:00
|
|
|
*
|
2014-03-22 01:24:10 +00:00
|
|
|
* @param array $assertions Assertions to run.
|
|
|
|
* @param string $string The HTML string to check.
|
|
|
|
* @return void
|
2009-03-21 23:55:39 +00:00
|
|
|
*/
|
2014-03-22 01:24:10 +00:00
|
|
|
protected function _assertAttributes($assertions, $string) {
|
|
|
|
$asserts = $assertions['attrs'];
|
|
|
|
$explains = $assertions['explains'];
|
2014-03-23 15:00:59 +00:00
|
|
|
$len = count($asserts);
|
|
|
|
do {
|
2014-03-22 01:24:10 +00:00
|
|
|
$matches = false;
|
|
|
|
foreach ($asserts as $j => $assert) {
|
|
|
|
if (preg_match(sprintf('/^%s/s', $assert), $string, $match)) {
|
|
|
|
$matches = true;
|
|
|
|
$string = substr($string, strlen($match[0]));
|
|
|
|
array_splice($asserts, $j, 1);
|
|
|
|
array_splice($explains, $j, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($matches === false) {
|
|
|
|
$this->assertTrue(false, 'Attribute did not match. Was expecting ' . $explains[$j]);
|
2009-03-21 23:55:39 +00:00
|
|
|
}
|
2014-03-23 15:00:59 +00:00
|
|
|
$len = count($asserts);
|
|
|
|
} while ($len > 0);
|
2014-03-22 01:24:10 +00:00
|
|
|
return $string;
|
2009-03-21 23:55:39 +00:00
|
|
|
}
|
2010-05-04 18:17:08 +00:00
|
|
|
|
2012-03-05 02:51:44 +00:00
|
|
|
// @codingStandardsIgnoreStart
|
|
|
|
|
2010-05-08 21:59:33 +00:00
|
|
|
/**
|
2011-07-19 03:15:44 +00:00
|
|
|
* Compatibility wrapper function for assertEquals
|
|
|
|
*
|
|
|
|
* @param mixed $result
|
|
|
|
* @param mixed $expected
|
|
|
|
* @param string $message the text to display if the assertion is not correct
|
2016-11-11 20:43:43 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0
|
2011-07-19 03:15:44 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-26 12:39:04 +00:00
|
|
|
protected static function assertEqual($result, $expected, $message = '') {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::assertEquals($expected, $result, $message);
|
2010-05-08 15:42:04 +00:00
|
|
|
}
|
2010-05-08 21:59:33 +00:00
|
|
|
|
|
|
|
/**
|
2011-07-19 03:15:44 +00:00
|
|
|
* Compatibility wrapper function for assertNotEquals
|
|
|
|
*
|
|
|
|
* @param mixed $result
|
|
|
|
* @param mixed $expected
|
|
|
|
* @param string $message the text to display if the assertion is not correct
|
2016-11-11 20:43:43 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0
|
2011-07-19 03:15:44 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-26 12:39:04 +00:00
|
|
|
protected static function assertNotEqual($result, $expected, $message = '') {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::assertNotEquals($expected, $result, $message);
|
2010-05-04 18:17:08 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 21:59:33 +00:00
|
|
|
/**
|
2011-07-19 03:15:44 +00:00
|
|
|
* Compatibility wrapper function for assertRegexp
|
|
|
|
*
|
|
|
|
* @param mixed $pattern a regular expression
|
|
|
|
* @param string $string the text to be matched
|
|
|
|
* @param string $message the text to display if the assertion is not correct
|
2016-11-11 20:43:43 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0
|
2011-07-19 03:15:44 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-26 12:39:04 +00:00
|
|
|
protected static function assertPattern($pattern, $string, $message = '') {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::assertRegExp($pattern, $string, $message);
|
2010-05-04 18:17:08 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 21:59:33 +00:00
|
|
|
/**
|
2011-07-19 03:15:44 +00:00
|
|
|
* Compatibility wrapper function for assertEquals
|
|
|
|
*
|
|
|
|
* @param mixed $actual
|
|
|
|
* @param mixed $expected
|
|
|
|
* @param string $message the text to display if the assertion is not correct
|
2016-11-11 20:43:43 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0
|
2011-07-19 03:15:44 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-26 12:39:04 +00:00
|
|
|
protected static function assertIdentical($actual, $expected, $message = '') {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::assertSame($expected, $actual, $message);
|
2010-05-04 18:17:08 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 21:59:33 +00:00
|
|
|
/**
|
2011-07-19 03:15:44 +00:00
|
|
|
* Compatibility wrapper function for assertNotEquals
|
|
|
|
*
|
|
|
|
* @param mixed $actual
|
|
|
|
* @param mixed $expected
|
|
|
|
* @param string $message the text to display if the assertion is not correct
|
2016-11-11 20:43:43 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0
|
2011-07-19 03:15:44 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-26 12:39:04 +00:00
|
|
|
protected static function assertNotIdentical($actual, $expected, $message = '') {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::assertNotSame($expected, $actual, $message);
|
2010-06-05 03:57:05 +00:00
|
|
|
}
|
2011-07-19 03:15:44 +00:00
|
|
|
|
2010-06-05 03:57:05 +00:00
|
|
|
/**
|
2011-07-19 03:15:44 +00:00
|
|
|
* Compatibility wrapper function for assertNotRegExp
|
|
|
|
*
|
|
|
|
* @param mixed $pattern a regular expression
|
|
|
|
* @param string $string the text to be matched
|
|
|
|
* @param string $message the text to display if the assertion is not correct
|
2016-11-11 20:43:43 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0
|
2011-07-19 03:15:44 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-26 12:39:04 +00:00
|
|
|
protected static function assertNoPattern($pattern, $string, $message = '') {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::assertNotRegExp($pattern, $string, $message);
|
2010-05-04 18:17:08 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 22:30:47 +00:00
|
|
|
/**
|
|
|
|
* assert no errors
|
2014-02-17 15:29:03 +00:00
|
|
|
*
|
2016-11-11 20:43:43 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0
|
2014-02-17 15:29:03 +00:00
|
|
|
* @return void
|
2012-11-28 22:30:47 +00:00
|
|
|
*/
|
2010-05-04 18:17:08 +00:00
|
|
|
protected function assertNoErrors() {
|
|
|
|
}
|
2010-05-08 21:59:33 +00:00
|
|
|
|
|
|
|
/**
|
2011-07-19 03:15:44 +00:00
|
|
|
* Compatibility wrapper function for setExpectedException
|
|
|
|
*
|
|
|
|
* @param mixed $expected the name of the Exception or error
|
|
|
|
* @param string $message the text to display if the assertion is not correct
|
2016-11-11 20:43:43 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0
|
2011-07-19 03:15:44 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-08 15:42:04 +00:00
|
|
|
protected function expectError($expected = false, $message = '') {
|
|
|
|
if (!$expected) {
|
|
|
|
$expected = 'Exception';
|
|
|
|
}
|
|
|
|
$this->setExpectedException($expected, $message);
|
|
|
|
}
|
2010-05-04 18:17:08 +00:00
|
|
|
|
2010-05-08 21:59:33 +00:00
|
|
|
/**
|
2011-07-19 03:15:44 +00:00
|
|
|
* Compatibility wrapper function for setExpectedException
|
|
|
|
*
|
2015-09-25 15:22:00 +00:00
|
|
|
* @param mixed $name The name of the expected Exception.
|
2011-07-19 03:15:44 +00:00
|
|
|
* @param string $message the text to display if the assertion is not correct
|
2015-09-25 15:22:00 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0.
|
2011-07-19 03:15:44 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-06 20:49:37 +00:00
|
|
|
public function expectException($name = 'Exception', $message = '') {
|
2010-05-08 21:59:33 +00:00
|
|
|
$this->setExpectedException($name, $message);
|
2010-05-04 18:17:08 +00:00
|
|
|
}
|
2010-05-09 20:39:57 +00:00
|
|
|
|
|
|
|
/**
|
2011-07-19 03:15:44 +00:00
|
|
|
* Compatibility wrapper function for assertSame
|
|
|
|
*
|
|
|
|
* @param mixed $first
|
|
|
|
* @param mixed $second
|
|
|
|
* @param string $message the text to display if the assertion is not correct
|
2016-11-11 20:43:43 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0
|
2011-07-19 03:15:44 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-26 12:39:04 +00:00
|
|
|
protected static function assertReference(&$first, &$second, $message = '') {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::assertSame($first, $second, $message);
|
2010-05-09 20:39:57 +00:00
|
|
|
}
|
2010-05-09 22:46:42 +00:00
|
|
|
|
2010-05-22 17:09:17 +00:00
|
|
|
/**
|
|
|
|
* Compatibility wrapper for assertIsA
|
|
|
|
*
|
2011-10-28 05:01:17 +00:00
|
|
|
* @param string $object
|
|
|
|
* @param string $type
|
|
|
|
* @param string $message
|
2016-11-11 20:43:43 +00:00
|
|
|
* @deprecated 3.0.0 This is a compatibility wrapper for 1.x. It will be removed in 3.0
|
2010-05-22 17:09:17 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-26 12:39:04 +00:00
|
|
|
protected static function assertIsA($object, $type, $message = '') {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::assertInstanceOf($type, $object, $message);
|
2010-05-22 17:09:17 +00:00
|
|
|
}
|
|
|
|
|
2010-05-09 22:46:42 +00:00
|
|
|
/**
|
2011-07-19 03:15:44 +00:00
|
|
|
* Compatibility function to test if value is between an acceptable range
|
|
|
|
*
|
|
|
|
* @param mixed $result
|
|
|
|
* @param mixed $expected
|
|
|
|
* @param mixed $margin the rage of acceptation
|
|
|
|
* @param string $message the text to display if the assertion is not correct
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-26 12:39:04 +00:00
|
|
|
protected static function assertWithinMargin($result, $expected, $margin, $message = '') {
|
2011-07-19 03:15:44 +00:00
|
|
|
$upper = $result + $margin;
|
|
|
|
$lower = $result - $margin;
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::assertTrue((($expected <= $upper) && ($expected >= $lower)), $message);
|
2010-05-09 22:46:42 +00:00
|
|
|
}
|
2010-06-09 21:07:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compatibility function for skipping.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $condition Condition to trigger skipping
|
2010-06-09 21:07:17 +00:00
|
|
|
* @param string $message Message for skip
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2010-06-09 21:07:17 +00:00
|
|
|
*/
|
|
|
|
protected function skipUnless($condition, $message = '') {
|
|
|
|
if (!$condition) {
|
|
|
|
$this->markTestSkipped($message);
|
|
|
|
}
|
|
|
|
return $condition;
|
|
|
|
}
|
2012-11-14 12:16:08 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2012-03-05 02:51:44 +00:00
|
|
|
|
2017-07-15 21:23:14 +00:00
|
|
|
/**
|
|
|
|
* Returns a mock object for the specified class.
|
|
|
|
*
|
2017-07-15 22:27:20 +00:00
|
|
|
* @param string $originalClassName The class name of the object to be mocked.
|
2017-07-15 21:23:14 +00:00
|
|
|
* @param array $methods By default, all methods of the given class are replaced
|
|
|
|
* with a test double that just returns NULL unless a return value is configured
|
|
|
|
* using will($this->returnValue()), for instance.
|
|
|
|
* When the second (optional) parameter is provided, only the methods whose names
|
|
|
|
* are in the array are replaced with a configurable test double. The behavior
|
|
|
|
* of the other methods is not changed. Providing NULL as the parameter means
|
|
|
|
* that no methods will be replaced.
|
|
|
|
* @param array $arguments The third (optional) parameter may hold a parameter
|
|
|
|
* array that is passed to the original class' constructor (which is not replaced
|
|
|
|
* with a dummy implementation by default).
|
|
|
|
* @param string $mockClassName The fourth (optional) parameter can be used to
|
|
|
|
* specify a class name for the generated test double class.
|
2017-07-15 22:27:20 +00:00
|
|
|
* @param bool $callOriginalConstructor The fifth (optional) parameter can be
|
2017-07-15 21:23:14 +00:00
|
|
|
* used to disable the call to the original class' constructor.
|
2017-07-15 22:27:20 +00:00
|
|
|
* @param bool $callOriginalClone The sixth (optional) parameter can be used
|
2017-07-15 21:23:14 +00:00
|
|
|
* to disable the call to the original class' clone constructor.
|
2017-07-15 22:27:20 +00:00
|
|
|
* @param bool $callAutoload The seventh (optional) parameter can be used to
|
2017-07-15 21:23:14 +00:00
|
|
|
* disable __autoload() during the generation of the test double class.
|
|
|
|
* @return object
|
2017-07-15 22:27:20 +00:00
|
|
|
* @deprecated Use `getMockBuilder()` or `createMock()` in new unit tests.
|
2017-07-15 21:23:14 +00:00
|
|
|
* @see https://phpunit.de/manual/current/en/test-doubles.html
|
|
|
|
*/
|
2017-08-03 17:17:05 +00:00
|
|
|
protected function _buildMock(
|
|
|
|
$originalClassName,
|
|
|
|
$methods = array(),
|
|
|
|
array $arguments = array(),
|
|
|
|
$mockClassName = '',
|
|
|
|
$callOriginalConstructor = true,
|
|
|
|
$callOriginalClone = true,
|
|
|
|
$callAutoload = true
|
|
|
|
) {
|
2017-07-15 21:23:14 +00:00
|
|
|
$MockBuilder = $this->getMockBuilder($originalClassName);
|
|
|
|
if (!empty($methods)) {
|
|
|
|
$MockBuilder = $MockBuilder->setMethods($methods);
|
|
|
|
}
|
|
|
|
if (!empty($arguments)) {
|
|
|
|
$MockBuilder = $MockBuilder->setConstructorArgs($arguments);
|
|
|
|
}
|
|
|
|
if ($mockClassName != '') {
|
|
|
|
$MockBuilder = $MockBuilder->setMockClassName($mockClassName);
|
|
|
|
}
|
|
|
|
if ($callOriginalConstructor !== true) {
|
2017-07-15 22:27:20 +00:00
|
|
|
$MockBuilder = $MockBuilder->disableOriginalConstructor();
|
2017-07-15 21:23:14 +00:00
|
|
|
}
|
|
|
|
if ($callOriginalClone !== true) {
|
2017-07-15 22:27:20 +00:00
|
|
|
$MockBuilder = $MockBuilder->disableOriginalClone();
|
2017-07-15 21:23:14 +00:00
|
|
|
}
|
|
|
|
if ($callAutoload !== true) {
|
2017-07-15 22:27:20 +00:00
|
|
|
$MockBuilder = $MockBuilder->disableAutoload();
|
2017-07-15 21:23:14 +00:00
|
|
|
}
|
2017-07-18 20:31:28 +00:00
|
|
|
return $MockBuilder->getMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a mock object for the specified class.
|
|
|
|
*
|
|
|
|
* @param string $originalClassName The class name of the object to be mocked.
|
|
|
|
* @param array $methods By default, all methods of the given class are replaced
|
|
|
|
* with a test double that just returns NULL unless a return value is configured
|
|
|
|
* using will($this->returnValue()), for instance.
|
|
|
|
* When the second (optional) parameter is provided, only the methods whose names
|
|
|
|
* are in the array are replaced with a configurable test double. The behavior
|
|
|
|
* of the other methods is not changed. Providing NULL as the parameter means
|
|
|
|
* that no methods will be replaced.
|
|
|
|
* @param array $arguments The third (optional) parameter may hold a parameter
|
|
|
|
* array that is passed to the original class' constructor (which is not replaced
|
|
|
|
* with a dummy implementation by default).
|
|
|
|
* @param string $mockClassName The fourth (optional) parameter can be used to
|
|
|
|
* specify a class name for the generated test double class.
|
|
|
|
* @param bool $callOriginalConstructor The fifth (optional) parameter can be
|
|
|
|
* used to disable the call to the original class' constructor.
|
|
|
|
* @param bool $callOriginalClone The sixth (optional) parameter can be used
|
|
|
|
* to disable the call to the original class' clone constructor.
|
|
|
|
* @param bool $callAutoload The seventh (optional) parameter can be used to
|
|
|
|
* disable __autoload() during the generation of the test double class.
|
|
|
|
* @param bool $cloneArguments Not supported.
|
|
|
|
* @param bool $callOriginalMethods Not supported.
|
|
|
|
* @param string $proxyTarget Not supported.
|
|
|
|
* @return object
|
|
|
|
* @throws InvalidArgumentException When not supported parameters are set.
|
|
|
|
* @deprecated Use `getMockBuilder()` or `createMock()` in new unit tests.
|
|
|
|
* @see https://phpunit.de/manual/current/en/test-doubles.html
|
|
|
|
*/
|
2017-08-03 17:17:05 +00:00
|
|
|
public function getMock(
|
|
|
|
$originalClassName,
|
|
|
|
$methods = array(),
|
|
|
|
array $arguments = array(),
|
|
|
|
$mockClassName = '',
|
|
|
|
$callOriginalConstructor = true,
|
|
|
|
$callOriginalClone = true,
|
|
|
|
$callAutoload = true,
|
|
|
|
$cloneArguments = false,
|
|
|
|
$callOriginalMethods = false,
|
|
|
|
$proxyTarget = null
|
|
|
|
) {
|
2017-07-18 20:31:28 +00:00
|
|
|
$phpUnitVersion = PHPUnit_Runner_Version::id();
|
|
|
|
if (version_compare($phpUnitVersion, '5.7.0', '<')) {
|
|
|
|
return parent::getMock($originalClassName, $methods, $arguments,
|
|
|
|
$mockClassName, $callOriginalConstructor, $callOriginalClone,
|
|
|
|
$callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget);
|
|
|
|
}
|
2017-07-15 21:23:14 +00:00
|
|
|
if ($cloneArguments) {
|
2017-07-15 22:27:20 +00:00
|
|
|
throw new InvalidArgumentException('$cloneArguments parameter is not supported');
|
2017-07-15 21:23:14 +00:00
|
|
|
}
|
|
|
|
if ($callOriginalMethods) {
|
2017-07-15 22:27:20 +00:00
|
|
|
throw new InvalidArgumentException('$callOriginalMethods parameter is not supported');
|
2017-07-15 21:23:14 +00:00
|
|
|
}
|
|
|
|
if ($proxyTarget !== null) {
|
2017-07-15 22:27:20 +00:00
|
|
|
throw new InvalidArgumentException('$proxyTarget parameter is not supported');
|
2017-07-15 21:23:14 +00:00
|
|
|
}
|
2017-08-03 17:17:05 +00:00
|
|
|
return $this->_buildMock(
|
|
|
|
$originalClassName,
|
|
|
|
$methods,
|
|
|
|
$arguments,
|
|
|
|
$mockClassName,
|
|
|
|
$callOriginalConstructor,
|
|
|
|
$callOriginalClone,
|
|
|
|
$callAutoload
|
|
|
|
);
|
2017-07-15 21:23:14 +00:00
|
|
|
}
|
|
|
|
|
2012-11-13 01:10:53 +00:00
|
|
|
/**
|
|
|
|
* Mock a model, maintain fixtures and table association
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $model The model to get a mock for.
|
|
|
|
* @param mixed $methods The list of methods to mock
|
|
|
|
* @param array $config The config data for the mock's constructor.
|
2013-08-03 14:46:20 +00:00
|
|
|
* @throws MissingModelException
|
2012-11-13 01:10:53 +00:00
|
|
|
* @return Model
|
|
|
|
*/
|
2013-08-03 08:44:35 +00:00
|
|
|
public function getMockForModel($model, $methods = array(), $config = array()) {
|
2016-09-13 02:15:22 +00:00
|
|
|
$defaults = ClassRegistry::config('Model');
|
|
|
|
unset($defaults['ds']);
|
2012-11-13 01:10:53 +00:00
|
|
|
|
2012-12-22 23:30:27 +00:00
|
|
|
list($plugin, $name) = pluginSplit($model, true);
|
|
|
|
App::uses($name, $plugin . 'Model');
|
2014-10-13 02:14:47 +00:00
|
|
|
|
2016-09-13 02:15:22 +00:00
|
|
|
$config = array_merge($defaults, (array)$config, array('name' => $name));
|
2013-08-03 14:46:20 +00:00
|
|
|
|
|
|
|
if (!class_exists($name)) {
|
|
|
|
throw new MissingModelException(array($model));
|
|
|
|
}
|
2012-11-13 01:10:53 +00:00
|
|
|
$mock = $this->getMock($name, $methods, array($config));
|
2014-10-13 02:14:47 +00:00
|
|
|
|
|
|
|
$availableDs = array_keys(ConnectionManager::enumConnectionObjects());
|
2015-02-27 14:25:23 +00:00
|
|
|
|
2014-10-13 02:14:47 +00:00
|
|
|
if ($mock->useDbConfig !== 'test' && in_array('test_' . $mock->useDbConfig, $availableDs)) {
|
2018-03-10 01:51:30 +00:00
|
|
|
$mock->useDbConfig = 'test_' . $mock->useDbConfig;
|
|
|
|
$mock->setDataSource($mock->useDbConfig);
|
2015-02-27 14:25:23 +00:00
|
|
|
} else {
|
|
|
|
$mock->useDbConfig = 'test';
|
|
|
|
$mock->setDataSource('test');
|
2014-10-13 02:14:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-13 01:10:53 +00:00
|
|
|
ClassRegistry::removeObject($name);
|
|
|
|
ClassRegistry::addObject($name, $mock);
|
|
|
|
return $mock;
|
|
|
|
}
|
2012-12-22 23:30:27 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|