mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-11 15:44:34 +02:00
MongoDBHandler only supports ext-mongodb and mongodb/mongodb
The legacy driver (i.e. ext-mongo) is not supported on PHP 7, so there is no reason to leave behind its supporting code.
This commit is contained in:
@@ -11,8 +11,9 @@
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use MongoDB\Driver\Manager;
|
||||
use Monolog\TestCase;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Formatter\NormalizerFormatter;
|
||||
|
||||
class MongoDBHandlerTest extends TestCase
|
||||
{
|
||||
@@ -21,78 +22,57 @@ class MongoDBHandlerTest extends TestCase
|
||||
*/
|
||||
public function testConstructorShouldThrowExceptionForInvalidMongo()
|
||||
{
|
||||
new MongoDBHandler(new \stdClass(), 'DB', 'Collection');
|
||||
new MongoDBHandler(new \stdClass, 'db', 'collection');
|
||||
}
|
||||
|
||||
public function testHandle()
|
||||
public function testHandleWithLibraryClient()
|
||||
{
|
||||
$mongo = $this->getMock('Mongo', array('selectCollection'), array(), '', false);
|
||||
$collection = $this->getMock('stdClass', array('insert'));
|
||||
if (!(class_exists('MongoDB\Client'))) {
|
||||
$this->markTestSkipped('mongodb/mongodb not installed');
|
||||
}
|
||||
|
||||
$mongo->expects($this->once())
|
||||
$mongodb = $this->getMockBuilder('MongoDB\Client')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$collection = $this->getMockBuilder('MongoDB\Collection')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$mongodb->expects($this->once())
|
||||
->method('selectCollection')
|
||||
->with('DB', 'Collection')
|
||||
->with('db', 'collection')
|
||||
->will($this->returnValue($collection));
|
||||
|
||||
$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(),
|
||||
);
|
||||
$record = $this->getRecord();
|
||||
$expected = $record;
|
||||
$expected['datetime'] = $record['datetime']->format(NormalizerFormatter::SIMPLE_DATE);
|
||||
|
||||
$collection->expects($this->once())
|
||||
->method('insert')
|
||||
->method('insertOne')
|
||||
->with($expected);
|
||||
|
||||
$handler = new MongoDBHandler($mongo, 'DB', 'Collection');
|
||||
$handler = new MongoDBHandler($mongodb, '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')) {
|
||||
class Mongo
|
||||
public function testHandleWithDriverManager()
|
||||
{
|
||||
public function selectCollection()
|
||||
{
|
||||
if (!(class_exists('MongoDB\Driver\Manager'))) {
|
||||
$this->markTestSkipped('ext-mongodb not installed');
|
||||
}
|
||||
|
||||
/* This can become a unit test once ManagerInterface can be mocked.
|
||||
* See: https://jira.mongodb.org/browse/PHPC-378
|
||||
*/
|
||||
$mongodb = new Manager('mongodb://localhost:27017');
|
||||
$handler = new MongoDBHandler($mongodb, 'test', 'monolog');
|
||||
$record = $this->getRecord();
|
||||
|
||||
try {
|
||||
$handler->handle($record);
|
||||
} catch (\RuntimeException $e) {
|
||||
$this->markTestSkipped('Could not connect to MongoDB server on mongodb://localhost:27017');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user