Browse Source

Make ModelAwareTrait::loadModel() return model instance.

ADmad 11 years ago
parent
commit
137ebcf2aa

+ 3 - 3
src/Model/ModelAwareTrait.php

@@ -71,7 +71,7 @@ trait ModelAwareTrait {
  * @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.
  */
@@ -81,7 +81,7 @@ trait ModelAwareTrait {
 		}
 
 		if (isset($this->{$modelClass})) {
-			return true;
+			return $this->{$modelClass};
 		}
 
 		list($plugin, $modelClass) = pluginSplit($modelClass, true);
@@ -97,7 +97,7 @@ trait ModelAwareTrait {
 		if (!$this->{$modelClass}) {
 			throw new MissingModelException([$modelClass, $type]);
 		}
-		return true;
+		return $this->{$modelClass};
 	}
 
 /**

+ 5 - 2
tests/TestCase/Console/ShellTest.php

@@ -191,8 +191,11 @@ class ShellTest extends TestCase {
 		$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

+ 8 - 2
tests/TestCase/Controller/ControllerTest.php

@@ -272,7 +272,10 @@ class ControllerTest extends TestCase {
 		$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
@@ -293,7 +296,10 @@ class ControllerTest extends TestCase {
 		$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

+ 5 - 3
tests/TestCase/Model/ModelAwareTraitTest.php

@@ -57,10 +57,12 @@ class ModelAwareTraitTest extends TestCase {
 		$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);
 	}
 
@@ -80,7 +82,7 @@ class ModelAwareTraitTest extends TestCase {
 		});
 
 		$result = $stub->loadModel('Magic', 'Test');
-		$this->assertTrue($result);
+		$this->assertInstanceOf('\StdClass', $result);
 		$this->assertInstanceOf('\StdClass', $stub->Magic);
 		$this->assertEquals('Magic', $stub->Magic->name);
 	}