Browse Source

Merge pull request #6208 from cakephp/table-connection

Throw exception if argument passed to Table::connection() is of incorrec...
Mark Story 11 years ago
parent
commit
0e156460ce
3 changed files with 20 additions and 3 deletions
  1. 6 0
      src/ORM/Table.php
  2. 1 3
      tests/TestCase/ORM/TableRegistryTest.php
  3. 13 0
      tests/TestCase/ORM/TableTest.php

+ 6 - 0
src/ORM/Table.php

@@ -17,6 +17,7 @@ namespace Cake\ORM;
 use ArrayObject;
 use BadMethodCallException;
 use Cake\Core\App;
+use Cake\Database\Connection;
 use Cake\Database\Schema\Table as Schema;
 use Cake\Database\Type;
 use Cake\Datasource\EntityInterface;
@@ -397,6 +398,11 @@ class Table implements RepositoryInterface, EventListenerInterface
         if ($conn === null) {
             return $this->_connection;
         }
+
+        if (!($conn instanceof Connection)) {
+            throw new RuntimeException('$conn must be an instance of \Cake\Database\Connection');
+        }
+
         return $this->_connection = $conn;
     }
 

+ 1 - 3
tests/TestCase/ORM/TableRegistryTest.php

@@ -275,7 +275,7 @@ class TableRegistryTest extends TestCase
     public function testGetPlugin()
     {
         Plugin::load('TestPlugin');
-        $table = TableRegistry::get('TestPlugin.TestPluginComments', ['connection' => 'test']);
+        $table = TableRegistry::get('TestPlugin.TestPluginComments');
 
         $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $table);
         $this->assertFalse(
@@ -330,7 +330,6 @@ class TableRegistryTest extends TestCase
         Plugin::load('TestPlugin');
         $table = TableRegistry::get('Comments', [
             'className' => 'TestPlugin.TestPluginComments',
-            'connection' => 'test'
         ]);
         $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
         $this->assertInstanceOf($class, $table);
@@ -353,7 +352,6 @@ class TableRegistryTest extends TestCase
         $class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
         $table = TableRegistry::get('Comments', [
             'className' => $class,
-            'connection' => 'test'
         ]);
         $this->assertInstanceOf($class, $table);
         $this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');

+ 13 - 0
tests/TestCase/ORM/TableTest.php

@@ -186,6 +186,19 @@ class TableTest extends TestCase
     }
 
     /**
+     * Tests exception is thrown in connection is not instance of
+     * \Cake\Database\Connection
+     *
+     * @expectedException \RuntimeException
+     * @expectedExceptionMessage $conn must be an instance of \Cake\Database\Connection
+     * @return void
+     */
+    public function testConnectionException()
+    {
+        $table = new Table(['table' => 'users', 'connection' => 'default']);
+    }
+
+    /**
      * Tests primaryKey method
      *
      * @return void