Skip to content

Commit

Permalink
Adding getters and setters for logger objects
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum committed May 10, 2017
1 parent 6eb6968 commit 7149daf
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 24 deletions.
32 changes: 27 additions & 5 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,13 +814,35 @@ public function logQueries($enable = null)
public function logger($instance = null)
{
if ($instance === null) {
if ($this->_logger === null) {
$this->_logger = new QueryLogger();
}
return $this->getLogger();
}

$this->setLogger($instance);
}

/**
* Sets a logger
*
* @param object Logger object
* @return void
*/
public function setLogger($logger)
{
$this->_logger = $logger;
}

return $this->_logger;
/**
* Gets the logger object
*
* @return object logger instance
*/
public function getLogger()
{
if ($this->_logger === null) {
$this->_logger = new QueryLogger();
}
$this->_logger = $instance;

return $this->_logger;
}

/**
Expand Down
23 changes: 22 additions & 1 deletion src/Database/Log/LoggingStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,30 @@ public function bindValue($column, $value, $type = 'string')
public function logger($instance = null)
{
if ($instance === null) {
return $this->_logger;
return $this->getLogger();
}

return $this->_logger = $instance;
}

/**
* Sets a logger
*
* @param object Logger object
* @return void
*/
public function setLogger($logger)
{
$this->_logger = $logger;
}

/**
* Gets the logger object
*
* @return object logger instance
*/
public function getLogger()
{
return $this->_logger;
}
}
50 changes: 32 additions & 18 deletions tests/TestCase/Database/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Cake\Database\Connection;
use Cake\Database\Driver\Mysql;
use Cake\Database\Exception\NestedTransactionRollbackException;
use Cake\Database\Log\LoggingStatement;
use Cake\Database\Log\QueryLogger;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
use ReflectionMethod;
Expand Down Expand Up @@ -145,14 +147,14 @@ public function testDisabledDriver()
public function testDriverOptionClassNameSupport()
{
$connection = new Connection(['driver' => 'TestDriver']);
$this->assertInstanceOf('\TestApp\Database\Driver\TestDriver', $connection->driver());
$this->assertInstanceOf('\TestApp\Database\Driver\TestDriver', $connection->getDriver());

$connection = new Connection(['driver' => 'TestPlugin.TestDriver']);
$this->assertInstanceOf('\TestPlugin\Database\Driver\TestDriver', $connection->driver());
$this->assertInstanceOf('\TestPlugin\Database\Driver\TestDriver', $connection->getDriver());

list(, $name) = namespaceSplit(get_class($this->connection->driver()));
list(, $name) = namespaceSplit(get_class($this->connection->getDriver()));
$connection = new Connection(['driver' => $name]);
$this->assertInstanceOf(get_class($this->connection->driver()), $connection->driver());
$this->assertInstanceOf(get_class($this->connection->getDriver()), $connection->getDriver());
}

/**
Expand All @@ -163,9 +165,9 @@ public function testDriverOptionClassNameSupport()
*/
public function testWrongCredentials()
{
$config = ConnectionManager::config('test');
$config = ConnectionManager::getConfig('test');
$this->skipIf(isset($config['url']), 'Datasource has dsn, skipping.');
$connection = new Connection(['database' => '/dev/nonexistent'] + ConnectionManager::config('test'));
$connection = new Connection(['database' => '/dev/nonexistent'] + ConnectionManager::getConfig('test'));
$connection->connect();
}

Expand Down Expand Up @@ -794,23 +796,35 @@ public function testLoggerDefault()
*/
public function testSetLogger()
{
$logger = new \Cake\Database\Log\QueryLogger;
$logger = new QueryLogger;
$this->connection->logger($logger);
$this->assertSame($logger, $this->connection->logger());
}

/**
* Tests setting and getting the logger object
*
* @return void
*/
public function testGetAndSetLogger()
{
$logger = new QueryLogger();
$this->connection->setLogger($logger);
$this->assertSame($logger, $this->connection->getLogger());
}

/**
* Tests that statements are decorated with a logger when logQueries is set to true
*
* @return void
*/
public function testLoggerDecorator()
{
$logger = new \Cake\Database\Log\QueryLogger;
$logger = new QueryLogger;
$this->connection->logQueries(true);
$this->connection->logger($logger);
$st = $this->connection->prepare('SELECT 1');
$this->assertInstanceOf('Cake\Database\Log\LoggingStatement', $st);
$this->assertInstanceOf(LoggingStatement::class, $st);
$this->assertSame($logger, $st->logger());

$this->connection->logQueries(false);
Expand Down Expand Up @@ -839,7 +853,7 @@ public function testLogQueries()
*/
public function testLogFunction()
{
$logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
$logger = $this->getMockBuilder(QueryLogger::class)->getMock();
$this->connection->logger($logger);
$logger->expects($this->once())->method('log')
->with($this->logicalAnd(
Expand All @@ -857,7 +871,7 @@ public function testLogFunction()
public function testLogBeginRollbackTransaction()
{
$connection = $this
->getMockBuilder('\Cake\Database\Connection')
->getMockBuilder(Connection::class)
->setMethods(['connect'])
->disableOriginalConstructor()
->getMock();
Expand All @@ -866,7 +880,7 @@ public function testLogBeginRollbackTransaction()
$driver = $this->getMockFormDriver();
$connection->driver($driver);

$logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
$logger = $this->getMockBuilder(QueryLogger::class)->getMock();
$connection->logger($logger);
$logger->expects($this->at(0))->method('log')
->with($this->logicalAnd(
Expand All @@ -892,12 +906,12 @@ public function testLogBeginRollbackTransaction()
public function testLogCommitTransaction()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMockBuilder('\Cake\Database\Connection')
$connection = $this->getMockBuilder(Connection::class)
->setMethods(['connect'])
->setConstructorArgs([['driver' => $driver]])
->getMock();

$logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
$logger = $this->getMockBuilder(QueryLogger::class)->getMock();
$connection->logger($logger);

$logger->expects($this->at(1))->method('log')
Expand All @@ -919,7 +933,7 @@ public function testLogCommitTransaction()
public function testTransactionalSuccess()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMockBuilder('\Cake\Database\Connection')
$connection = $this->getMockBuilder(Connection::class)
->setMethods(['connect', 'commit', 'begin'])
->setConstructorArgs([['driver' => $driver]])
->getMock();
Expand All @@ -942,7 +956,7 @@ public function testTransactionalSuccess()
public function testTransactionalFail()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMockBuilder('\Cake\Database\Connection')
$connection = $this->getMockBuilder(Connection::class)
->setMethods(['connect', 'commit', 'begin', 'rollback'])
->setConstructorArgs([['driver' => $driver]])
->getMock();
Expand All @@ -968,7 +982,7 @@ public function testTransactionalFail()
public function testTransactionalWithException()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMockBuilder('\Cake\Database\Connection')
$connection = $this->getMockBuilder(Connection::class)
->setMethods(['connect', 'commit', 'begin', 'rollback'])
->setConstructorArgs([['driver' => $driver]])
->getMock();
Expand All @@ -989,7 +1003,7 @@ public function testTransactionalWithException()
public function testSchemaCollection()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMockBuilder('\Cake\Database\Connection')
$connection = $this->getMockBuilder(Connection::class)
->setMethods(['connect'])
->setConstructorArgs([['driver' => $driver]])
->getMock();
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Database/Log/LoggingStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,18 @@ public function testExecuteWithError()
$st->logger($logger);
$st->execute();
}

/**
* Tests setting and getting the logger
*
* @return void
*/
public function testSetAndGetLogger()
{
$logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
$st = new LoggingStatement();
$this->assertNull($st->getLogger());
$st->setLogger($logger);
$this->assertSame($logger, $st->getLogger());
}
}

0 comments on commit 7149daf

Please sign in to comment.