Skip to content

Commit 137ebcf

Browse files
committed
Make ModelAwareTrait::loadModel() return model instance.
1 parent fa16a31 commit 137ebcf

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

src/Model/ModelAwareTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function _setModelClass($name) {
7171
* @param string $modelClass Name of model class to load. Defaults to $this->modelClass
7272
* @param string $type The type of repository to load. Defaults to 'Table' which
7373
* delegates to Cake\ORM\TableRegistry.
74-
* @return bool True when single repository found and instance created.
74+
* @return object The model instance created.
7575
* @throws \Cake\Model\Exception\MissingModelException If the model class cannot be found.
7676
* @throws \InvalidArgumentException When using a type that has not been registered.
7777
*/
@@ -81,7 +81,7 @@ public function loadModel($modelClass = null, $type = 'Table') {
8181
}
8282

8383
if (isset($this->{$modelClass})) {
84-
return true;
84+
return $this->{$modelClass};
8585
}
8686

8787
list($plugin, $modelClass) = pluginSplit($modelClass, true);
@@ -97,7 +97,7 @@ public function loadModel($modelClass = null, $type = 'Table') {
9797
if (!$this->{$modelClass}) {
9898
throw new MissingModelException([$modelClass, $type]);
9999
}
100-
return true;
100+
return $this->{$modelClass};
101101
}
102102

103103
/**

tests/TestCase/Console/ShellTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,11 @@ public function testLoadModel() {
191191
$this->assertEquals('Articles', $Shell->modelClass);
192192

193193
Plugin::load('TestPlugin');
194-
$this->Shell->loadModel('TestPlugin.TestPluginComments');
195-
$this->assertTrue(isset($this->Shell->TestPluginComments));
194+
$result = $this->Shell->loadModel('TestPlugin.TestPluginComments');
195+
$this->assertInstanceOf(
196+
'TestPlugin\Model\Table\TestPluginCommentsTable',
197+
$result
198+
);
196199
$this->assertInstanceOf(
197200
'TestPlugin\Model\Table\TestPluginCommentsTable',
198201
$this->Shell->TestPluginComments

tests/TestCase/Controller/ControllerTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,10 @@ public function testLoadModel() {
272272
$this->assertFalse(isset($Controller->Articles));
273273

274274
$result = $Controller->loadModel('Articles');
275-
$this->assertTrue($result);
275+
$this->assertInstanceOf(
276+
'TestApp\Model\Table\ArticlesTable',
277+
$result
278+
);
276279
$this->assertInstanceOf(
277280
'TestApp\Model\Table\ArticlesTable',
278281
$Controller->Articles
@@ -293,7 +296,10 @@ public function testLoadModelInPlugins() {
293296
$this->assertFalse(isset($Controller->TestPluginComments));
294297

295298
$result = $Controller->loadModel('TestPlugin.TestPluginComments');
296-
$this->assertTrue($result);
299+
$this->assertInstanceOf(
300+
'TestPlugin\Model\Table\TestPluginCommentsTable',
301+
$result
302+
);
297303
$this->assertInstanceOf(
298304
'TestPlugin\Model\Table\TestPluginCommentsTable',
299305
$Controller->TestPluginComments

tests/TestCase/Model/ModelAwareTraitTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ public function testLoadModel() {
5757
$stub->setProps('Articles');
5858
$stub->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);
5959

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

63-
$this->assertTrue($stub->loadModel('Comments'));
64+
$result = $stub->loadModel('Comments');
65+
$this->assertInstanceOf('Cake\ORM\Table', $result);
6466
$this->assertInstanceOf('Cake\ORM\Table', $stub->Comments);
6567
}
6668

@@ -80,7 +82,7 @@ public function testModelFactory() {
8082
});
8183

8284
$result = $stub->loadModel('Magic', 'Test');
83-
$this->assertTrue($result);
85+
$this->assertInstanceOf('\StdClass', $result);
8486
$this->assertInstanceOf('\StdClass', $stub->Magic);
8587
$this->assertEquals('Magic', $stub->Magic->name);
8688
}

0 commit comments

Comments
 (0)