1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-03 11:47:38 +02:00

mock implemenation, kind of

This commit is contained in:
Andrew Tch
2012-06-13 17:10:09 +03:00
parent a9df0f461a
commit 72ee0aee96
3 changed files with 87 additions and 86 deletions

View File

@@ -16,24 +16,33 @@ use Monolog\Formatter\JsonFormatter;
class AmqpHandler extends AbstractProcessingHandler class AmqpHandler extends AbstractProcessingHandler
{ {
/** @var \AMQPExchange $exchange */ /**
* @var \AMQPExchange $exchange
*/
protected $exchange; protected $exchange;
/** @var string $space */
protected $space;
/** /**
* @param \AMQPConnection $amqp AMQP connection, ready for use * Describes current issuer (e.g. "database", "landing", "server" and so on)
* @var string $issuer
*/
protected $issuer;
/**
* @param \AMQPExchange $exchange AMQP exchange, ready for use
* @param string $exchangeName * @param string $exchangeName
* @param string $space string to be able better manage routing keys * @param string $issuer isser name
* @param int $level * @param int $level
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
*/ */
function __construct(\AMQPConnection $amqp, $exchangeName = 'log', $space = '', $level = Logger::DEBUG, $bubble = true) public function __construct($exchange, $exchangeName = 'log', $issuer = 'default', $level = Logger::DEBUG, $bubble = true)
{ {
$this->space = $space; if (!$exchange instanceof \AMQPExchange) {
$channel = new \AMQPChannel($amqp); throw new \LogicException('AMQP handler requires a valid exchange to be provided');
$this->exchange = new \AMQPExchange($channel); }
$this->exchange = $exchange;
$this->exchange->setName($exchangeName); $this->exchange->setName($exchangeName);
parent::__construct($level, $bubble); parent::__construct($level, $bubble);
} }
@@ -46,9 +55,19 @@ class AmqpHandler extends AbstractProcessingHandler
protected function write(array $record) protected function write(array $record)
{ {
$data = $record["formatted"]; $data = $record["formatted"];
$routingKey = substr(strtolower($record['level_name']),0,4 ).'.'.$this->space;
$this->exchange->publish($data, $routingKey, 0, $routingKey = sprintf('%s.%s',
array('delivery_mode' => 2, 'Content-type' => 'application/json')); substr($record['level_name'], 0, 4),
$this->getIssuer());
//we do not check return value because no handler really does
$this->exchange->publish($data,
strtolower($routingKey),
0,
array(
'delivery_mode' => 2,
'Content-type' => 'application/json'
));
} }
/** /**
@@ -58,4 +77,22 @@ class AmqpHandler extends AbstractProcessingHandler
{ {
return new JsonFormatter(); return new JsonFormatter();
} }
/**
* Issuer setter
* @param string $issuer
*/
public function setIssuer($issuer)
{
$this->issuer = $issuer;
}
/**
* Issuer getter
* @return string
*/
public function getIssuer()
{
return $this->issuer;
}
} }

View File

@@ -28,31 +28,56 @@ class AmqpHandlerTest extends TestCase
if (!class_exists('AMQPChannel')) { if (!class_exists('AMQPChannel')) {
throw new \Exception(' Please update AMQP to version >= 1'); throw new \Exception(' Please update AMQP to version >= 1');
} }
require_once __DIR__ . '/AmqpMocks.php';
} }
/**
public function testConstruct() * @covers Monolog\Handler\AmqpHandler::__construct
* @covers Monolog\Handler\AmqpHandler::handle
* @covers Monolog\Handler\AmqpHandler::write
* @covers Monolog\Handler\AmqpHandler::getDefaultFormatter
*/
public function testHandle()
{ {
// $handler = new AmqpHandler($this->getMockAMQPConnection(), 'log', 'monolog'); $exchange = $this->getExchange();
// $this->assertInstanceOf('Monolog\Handler\AmqpHandler', $handler);
$handler = new AmqpHandler($exchange, 'log', 'test');
$record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34));
$handler->handle($record);
} }
public function testWrite() protected function getExchange()
{ {
/* sorry, but PHP bug in zend_object_store_get_object segfaults
php where using mocks on AMQP classes. should be fixed someday,
but now it's time for some shitcode (see below)
$exchange = $this->getMockBuilder('\AMQPExchange')
->setConstructorArgs(array($this->getMock('\AMQPChannel')))
->setMethods(array('setName'))
->getMock();
$exchange->expects($this->any())
->method('setName')
->will($this->returnValue(true));
*/
return new MockAMQPExchange();
}
} }
public function getMockAMQPConnection() class MockAMQPExchange extends \AMQPExchange
{
public function __construct()
{ {
return new MockAMQPConnection();
} }
public function tearDown() public function publish($message, $routing_key, $params = 0, $attributes = array())
{ {
foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) { return true;
unlink($file); }
}
public function setName($name)
{
return true;
} }
} }

View File

@@ -1,61 +0,0 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler;
class MockAMQPConnection extends \AMQPConnection
{
public function __construct(){
}
public function pconnect()
{
return true;
}
public function reconnect()
{
return false;
}
public function connect() {
return true;
}
public function isConnected() {
return true;
}
}
class MockAMQPChannel extends \AMQPChannel
{
public function __construct(MockAMQPConnection $connection) {
}
}
class MockAMQPExchange extends \AMQPExchange
{
public function __construct(MockAMQPChannel $channel) {
}
public function publish($message, $routing_key, $flags, $headers){
return;
}
public function setName($exchangeName) {
return true;
}
}