Skip to content

Commit

Permalink
Merge pull request #1475 from cakephp/feature/2.4-mock-app-model
Browse files Browse the repository at this point in the history
Make mocking random models easier
  • Loading branch information
lorenzo committed Aug 5, 2013
2 parents 38b050a + ff856b7 commit a54c92f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
45 changes: 45 additions & 0 deletions lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php
Expand Up @@ -353,6 +353,11 @@ public function testAssertTextNotContains() {
* @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);
Expand All @@ -372,6 +377,13 @@ public function testGetMockForModel() {
* @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');
Expand All @@ -389,4 +401,37 @@ public function testGetMockForModelWithPlugin() {
$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');
}
}
14 changes: 9 additions & 5 deletions lib/Cake/TestSuite/CakeTestCase.php
Expand Up @@ -689,17 +689,21 @@ protected function skipUnless($condition, $message = '') {
*
* @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);
Expand Down

0 comments on commit a54c92f

Please sign in to comment.