Browse Source

Merge pull request #3078 from AD7six/3.0-error-empty-driver

throw an exception with invalid connection config
Mark Story 12 years ago
parent
commit
19afa31c9e

+ 3 - 1
src/Database/Connection.php

@@ -90,9 +90,11 @@ class Connection {
 	public function __construct($config) {
 		$this->_config = $config;
 
+		$driver = '';
 		if (!empty($config['driver'])) {
-			$this->driver($config['driver'], $config);
+			$driver = $config['driver'];
 		}
+		$this->driver($driver, $config);
 
 		if (!empty($config['log'])) {
 			$this->logQueries($config['log']);

+ 1 - 1
src/Database/Exception/MissingDriverException.php

@@ -18,6 +18,6 @@ namespace Cake\Database\Exception;
 
 class MissingDriverException extends \Cake\Error\Exception {
 
-	protected $_messageTemplate = 'Database driver %s could not be found.';
+	protected $_messageTemplate = 'Database driver "%s" could not be found.';
 
 }

+ 30 - 6
tests/TestCase/Database/ConnectionTest.php

@@ -63,10 +63,32 @@ class ConnectionTest extends TestCase {
 	}
 
 /**
+ * Tests creating a connection using no driver throws an exception
+ *
+ * @expectedException \Cake\Database\Exception\MissingDriverException
+ * @expectedExceptionMessage Database driver "" could not be found.
+ * @return void
+ */
+	public function testNoDriver() {
+		$connection = new Connection([]);
+	}
+
+/**
  * Tests creating a connection using an invalid driver throws an exception
  *
  * @expectedException \Cake\Database\Exception\MissingDriverException
- * @expectedExceptionMessage Database driver \Foo\InvalidDriver could not be found.
+ * @expectedExceptionMessage Database driver "" could not be found.
+ * @return void
+ */
+	public function testEmptyDriver() {
+		$connection = new Connection(['driver' => false]);
+	}
+
+/**
+ * Tests creating a connection using an invalid driver throws an exception
+ *
+ * @expectedException \Cake\Database\Exception\MissingDriverException
+ * @expectedExceptionMessage Database driver "\Foo\InvalidDriver" could not be found.
  * @return void
  */
 	public function testMissingDriver() {
@@ -696,11 +718,13 @@ class ConnectionTest extends TestCase {
  * @return void
  */
 	public function testLogBeginRollbackTransaction() {
-		$connection = $this->getMock(
-			'\Cake\Database\Connection',
-			['connect'],
-			[['log' => true]]
-		);
+		$connection = $this
+			->getMockBuilder('\Cake\Database\Connection')
+			->setMethods(['connect'])
+			->disableOriginalConstructor()
+			->getMock();
+		$connection->logQueries(true);
+
 		$driver = $this->getMockFormDriver();
 		$connection->driver($driver);
 

+ 6 - 5
tests/TestCase/Database/Driver/PostgresTest.php

@@ -142,11 +142,12 @@ class PostgresTest extends \Cake\TestSuite\TestCase {
 			['_connect', 'connection'],
 			[['dsn' => 'foo']]
 		);
-		$connection = $this->getMock(
-			'\Cake\Database\Connection',
-			['connect'],
-			[['log' => false]]
-		);
+		$connection = $this
+			->getMockBuilder('\Cake\Database\Connection')
+			->setMethods(['connect'])
+			->disableOriginalConstructor()
+			->getMock();
+
 		$query = new \Cake\Database\Query($connection);
 		$query->insert(['id', 'title'])
 			->into('articles')