From fa5952c279c1a21825bbbf4f11495436eb70bec6 Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 20 Nov 2013 00:00:53 +0530 Subject: [PATCH] Updated TestCase::getMockForModel() to return Table instance --- Cake/ORM/Error/MissingTableClassException.php | 10 ++- .../TestApp/Model/Repository/PostsTable.php | 24 +++++++ .../Repository/TestPluginCommentsTable.php | 30 +++++++++ Cake/Test/TestCase/TestSuite/TestCaseTest.php | 62 ++++++++++++------- Cake/TestSuite/TestCase.php | 34 +++++----- 5 files changed, 115 insertions(+), 45 deletions(-) create mode 100644 Cake/Test/TestApp/Model/Repository/PostsTable.php create mode 100644 Cake/Test/TestApp/Plugin/TestPlugin/Model/Repository/TestPluginCommentsTable.php diff --git a/Cake/ORM/Error/MissingTableClassException.php b/Cake/ORM/Error/MissingTableClassException.php index 2b0f43551c8..ad4e9904e9f 100644 --- a/Cake/ORM/Error/MissingTableClassException.php +++ b/Cake/ORM/Error/MissingTableClassException.php @@ -1,8 +1,6 @@ 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)); } /** @@ -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') @@ -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)); } } diff --git a/Cake/TestSuite/TestCase.php b/Cake/TestSuite/TestCase.php index 7f8970cc5b4..b88cc7c5847 100644 --- a/Cake/TestSuite/TestCase.php +++ b/Cake/TestSuite/TestCase.php @@ -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 @@ -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; }