From 9d1fbb95b3685677dec101bf3e5a095b8a61d7a2 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 12 Sep 2016 22:15:22 -0400 Subject: [PATCH 1/2] Allow datasource access in constructors of mocked models. When mock objects are created from models that access their datasource in the constructor, an exception would be raised for the missing default datasource. By changing how configuration data is handled in the mock creation we can avoid this issue and not reopen #4867 Refs #8225 --- .../Test/Case/TestSuite/CakeTestCaseTest.php | 26 +++++++++++++++++++ lib/Cake/TestSuite/CakeTestCase.php | 6 ++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php b/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php index da97150c3..d7b62e190 100644 --- a/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php +++ b/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php @@ -39,6 +39,22 @@ class SecondaryPost extends Model { } +/** + * ConstructorPost test stub. + */ +class ConstructorPost extends Model { + +/** + * @var string + */ + public $useTable = 'posts'; + + public function __construct($id = false, $table = null, $ds = null) { + parent::__construct($id, $table, $ds); + $this->getDataSource()->cacheMethods = false; + } +} + /** * CakeTestCaseTest * @@ -435,6 +451,16 @@ class CakeTestCaseTest extends CakeTestCase { ConnectionManager::drop('test_secondary'); } +/** + * Test getMockForModel when the model accesses the datasource in the constructor. + * + * @return void + */ + public function testGetMockForModelConstructorDatasource() { + $post = $this->getMockForModel('ConstructorPost', array('save'), array('ds' => 'test')); + $this->assertEquals('test', $post->useDbConfig); + } + /** * test getMockForModel() with plugin models * diff --git a/lib/Cake/TestSuite/CakeTestCase.php b/lib/Cake/TestSuite/CakeTestCase.php index c375717fe..ab04db6a7 100644 --- a/lib/Cake/TestSuite/CakeTestCase.php +++ b/lib/Cake/TestSuite/CakeTestCase.php @@ -718,13 +718,13 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { * @return Model */ public function getMockForModel($model, $methods = array(), $config = array()) { - $config += ClassRegistry::config('Model'); + $defaults = ClassRegistry::config('Model'); + unset($defaults['ds']); list($plugin, $name) = pluginSplit($model, true); App::uses($name, $plugin . 'Model'); - $config = array_merge((array)$config, array('name' => $name)); - unset($config['ds']); + $config = array_merge($defaults, (array)$config, array('name' => $name)); if (!class_exists($name)) { throw new MissingModelException(array($model)); From dc2fa498ef5ad3149f6db8ebafffbb9c2d887614 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 12 Sep 2016 23:19:08 -0400 Subject: [PATCH 2/2] PHPCS. --- lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php b/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php index d7b62e190..0ea4d6253 100644 --- a/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php +++ b/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php @@ -53,6 +53,7 @@ class ConstructorPost extends Model { parent::__construct($id, $table, $ds); $this->getDataSource()->cacheMethods = false; } + } /**