Skip to content

Commit

Permalink
throwing exception when suplying invalid credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Oct 14, 2012
1 parent a3acaf7 commit 4c4a764
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/Cake/Model/Datasource/Database/Connection.php
Expand Up @@ -4,7 +4,8 @@

use PDOException,
Cake\Model\Datasource\Database\Exception\MissingDriverException,
Cake\Model\Datasource\Database\Exception\MissingExtensionException;
Cake\Model\Datasource\Database\Exception\MissingExtensionException,
Cake\Model\Datasource\Database\Exception\MissingConnectionException;

/**
* Represents a conection with a database server
Expand Down Expand Up @@ -57,13 +58,18 @@ public function __construct($config) {
/**
* Connects to the configured databatase
*
* @throws \Cake\Model\Datasource\Database\Exception\MissingConnectionException if credentials are invalid
* @return boolean true on success or false if already connected
**/
public function connect() {
if ($this->_connected) {
return false;
}
return $this->_connected = $this->_driver->connect($this->_config);
try {
return $this->_connected = $this->_driver->connect($this->_config);
} catch(\Exception $e) {
throw new MissingConnectionException(array('reason' => $e->getMessage()));
}
}

/**
Expand Down
@@ -0,0 +1,26 @@
<?php
/**
*
* PHP Version 5.x
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Model.Datasource.Database.Exception
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Model\Datasource\Database\Exception;


class MissingConnectionException extends \Cake\Error\Exception {

protected $_messageTemplate = 'Connection to database could not be established: %s';

}

Expand Up @@ -64,6 +64,17 @@ public function testDisabledDriver() {
$connection = new Connection(array('datasource' => get_class($mock)));
}

/**
* Tests that connecting with invalid credentials or database name throws an exception
*
* @expectedException \Cake\Model\Datasource\Database\Exception\MissingConnectionException
* @return void
**/
public function testWrongCredentials() {
$connection = new Connection(array('database' => 'foobar') + Configure::read('Connections.test'));
$connection->connect();
}

/**
* Tests disconnecting from database
*
Expand All @@ -88,6 +99,11 @@ public function testPrepare() {
$this->assertEquals($sql, $result->queryString);
}

/**
* Tests executing a simple query using bound values
*
* @return void
**/
public function testExecuteWithArguments() {
$sql = 'SELECT 1 + ?';
$statement = $this->connection->execute($sql, array(1));
Expand Down

0 comments on commit 4c4a764

Please sign in to comment.