diff --git a/src/ORM/Table.php b/src/ORM/Table.php index 2e463235323..366a23a2e7f 100644 --- a/src/ORM/Table.php +++ b/src/ORM/Table.php @@ -17,6 +17,7 @@ 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 @@ public function connection($conn = null) 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; } diff --git a/tests/TestCase/ORM/TableTest.php b/tests/TestCase/ORM/TableTest.php index a412b65a44e..e9ddb0f8126 100644 --- a/tests/TestCase/ORM/TableTest.php +++ b/tests/TestCase/ORM/TableTest.php @@ -185,6 +185,19 @@ public function testConnection() $this->assertSame($this->connection, $table->connection()); } + /** + * 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 *