Skip to content

Commit

Permalink
Throw exception if argument passed to Table::connection() is of incor…
Browse files Browse the repository at this point in the history
…rect type

Refs #6199
  • Loading branch information
ADmad committed Mar 29, 2015
1 parent f6da132 commit 51d9247
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ORM/Table.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 51d9247

Please sign in to comment.