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

Commit

Permalink
Merge branch 'mongo-log-writer' of https://github.com/jmikola/zf2
Browse files Browse the repository at this point in the history
Conflicts:
	library/Zend/Log/composer.json
  • Loading branch information
weierophinney committed Jul 12, 2012
2 parents 7946c9f + 9b773b4 commit 89295c5
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/Writer/MongoDB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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\Writer;

use Mongo;
use Zend\Log\Exception\InvalidArgumentException;
use Zend\Log\Exception\RuntimeException;
use Zend\Log\Formatter\FormatterInterface;

/**
* MongoDB log writer.
*
* @category Zend
* @package Zend_Log
* @subpackage Writer
*/
class MongoDB extends AbstractWriter
{
/**
* MongoCollection instance
*
* @var MongoCollection
*/
protected $mongoCollection;

/**
* Options used for MongoCollection::save()
*
* @var array
*/
protected $saveOptions;

/**
* Constructor
*
* @param Mongo $mongo
* @param string $database
* @param string $collection
* @param array $saveOptions
* @return Zend\Log\Writer\MongoDB
*/
public function __construct(Mongo $mongo, $database, $collection, array $saveOptions = array())
{
$this->mongoCollection = $mongo->selectCollection($database, $collection);
$this->saveOptions = $saveOptions;
}

/**
* This writer does not support formatting.
*
* @param Zend\Log\Formatter\FormatterInterface $formatter
* @return void
* @throws Zend\Log\Exception\InvalidArgumentException
*/
public function setFormatter(FormatterInterface $formatter)
{
throw new InvalidArgumentException(get_class() . ' does not support formatting');
}

/**
* Write a message to the log.
*
* @param array $event Event data
* @return void
* @throws Zend\Log\Exception\RuntimeException
*/
protected function doWrite(array $event)
{
if (null === $this->mongoCollection) {
throw new RuntimeException('MongoCollection must be defined');
}

$this->mongoCollection->save($event, $this->saveOptions);
}
}
86 changes: 86 additions & 0 deletions test/Writer/MongoDBTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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\Writer;

use Zend\Log\Logger;
use Zend\Log\Writer\MongoDB as MongoDBWriter;

/**
* @category Zend
* @package Zend_Log
* @subpackage UnitTests
* @group Zend_Log
*/
class MongoDBTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!extension_loaded('mongo')) {
$this->markTestSkipped('The mongo PHP extension is not available');
}

$this->database = 'zf2_test';
$this->collection = 'logs';

$this->mongo = $this->getMockBuilder('Mongo')
->disableOriginalConstructor()
->setMethods(array('selectCollection'))
->getMock();

$this->mongoCollection = $this->getMockBuilder('MongoCollection')
->disableOriginalConstructor()
->setMethods(array('save'))
->getMock();

$this->mongo->expects($this->any())
->method('selectCollection')
->with($this->database, $this->collection)
->will($this->returnValue($this->mongoCollection));
}

/**
* @expectedException Zend\Log\Exception\InvalidArgumentException
*/
public function testFormattingIsNotSupported()
{
$writer = new MongoDBWriter($this->mongo, $this->database, $this->collection);

$writer->setFormatter($this->getMock('Zend\Log\Formatter\FormatterInterface'));
}

public function testWriteWithDefaultSaveOptions()
{
$event = array('message'=> 'foo', 'priority' => 42);

$this->mongoCollection->expects($this->once())
->method('save')
->with($event, array());

$writer = new MongoDBWriter($this->mongo, $this->database, $this->collection);

$writer->write($event);
}

public function testWriteWithCustomSaveOptions()
{
$event = array('message' => 'foo', 'priority' => 42);
$saveOptions = array('safe' => false, 'fsync' => false, 'timeout' => 100);

$this->mongoCollection->expects($this->once())
->method('save')
->with($event, $saveOptions);

$writer = new MongoDBWriter($this->mongo, $this->database, $this->collection, $saveOptions);

$writer->write($event);
}
}

0 comments on commit 89295c5

Please sign in to comment.