Skip to content

Commit

Permalink
Make ModelAwareTrait::loadModel() return model instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Sep 14, 2014
1 parent fa16a31 commit 137ebcf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/Model/ModelAwareTrait.php
Expand Up @@ -71,7 +71,7 @@ protected function _setModelClass($name) {
* @param string $modelClass Name of model class to load. Defaults to $this->modelClass
* @param string $type The type of repository to load. Defaults to 'Table' which
* delegates to Cake\ORM\TableRegistry.
* @return bool True when single repository found and instance created.
* @return object The model instance created.
* @throws \Cake\Model\Exception\MissingModelException If the model class cannot be found.
* @throws \InvalidArgumentException When using a type that has not been registered.
*/
Expand All @@ -81,7 +81,7 @@ public function loadModel($modelClass = null, $type = 'Table') {
}

if (isset($this->{$modelClass})) {
return true;
return $this->{$modelClass};
}

list($plugin, $modelClass) = pluginSplit($modelClass, true);
Expand All @@ -97,7 +97,7 @@ public function loadModel($modelClass = null, $type = 'Table') {
if (!$this->{$modelClass}) {
throw new MissingModelException([$modelClass, $type]);
}
return true;
return $this->{$modelClass};
}

/**
Expand Down
7 changes: 5 additions & 2 deletions tests/TestCase/Console/ShellTest.php
Expand Up @@ -191,8 +191,11 @@ public function testLoadModel() {
$this->assertEquals('Articles', $Shell->modelClass);

Plugin::load('TestPlugin');
$this->Shell->loadModel('TestPlugin.TestPluginComments');
$this->assertTrue(isset($this->Shell->TestPluginComments));
$result = $this->Shell->loadModel('TestPlugin.TestPluginComments');
$this->assertInstanceOf(
'TestPlugin\Model\Table\TestPluginCommentsTable',
$result
);
$this->assertInstanceOf(
'TestPlugin\Model\Table\TestPluginCommentsTable',
$this->Shell->TestPluginComments
Expand Down
10 changes: 8 additions & 2 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -272,7 +272,10 @@ public function testLoadModel() {
$this->assertFalse(isset($Controller->Articles));

$result = $Controller->loadModel('Articles');
$this->assertTrue($result);
$this->assertInstanceOf(
'TestApp\Model\Table\ArticlesTable',
$result
);
$this->assertInstanceOf(
'TestApp\Model\Table\ArticlesTable',
$Controller->Articles
Expand All @@ -293,7 +296,10 @@ public function testLoadModelInPlugins() {
$this->assertFalse(isset($Controller->TestPluginComments));

$result = $Controller->loadModel('TestPlugin.TestPluginComments');
$this->assertTrue($result);
$this->assertInstanceOf(
'TestPlugin\Model\Table\TestPluginCommentsTable',
$result
);
$this->assertInstanceOf(
'TestPlugin\Model\Table\TestPluginCommentsTable',
$Controller->TestPluginComments
Expand Down
8 changes: 5 additions & 3 deletions tests/TestCase/Model/ModelAwareTraitTest.php
Expand Up @@ -57,10 +57,12 @@ public function testLoadModel() {
$stub->setProps('Articles');
$stub->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);

$this->assertTrue($stub->loadModel());
$result = $stub->loadModel();
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertInstanceOf('Cake\ORM\Table', $stub->Articles);

$this->assertTrue($stub->loadModel('Comments'));
$result = $stub->loadModel('Comments');
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertInstanceOf('Cake\ORM\Table', $stub->Comments);
}

Expand All @@ -80,7 +82,7 @@ public function testModelFactory() {
});

$result = $stub->loadModel('Magic', 'Test');
$this->assertTrue($result);
$this->assertInstanceOf('\StdClass', $result);
$this->assertInstanceOf('\StdClass', $stub->Magic);
$this->assertEquals('Magic', $stub->Magic->name);
}
Expand Down

0 comments on commit 137ebcf

Please sign in to comment.