Skip to content

Commit

Permalink
Updated TestCase::getMockForModel() to return Table instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Nov 20, 2013
1 parent 9618de1 commit fa5952c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 45 deletions.
10 changes: 4 additions & 6 deletions Cake/ORM/Error/MissingTableClassException.php
@@ -1,8 +1,6 @@
<?php
/**
* MissingModelException class
*
* PHP 5
* MissingTableClassException class
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
Expand All @@ -19,11 +17,11 @@
use Cake\Error\Exception;

/**
* Exception raised when an Entity could not be found.
* Exception raised when a Table could not be found.
*
*/
class MissingEntityException extends Exception {
class MissingTableClassException extends Exception {

protected $_messageTemplate = 'Entity class %s could not be found.';
protected $_messageTemplate = 'Table class %s could not be found.';

}
24 changes: 24 additions & 0 deletions Cake/Test/TestApp/Model/Repository/PostsTable.php
@@ -0,0 +1,24 @@
<?php
/**
* Test App Posts Model
*
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
* @link http://cakephp.org CakePHP Project
* @since CakePHP v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace TestApp\Model\Repository;

use Cake\ORM\Table;

class PostsTable extends Table {

protected $_table = 'posts';

}
@@ -0,0 +1,30 @@
<?php
/**
* Test Plugin Comments Model
*
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakefoundation.org/projects/info/cakephp CakePHP Project
* @since CakePHP v 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

/**
* Class TestPluginCommentsTable
*
*/
namespace TestPlugin\Model\Repository;

use Cake\ORM\Table;

class TestPluginCommentsTable extends Table {

protected $_table = 'test_plugin_comments';

}
62 changes: 39 additions & 23 deletions Cake/Test/TestCase/TestSuite/TestCaseTest.php
Expand Up @@ -22,10 +22,10 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Cake\Test\Fixture\AssertTagsTestCase;
use Cake\Test\Fixture\FixturizedTestCase;
use Cake\Utility\ClassRegistry;

/**
* TestCaseTest
Expand Down Expand Up @@ -349,17 +349,25 @@ public function testAssertTextNotContains() {
*/
public function testGetMockForModel() {
Configure::write('App.namespace', 'TestApp');
$Post = $this->getMockForModel('Post');
$Posts = $this->getMockForModel('Posts');
$entity = new \Cake\ORM\Entity(array());

$this->assertInstanceOf('TestApp\Model\Post', $Post);
$this->assertNull($Post->save(array()));
$this->assertNull($Post->implementedEvents());
$this->assertEquals('posts', $Post->useTable);
$this->assertInstanceOf('TestApp\Model\Repository\PostsTable', $Posts);
$this->assertNull($Posts->save($entity));
$this->assertNull($Posts->table());

$Post = $this->getMockForModel('Post', array('save'));
$Posts = $this->getMockForModel('Posts', array('save'));

$this->assertNull($Post->save(array()));
$this->assertInternalType('array', $Post->implementedEvents());
$this->assertNull($Posts->save($entity));

$Posts->expects($this->at(0))
->method('save')
->will($this->returnValue(true));
$Posts->expects($this->at(1))
->method('save')
->will($this->returnValue(false));
$this->assertTrue($Posts->save($entity));
$this->assertFalse($Posts->save($entity));
}

/**
Expand All @@ -370,34 +378,41 @@ public function testGetMockForModel() {
public function testGetMockForModelWithPlugin() {
Configure::write('App.namespace', 'TestApp');
Plugin::load('TestPlugin');
$TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComment');
$TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments');

$result = ClassRegistry::init('TestPlugin.TestPluginComment');
$this->assertInstanceOf('\TestPlugin\Model\TestPluginComment', $result);
$result = TableRegistry::get('TestPlugin.TestPluginComments');
$this->assertInstanceOf('\TestPlugin\Model\Repository\TestPluginCommentsTable', $result);

$TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComment', array('save'));
$TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments', array('save'));

$this->assertInstanceOf('\TestPlugin\Model\TestPluginComment', $TestPluginComment);
$this->assertInstanceOf('\TestPlugin\Model\Repository\TestPluginCommentsTable', $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()));

$entity = new \Cake\ORM\Entity(array());
$this->assertTrue($TestPluginComment->save($entity));
$this->assertFalse($TestPluginComment->save($entity));
}

/**
* testGetMockForModelModel
* testGetMockForModelTable
*
* @return void
*/
public function testGetMockForModelModel() {
$Mock = $this->getMockForModel('Model', array('save'), array('name' => 'Comment'));
public function testGetMockForModelTable() {
$Mock = $this->getMockForModel(
'Table',
array('save'),
array('alias' => 'Comments', 'className' => '\Cake\ORM\Table')
);

$result = ClassRegistry::init('Comment');
$this->assertInstanceOf('Cake\Model\Model', $result);
$result = TableRegistry::get('Comments');
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertEquals('Comments', $Mock->alias());

$Mock->expects($this->at(0))
->method('save')
Expand All @@ -406,8 +421,9 @@ public function testGetMockForModelModel() {
->method('save')
->will($this->returnValue(false));

$this->assertTrue($Mock->save(array()));
$this->assertFalse($Mock->save(array()));
$entity = new \Cake\ORM\Entity(array());
$this->assertTrue($Mock->save($entity));
$this->assertFalse($Mock->save($entity));
}

}
34 changes: 18 additions & 16 deletions Cake/TestSuite/TestCase.php
Expand Up @@ -19,8 +19,9 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Error;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Cake\Utility\ClassRegistry;
use Cake\Utility\Inflector;

/**
* Cake TestCase class
Expand Down Expand Up @@ -584,26 +585,27 @@ protected function skipUnless($condition, $message = '') {
/**
* Mock a model, maintain fixtures and table association
*
* @param string $model
* @param string $alias
* @param mixed $methods
* @param array $config
* @throws Cake\Error\MissingModelException
* @todo Rewrite so it gets a model for a Table object
* @param array $options
* @throws Cake\ORM\Error\MissingTableClassException
* @return Model
*/
public function getMockForModel($model, $methods = array(), $config = array()) {
$config += (array)ClassRegistry::config('Model');

$modelClass = App::className($model, 'Model');
list(, $name) = namespaceSplit($modelClass);
$config = array_merge((array)$config, array('name' => $name));
if (!$modelClass) {
throw new Error\MissingModelException(array($model));
public function getMockForModel($alias, $methods = array(), $options = array()) {
if (empty($options['className'])) {
$class = Inflector::camelize($alias);
$className = App::classname($class, 'Model\Repository', 'Table');
if (!$className) {
throw new \Cake\ORM\Error\MissingTableClassException(array($alias));
}
$options['className'] = $className;
}

$mock = $this->getMock($modelClass, $methods, array($config));
ClassRegistry::removeObject($name);
ClassRegistry::addObject($name, $mock);
list($plugin, $baseClass) = pluginSplit($alias);
$options = $options + ['alias' => $baseClass] + TableRegistry::config($alias);

$mock = $this->getMock($options['className'], $methods, array($options));
TableRegistry::set($alias, $mock);
return $mock;
}

Expand Down

0 comments on commit fa5952c

Please sign in to comment.