Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mocking random models easier #1475

Merged
merged 3 commits into from
Aug 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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