Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/log-db-formatter'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Aug 23, 2012

Verified

This commit was signed with the committer’s verified signature.
Robbepop Robin Freyler
Showing 4 changed files with 189 additions and 19 deletions.
78 changes: 78 additions & 0 deletions src/Formatter/Db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Log
*/

namespace Zend\Log\Formatter;

use DateTime;
use Zend\Log\Exception;

/**
* @category Zend
* @package Zend_Log
* @subpackage Formatter
*/
class Db implements FormatterInterface
{
/**
* Format specifier for DateTime objects in event data (default: ISO 8601)
*
* @see http://php.net/manual/en/function.date.php
* @var string
*/
protected $dateTimeFormat = self::DEFAULT_DATETIME_FORMAT;

/**
* Class constructor
*
* @see http://php.net/manual/en/function.date.php
* @param null|string $dateTimeFormat Format specifier for DateTime objects in event data
*/
public function __construct($dateTimeFormat = null)
{
if (null !== $dateTimeFormat) {
$this->setDateTimeFormat($dateTimeFormat);
}
}

/**
* Formats data to be written by the writer.
*
* @param array $event event data
* @return array
*/
public function format($event)
{
$format = $this->getDateTimeFormat();
array_walk_recursive($event, function (&$value) use ($format) {
if ($value instanceof DateTime) {
$value = $value->format($format);
}
});

return $event;
}

/**
* {@inheritDoc}
*/
public function getDateTimeFormat()
{
return $this->dateTimeFormat;
}

/**
* {@inheritDoc}
*/
public function setDateTimeFormat($dateTimeFormat)
{
$this->dateTimeFormat = (string) $dateTimeFormat;
return $this;
}
}
15 changes: 4 additions & 11 deletions src/Writer/Db.php
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
use Zend\Db\Adapter\Adapter;
use Zend\Log\Exception;
use Zend\Log\Formatter;
use Zend\Log\Formatter\Db as DbFormatter;

/**
* @category Zend
@@ -91,18 +92,8 @@ public function __construct($db, $tableName = null, array $columnMap = null, $se
if (!empty($separator)) {
$this->separator = $separator;
}
}

/**
* Formatting is not possible on this writer
*
* @param Formatter\FormatterInterface $formatter
* @return void
* @throws Exception\InvalidArgumentException
*/
public function setFormatter(Formatter\FormatterInterface $formatter)
{
throw new Exception\InvalidArgumentException(get_class() . ' does not support formatting');
$this->setFormatter(new DbFormatter());
}

/**
@@ -128,6 +119,8 @@ protected function doWrite(array $event)
throw new Exception\RuntimeException('Database adapter is null');
}

$event = $this->formatter->format($event);

// Transform the event array into fields
if (null === $this->columnMap) {
$dataToInsert = $this->eventIntoColumn($event);
73 changes: 73 additions & 0 deletions test/Formatter/DbTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Log
*/

namespace ZendTest\Log\Formatter;

use DateTime;
use ZendTest\Log\TestAsset\StringObject;
use Zend\Log\Formatter\Db as DbFormatter;

/**
* @category Zend
* @package Zend_Log
* @subpackage UnitTests
* @group Zend_Log
*/
class DbTest extends \PHPUnit_Framework_TestCase
{
public function testDefaultDateTimeFormat()
{
$formatter = new DbFormatter();
$this->assertEquals(DbFormatter::DEFAULT_DATETIME_FORMAT, $formatter->getDateTimeFormat());
}

/**
* @dataProvider provideDateTimeFormats
*/
public function testSetDateTimeFormat($dateTimeFormat)
{
$formatter = new DbFormatter();
$formatter->setDateTimeFormat($dateTimeFormat);

$this->assertEquals($dateTimeFormat, $formatter->getDateTimeFormat());
}

/**
* @return array
*/
public function provideDateTimeFormats()
{
return array(
array('r'),
array('U'),
array(DateTime::RSS),
);
}

/**
* @dataProvider provideDateTimeFormats
*/
public function testAllowsSpecifyingDateTimeFormatAsConstructorArgument($dateTimeFormat)
{
$formatter = new DbFormatter($dateTimeFormat);

$this->assertEquals($dateTimeFormat, $formatter->getDateTimeFormat());
}

public function testFormatDateTimeInEvent()
{
$datetime = new DateTime();
$event = array('timestamp' => $datetime);
$formatter = new DbFormatter();

$format = DbFormatter::DEFAULT_DATETIME_FORMAT;
$this->assertContains($datetime->format($format), $formatter->format($event));
}
}
42 changes: 34 additions & 8 deletions test/Writer/DbTest.php
Original file line number Diff line number Diff line change
@@ -10,11 +10,11 @@

namespace ZendTest\Log\Writer;

use DateTime;
use ZendTest\Log\TestAsset\MockDbAdapter;
use ZendTest\Log\TestAsset\MockDbDriver;
use Zend\Log\Writer\Db as DbWriter;
use Zend\Log\Logger;
use Zend\Log\Formatter\Simple as SimpleFormatter;
use Zend\Log\Formatter\FormatterInterface;

/**
* @category Zend
@@ -32,12 +32,6 @@ public function setUp()
$this->writer = new DbWriter($this->db, $this->tableName);
}

public function testFormattingIsNotSupported()
{
$this->setExpectedException('Zend\Log\Exception\InvalidArgumentException', 'does not support formatting');
$this->writer->setFormatter(new SimpleFormatter);
}

public function testNotPassingTableNameToConstructorThrowsException()
{
$this->setExpectedException('Zend\Log\Exception\InvalidArgumentException', 'table name');
@@ -212,4 +206,36 @@ public function testThrowStrictSetFormatter()
$this->setExpectedException('PHPUnit_Framework_Error');
$this->writer->setFormatter(new \StdClass());
}

public function testWriteDateTimeAsTimestamp()
{
$date = new DateTime();
$event = array('timestamp'=> $date);
$this->writer->write($event);

$this->assertContains('query', array_keys($this->db->calls));
$this->assertEquals(1, count($this->db->calls['query']));

$this->assertEquals(array(array(
'timestamp' => $date->format(FormatterInterface::DEFAULT_DATETIME_FORMAT)
)), $this->db->calls['execute'][0]);
}

public function testWriteDateTimeAsExtraValue()
{
$date = new DateTime();
$event = array(
'extra'=> array(
'request_time' => $date
)
);
$this->writer->write($event);

$this->assertContains('query', array_keys($this->db->calls));
$this->assertEquals(1, count($this->db->calls['query']));

$this->assertEquals(array(array(
'extra_request_time' => $date->format(FormatterInterface::DEFAULT_DATETIME_FORMAT)
)), $this->db->calls['execute'][0]);
}
}

0 comments on commit f94838d

Please sign in to comment.