ソースを参照

Throw exception if argument passed to Table::connection() is of incorrect type

Refs #6199
ADmad 11 年 前
コミット
51d9247af1
2 ファイル変更19 行追加0 行削除
  1. 6 0
      src/ORM/Table.php
  2. 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;
     }
 

+ 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