mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Merge pull request #1475 from cakephp/feature/2.4-mock-app-model
Make mocking random models easier
This commit is contained in:
commit
a54c92fc0f
2 changed files with 54 additions and 5 deletions
|
@ -353,6 +353,11 @@ class CakeTestCaseTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testGetMockForModel() {
|
||||
App::build(array(
|
||||
'Model' => array(
|
||||
CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS
|
||||
)
|
||||
), App::RESET);
|
||||
$Post = $this->getMockForModel('Post');
|
||||
|
||||
$this->assertInstanceOf('Post', $Post);
|
||||
|
@ -372,6 +377,13 @@ class CakeTestCaseTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testGetMockForModelWithPlugin() {
|
||||
App::build(array(
|
||||
'Plugin' => array(
|
||||
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
|
||||
)
|
||||
), App::RESET);
|
||||
CakePlugin::load('TestPlugin');
|
||||
$this->getMockForModel('TestPlugin.TestPluginAppModel');
|
||||
$TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComment');
|
||||
|
||||
$result = ClassRegistry::init('TestPlugin.TestPluginComment');
|
||||
|
@ -389,4 +401,37 @@ class CakeTestCaseTest extends CakeTestCase {
|
|||
$this->assertTrue($TestPluginComment->save(array()));
|
||||
$this->assertFalse($TestPluginComment->save(array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetMockForModelModel
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetMockForModelModel() {
|
||||
$Mock = $this->getMockForModel('Model', array('save'), array('name' => 'Comment'));
|
||||
|
||||
$result = ClassRegistry::init('Comment');
|
||||
$this->assertInstanceOf('Model', $result);
|
||||
|
||||
$Mock->expects($this->at(0))
|
||||
->method('save')
|
||||
->will($this->returnValue(true));
|
||||
$Mock->expects($this->at(1))
|
||||
->method('save')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$this->assertTrue($Mock->save(array()));
|
||||
$this->assertFalse($Mock->save(array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetMockForModelDoesNotExist
|
||||
*
|
||||
* @expectedException MissingModelException
|
||||
* @expectedExceptionMessage Model IDoNotExist could not be found
|
||||
* @return void
|
||||
*/
|
||||
public function testGetMockForModelDoesNotExist() {
|
||||
$this->getMockForModel('IDoNotExist');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -689,17 +689,21 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase {
|
|||
*
|
||||
* @param string $model
|
||||
* @param mixed $methods
|
||||
* @param mixed $config
|
||||
* @param array $config
|
||||
* @throws MissingModelException
|
||||
* @return Model
|
||||
*/
|
||||
public function getMockForModel($model, $methods = array(), $config = null) {
|
||||
if (is_null($config)) {
|
||||
$config = ClassRegistry::config('Model');
|
||||
}
|
||||
public function getMockForModel($model, $methods = array(), $config = array()) {
|
||||
$config += ClassRegistry::config('Model');
|
||||
|
||||
list($plugin, $name) = pluginSplit($model, true);
|
||||
App::uses($name, $plugin . 'Model');
|
||||
$config = array_merge((array)$config, array('name' => $name));
|
||||
|
||||
if (!class_exists($name)) {
|
||||
throw new MissingModelException(array($model));
|
||||
}
|
||||
|
||||
$mock = $this->getMock($name, $methods, array($config));
|
||||
ClassRegistry::removeObject($name);
|
||||
ClassRegistry::addObject($name, $mock);
|
||||
|
|
Loading…
Add table
Reference in a new issue