mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Add CakeTestCase::getMockForModel convenience method
This method provides a convenience to create mocked models that are still associated with fixtures and the datasource.
This commit is contained in:
parent
e0aab77dab
commit
4d87abfcdc
3 changed files with 71 additions and 6 deletions
|
@ -29,6 +29,13 @@ App::uses('CakeHtmlReporter', 'TestSuite/Reporter');
|
||||||
*/
|
*/
|
||||||
class CakeTestCaseTest extends CakeTestCase {
|
class CakeTestCaseTest extends CakeTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fixtures property
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $fixtures = array('core.post', 'core.author', 'core.test_plugin_comment');
|
||||||
|
|
||||||
public static function setUpBeforeClass() {
|
public static function setUpBeforeClass() {
|
||||||
require_once CAKE . 'Test' . DS . 'Fixture' . DS . 'AssertTagsTestCase.php';
|
require_once CAKE . 'Test' . DS . 'Fixture' . DS . 'AssertTagsTestCase.php';
|
||||||
require_once CAKE . 'Test' . DS . 'Fixture' . DS . 'FixturizedTestCase.php';
|
require_once CAKE . 'Test' . DS . 'Fixture' . DS . 'FixturizedTestCase.php';
|
||||||
|
@ -339,4 +346,48 @@ class CakeTestCaseTest extends CakeTestCase {
|
||||||
$this->assertTextNotContains("different\rlines", $stringDirty);
|
$this->assertTextNotContains("different\rlines", $stringDirty);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test getMockForModel()
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testGetMockForModel() {
|
||||||
|
|
||||||
|
$Post = $this->getMockForModel('Post');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('Post', $Post);
|
||||||
|
$this->assertNull($Post->save(array()));
|
||||||
|
$this->assertNull($Post->find('all'));
|
||||||
|
$this->assertEquals('posts', $Post->useTable);
|
||||||
|
|
||||||
|
$Post = $this->getMockForModel('Post', array('save'));
|
||||||
|
|
||||||
|
$this->assertNull($Post->save(array()));
|
||||||
|
$this->assertInternalType('array', $Post->find('all'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test getMockForModel() with plugin models
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testGetMockForModelWithPlugin() {
|
||||||
|
|
||||||
|
$TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComment');
|
||||||
|
|
||||||
|
$result = ClassRegistry::init('TestPlugin.TestPluginComment');
|
||||||
|
$this->assertInstanceOf('TestPluginComment', $result);
|
||||||
|
|
||||||
|
$TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComment', array('save'));
|
||||||
|
|
||||||
|
$this->assertInstanceOf('TestPluginComment', $TestPluginComment);
|
||||||
|
$TestPluginComment->expects($this->at(0))
|
||||||
|
->method('save')
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
$TestPluginComment->expects($this->at(1))
|
||||||
|
->method('save')
|
||||||
|
->will($this->returnValue(false));
|
||||||
|
$this->assertTrue($TestPluginComment->save(array()));
|
||||||
|
$this->assertFalse($TestPluginComment->save(array()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -676,4 +676,23 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase {
|
||||||
}
|
}
|
||||||
// @codingStandardsIgnoreStop
|
// @codingStandardsIgnoreStop
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mock a model, maintain fixtures and table association
|
||||||
|
*
|
||||||
|
* @param string $model
|
||||||
|
* @param mixed $methods
|
||||||
|
* @return Model
|
||||||
|
*/
|
||||||
|
public function getMockForModel($model, $methods = array(), $config = null) {
|
||||||
|
if (is_null($config)) {
|
||||||
|
$config = ClassRegistry::config('Model');
|
||||||
|
}
|
||||||
|
|
||||||
|
list($plugin, $name) = pluginSplit($model);
|
||||||
|
$config = array_merge((array) $config, array('name' => $name));
|
||||||
|
$mock = $this->getMock($name, $methods, array($config));
|
||||||
|
ClassRegistry::removeObject($name);
|
||||||
|
ClassRegistry::addObject($name, $mock);
|
||||||
|
return $mock;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -342,12 +342,7 @@ abstract class ControllerTestCase extends CakeTestCase {
|
||||||
if ($methods === true) {
|
if ($methods === true) {
|
||||||
$methods = array();
|
$methods = array();
|
||||||
}
|
}
|
||||||
ClassRegistry::init($model);
|
$this->getMockForModel($model, $methods, $config);
|
||||||
list($plugin, $name) = pluginSplit($model);
|
|
||||||
$config = array_merge((array)$config, array('name' => $model));
|
|
||||||
$_model = $this->getMock($name, $methods, array($config));
|
|
||||||
ClassRegistry::removeObject($name);
|
|
||||||
ClassRegistry::addObject($name, $_model);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($mocks['components'] as $component => $methods) {
|
foreach ($mocks['components'] as $component => $methods) {
|
||||||
|
|
Loading…
Reference in a new issue