Skip to content

Commit

Permalink
Use mocks to fix test failures.
Browse files Browse the repository at this point in the history
Postgres keeps one connection between multiple PDO instances. This
causes spurious failures when tests are run in suites.
  • Loading branch information
markstory committed Jun 28, 2013
1 parent 83d1100 commit 7818cde
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
34 changes: 22 additions & 12 deletions lib/Cake/Test/TestCase/Database/ConnectionTest.php
Expand Up @@ -18,17 +18,17 @@

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

/**
* Tests Connection class
*
**/
*/
class ConnectionTest extends TestCase {

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

public function tearDown() {
Expand Down Expand Up @@ -673,8 +673,13 @@ public function testLogFunction() {
* @return void
*/
public function testLogBeginRollbackTransaction() {
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect'],
[Configure::read('Datasource.test')]
);
$logger = $this->getMock('\Cake\Database\Log\QueryLogger');
$this->connection->logger($logger);
$connection->logger($logger);
$logger->expects($this->at(0))->method('log')
->with($this->logicalAnd(
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
Expand All @@ -685,10 +690,10 @@ public function testLogBeginRollbackTransaction() {
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
$this->attributeEqualTo('query', 'ROLLBACK')
));
$this->connection->logQueries(true);
$this->connection->begin();
$this->connection->begin(); //This one will not be logged
$this->connection->rollback();
$connection->logQueries(true);
$connection->begin();
$connection->begin(); //This one will not be logged
$connection->rollback();
}

/**
Expand All @@ -698,15 +703,20 @@ public function testLogBeginRollbackTransaction() {
*/
public function testLogCommitTransaction() {
$logger = $this->getMock('\Cake\Database\Log\QueryLogger');
$this->connection->logger($logger);
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect'],
[Configure::read('Datasource.test')]
);
$connection->logger($logger);
$logger->expects($this->at(1))->method('log')
->with($this->logicalAnd(
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
$this->attributeEqualTo('query', 'COMMIT')
));
$this->connection->logQueries(true);
$this->connection->begin();
$this->connection->commit();
$connection->logQueries(true);
$connection->begin();
$connection->commit();
}

}
1 change: 0 additions & 1 deletion lib/Cake/Test/TestCase/Database/Driver/PostgresTest.php
Expand Up @@ -25,7 +25,6 @@

/**
* Tests Postgres driver
*
*/
class PostgresTest extends \Cake\TestSuite\TestCase {

Expand Down

0 comments on commit 7818cde

Please sign in to comment.