Skip to content

Commit

Permalink
Added test code for the warning log message content.
Browse files Browse the repository at this point in the history
  • Loading branch information
motooka committed Aug 13, 2017
1 parent f5d982e commit 4fd7896
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions tests/TestCase/Database/ConnectionTest.php
Expand Up @@ -18,6 +18,7 @@
use Cake\Database\Driver\Mysql;
use Cake\Database\Exception\NestedTransactionRollbackException;
use Cake\Datasource\ConnectionManager;
use Cake\Log\Log;
use Cake\TestSuite\TestCase;
use ReflectionMethod;

Expand Down Expand Up @@ -52,6 +53,7 @@ public function setUp()

public function tearDown()
{
Log::reset();
$this->connection->useSavePoints(false);
unset($this->connection);
parent::tearDown();
Expand Down Expand Up @@ -488,7 +490,7 @@ public function testSimpleTransactions()
}

/**
* Tests that the destructor of Connection does not crash
* Tests that the destructor of Connection generates a warning log
* when transaction is not closed
*
* @return void
Expand All @@ -500,10 +502,36 @@ public function testCloseConnectionWithUncommittedTransaction()
->setMethods(['connect'])
->setConstructorArgs([['driver' => $driver]])
->getMock();
$connection->begin();
$this->assertTrue($connection->inTransaction());

// The returned value of getMock() is referenced by TestCase,
// so it's impossible(or difficult) to call __destruct() of it during the test.
// Instead, use cloned object.
$clonedConnection = clone $connection;
$clonedConnection->begin();
$this->assertTrue($clonedConnection->inTransaction());

Log::setConfig('debug', [
'engine' => 'File',
'path' => LOGS,
'levels' => ['notice', 'info', 'debug'],
'file' => 'debug',
]);
Log::setConfig('error', [
'engine' => 'File',
'path' => LOGS,
'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
'file' => 'error',
]);
$errorLogFile = LOGS . 'error.log';
if (file_exists($errorLogFile)) {
unlink($errorLogFile);
}

// a warning log will be generated by __destruct of Connection.
unset($clonedConnection);
$this->assertFileExists($errorLogFile);
$logContent = file_get_contents($errorLogFile);
$this->assertStringMatchesFormat('%AWarning: The connection is going to be closed but the transaction is still active.%A', $logContent);
}

/**
Expand Down

0 comments on commit 4fd7896

Please sign in to comment.