Skip to content

Commit

Permalink
Clean up tests a bit.
Browse files Browse the repository at this point in the history
Fix incorrect namespaces and add destructors to clean up connections.
The automatic disconnection helps solve too many connection errors with
postgres and help reduce memory usage.
  • Loading branch information
markstory committed Apr 27, 2013
1 parent d974c40 commit 2012437
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 20 deletions.
14 changes: 14 additions & 0 deletions lib/Cake/Database/Connection.php
Expand Up @@ -113,6 +113,20 @@ public function __construct($config) {
}
}

/**
* Destructor
*
* Disconnects the driver to release the connection.
*
* @return void
*/
public function __destruct() {
if ($this->_connected) {
$this->_driver->disconnect();
}
unset($this->_driver);
}

/**
* Get the configuration data used to create the connection.
*
Expand Down
5 changes: 3 additions & 2 deletions lib/Cake/Database/Schema/Collection.php
Expand Up @@ -79,8 +79,9 @@ public function describe($name) {
$name,
$this->_connection->config()
);
$statement = $this->_connection->execute($sql, $params);
if (count($statement) == 0) {
try {
$statement = $this->_connection->execute($sql, $params);
} catch (\PDOException $e) {
return null;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/Cake/Database/Schema/Dialect/Mysql.php
Expand Up @@ -25,6 +25,11 @@ class Mysql {
*/
protected $driver;

/**
* Constructor
*
* @param Cake\Database\Driver $driver The driver to use.
*/
public function __construct($driver) {
$this->_driver = $driver;
}
Expand Down
8 changes: 6 additions & 2 deletions lib/Cake/Test/TestCase/Database/ConnectionTest.php
Expand Up @@ -14,23 +14,27 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database;
namespace Cake\Test\TestCase\Database;

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\TestSuite\TestCase;

/**
* Tests Connection class
*
**/
class ConnectionTest extends \Cake\TestSuite\TestCase {
class ConnectionTest extends TestCase {

public function setUp() {
parent::setUp();
$this->connection = new Connection(Configure::read('Datasource.test'));
}

public function tearDown() {
parent::tearDown();
$this->connection->execute('DROP TABLE IF EXISTS things');
unset($this->connection);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/Cake/Test/TestCase/Database/Driver/MysqlTest.php
Expand Up @@ -15,18 +15,19 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Driver;
namespace Cake\Test\TestCase\Database\Driver;

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\Driver\Mysql;
use Cake\TestSuite\TestCase;
use \PDO;

/**
* Tests Mysql driver
*
*/
class MysqlTest extends \Cake\TestSuite\TestCase {
class MysqlTest extends TestCase {

/**
* Helper method for skipping tests that need a real connection.
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Database/Driver/PostgresTest.php
Expand Up @@ -15,7 +15,7 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Driver;
namespace Cake\Test\TestCase\Database\Driver;

use Cake\Core\Configure;
use Cake\Database\Connection;
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Database/Driver/SqliteTest.php
Expand Up @@ -15,7 +15,7 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Driver;
namespace Cake\Test\TestCase\Database\Driver;

use Cake\Core\Configure;
use Cake\Database\Connection;
Expand Down
Expand Up @@ -15,7 +15,7 @@
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Expression;
namespace Cake\Test\TestCase\Database\Expression;

use Cake\Database\Expression\FunctionExpression;

Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Database/FunctionsTraitTest.php
Expand Up @@ -15,7 +15,7 @@
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database;
namespace Cake\Test\TestCase\Database;

use Cake\Database\FunctionsTrait;

Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Database/Log/LoggedQueryTest.php
Expand Up @@ -15,7 +15,7 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Log;
namespace Cake\Test\TestCase\Database\Log;

use Cake\Database\Log\LoggedQuery;

Expand Down
Expand Up @@ -14,7 +14,7 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Log;
namespace Cake\Test\TestCase\Database\Log;

use Cake\Database\Log\LoggingStatement;
use PDOStatement;
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Database/Log/QueryLoggerTest.php
Expand Up @@ -14,7 +14,7 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Log;
namespace Cake\Test\TestCase\Database\Log;

use Cake\Database\Log\LoggedQuery;
use Cake\Database\Log\QueryLogger;
Expand Down
11 changes: 9 additions & 2 deletions lib/Cake/Test/TestCase/Database/QueryTest.php
Expand Up @@ -15,25 +15,32 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database;
namespace Cake\Test\TestCase\Database;

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\Query;
use Cake\TestSuite\TestCase;

/**
* Tests Query class
*
*/
class QueryTest extends \Cake\TestSuite\TestCase {
class QueryTest extends TestCase {

public function setUp() {
parent::setUp();
$this->connection = new Connection(Configure::read('Datasource.test'));
$this->connection->execute('DROP TABLE IF EXISTS articles');
$this->connection->execute('DROP TABLE IF EXISTS authors');
$this->connection->execute('DROP TABLE IF EXISTS dates');
}

public function tearDown() {
parent::tearDown();
unset($this->connection);
}

/**
* Test helper for creating tables.
*
Expand Down
26 changes: 25 additions & 1 deletion lib/Cake/Test/TestCase/Database/Schema/CollectionTest.php
Expand Up @@ -16,7 +16,9 @@
*/
namespace Cake\Test\TestCase\Database\Schema;

use Cake\Core\Configure;
use Cake\Database\Schema\Table;
use Cake\Database\Connection;
use Cake\Database\Schema\Collection;
use Cake\TestSuite\TestCase;

Expand All @@ -25,6 +27,27 @@
*/
class CollectionTest extends TestCase {

/**
* Setup function
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->connection = new Connection(Configure::read('Datasource.test'));
}

/**
* Teardown function
*
* @return void
*/
public function tearDown() {
parent::tearDown();
$this->connection->disconnect();
unset($this->connection);
}

/**
* Test getting table listings.
*
Expand All @@ -42,10 +65,11 @@ public function testDescribe() {
}

/**
* @expectedException Cake\Error\Exception
* @return void
*/
public function testDescribeIncorrectTable() {
$schema = new Collection($this->connection);
$this->assertNull($schema->describe('derp'));
}

}
Expand Up @@ -45,6 +45,7 @@ protected function _needsConnection() {
*/
protected function _createTables($connection) {
$this->_needsConnection();

$connection->execute('DROP TABLE IF EXISTS articles');
$connection->execute('DROP TABLE IF EXISTS authors');

Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Database/Type/BinaryTypeTest.php
Expand Up @@ -14,7 +14,7 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Type;
namespace Cake\Test\TestCase\Database\Type;

use Cake\Database\Type;
use Cake\Database\Type\BinaryType;
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Database/Type/DateTimeTypeTest.php
Expand Up @@ -14,7 +14,7 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Type;
namespace Cake\Test\TestCase\Database\Type;

use Cake\Database\Type;
use Cake\Database\Type\DateTimeType;
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Database/Type/DateTypeTest.php
Expand Up @@ -14,7 +14,7 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Type;
namespace Cake\Test\TestCase\Database\Type;

use Cake\Database\Type;
use Cake\Database\Type\DateType;
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Database/TypeTest.php
Expand Up @@ -15,7 +15,7 @@
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database;
namespace Cake\Test\TestCase\Database;

use Cake\Database\Type;
use PDO;
Expand Down

0 comments on commit 2012437

Please sign in to comment.