mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-12 16:14:08 +02:00
Add case for if manager is passed in
This commit is contained in:
@@ -28,14 +28,20 @@ use Monolog\Formatter\NormalizerFormatter;
|
||||
class MongoDBHandler extends AbstractProcessingHandler
|
||||
{
|
||||
protected $mongoCollection;
|
||||
protected $namespace;
|
||||
protected $manager;
|
||||
|
||||
public function __construct($mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true)
|
||||
{
|
||||
if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo || $mongo instanceof \MongoDB\Client)) {
|
||||
if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo || $mongo instanceof \MongoDB\Client || $mongo instanceof \MongoDB\Driver\Manager)) {
|
||||
throw new \InvalidArgumentException('MongoClient, Mongo or MongoDB\Client instance required');
|
||||
}
|
||||
|
||||
$this->namespace = "$database.$collection";
|
||||
if($mongo instanceof \MongoDB\Driver\Manger) {
|
||||
$this->manager = $mongo;
|
||||
} else {
|
||||
$this->mongoCollection = $mongo->selectCollection($database, $collection);
|
||||
}
|
||||
|
||||
parent::__construct($level, $bubble);
|
||||
}
|
||||
@@ -44,8 +50,12 @@ class MongoDBHandler extends AbstractProcessingHandler
|
||||
{
|
||||
if ($this->mongoCollection instanceof \MongoDB\Collection) {
|
||||
$this->mongoCollection->insertOne($record["formatted"]);
|
||||
} else {
|
||||
} else if($this->mongoCollection instanceof \MongoCollection) {
|
||||
$this->mongoCollection->insert($record["formatted"]);
|
||||
} else {
|
||||
$bulk = new \MongoDB\Driver\BulkWrite();
|
||||
$bulk->insert($record["formatted"]);
|
||||
$this->$manager->executeBulkWrite($this->namespace, $bulk);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -27,7 +27,7 @@ class MongoDBHandlerTest extends TestCase
|
||||
public function testHandle()
|
||||
{
|
||||
$mongo = $this->getMock('Mongo', array('selectCollection'), array(), '', false);
|
||||
$collection = $this->getMock('stdClass', array('save'));
|
||||
$collection = $this->getMock('stdClass', array('insert'));
|
||||
|
||||
$mongo->expects($this->once())
|
||||
->method('selectCollection')
|
||||
@@ -47,12 +47,45 @@ class MongoDBHandlerTest extends TestCase
|
||||
);
|
||||
|
||||
$collection->expects($this->once())
|
||||
->method('save')
|
||||
->method('insert')
|
||||
->with($expected);
|
||||
|
||||
$handler = new MongoDBHandler($mongo, 'DB', 'Collection');
|
||||
$handler->handle($record);
|
||||
}
|
||||
|
||||
public function testHandleWithManager() {
|
||||
if (!(class_exists('MongoDB\Driver\Manager'))) {
|
||||
$this->markTestSkipped('mongo extension not installed');
|
||||
}
|
||||
|
||||
$manager = $this->getMock('MongoDB\Driver\Manager', array('executeBulkWrite'), array(), '', false);
|
||||
|
||||
|
||||
$record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34));
|
||||
$expected = array(
|
||||
'message' => 'test',
|
||||
'context' => array('data' => '[object] (stdClass: {})', 'foo' => 34),
|
||||
'level' => Logger::WARNING,
|
||||
'level_name' => 'WARNING',
|
||||
'channel' => 'test',
|
||||
'datetime' => $record['datetime']->format('Y-m-d H:i:s'),
|
||||
'extra' => array(),
|
||||
);
|
||||
|
||||
$bulk = new \MongoDB\Driver\BulkWrite();
|
||||
$bulk->insert($expected);
|
||||
|
||||
$manager->expects($this->once())
|
||||
->method('executeBulkWrite')
|
||||
->with('DB.Collection', $bulk);
|
||||
|
||||
|
||||
$handler = new MongoDBHandler($manager, 'DB', 'Collection');
|
||||
$handler->handle($record);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!class_exists('Mongo')) {
|
||||
|
Reference in New Issue
Block a user