mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Merge pull request #9450 from cakephp/issue-8225
Allow datasource access in constructors of mocked models.
This commit is contained in:
commit
925a45b6b1
2 changed files with 30 additions and 3 deletions
|
@ -39,6 +39,23 @@ 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 +452,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
|
||||
*
|
||||
|
|
|
@ -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));
|
||||
|
|
Loading…
Add table
Reference in a new issue