mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-26 18:16:24 +01:00
* Remove unused code path in MongoDBFormatter Monolog already required mongodb/mongodb 1.8+ (and a related ext-mongodb version) so this code path was never used. * Use Collection::getCollection() from mongodb/mongodb 1.21 The original selectCollection method is deprecated. Since Monolog itself requires PHP 8.1, it should be sane to rely on 1.21+, which shares the same requirement. * Update class refs for MongoDB extension and library * Conditionally use Collection getter by version * Relax PHPLIB version requirement * Use RequiresPhpExtension attribute to require ext-mongodb * Import UTCDateTime class and revise var names
74 lines
2.1 KiB
PHP
74 lines
2.1 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
/*
|
|
* 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;
|
|
|
|
use MongoDB\BSON\UTCDateTime;
|
|
use MongoDB\Client;
|
|
use MongoDB\Collection;
|
|
use MongoDB\Driver\Manager;
|
|
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
|
|
|
#[RequiresPhpExtension('mongodb')]
|
|
class MongoDBHandlerTest extends \Monolog\Test\MonologTestCase
|
|
{
|
|
public function testConstructorShouldThrowExceptionForInvalidMongo()
|
|
{
|
|
$this->expectException(\TypeError::class);
|
|
|
|
new MongoDBHandler(new \stdClass, 'db', 'collection');
|
|
}
|
|
|
|
public function testHandleWithLibraryClient()
|
|
{
|
|
if (!class_exists(Client::class)) {
|
|
$this->markTestSkipped('mongodb/mongodb not installed');
|
|
}
|
|
|
|
$client = $this->getMockBuilder(Client::class)
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$collection = $this->getMockBuilder(Collection::class)
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$client->expects($this->once())
|
|
->method('getCollection')
|
|
->with('db', 'collection')
|
|
->willReturn($collection);
|
|
|
|
$record = $this->getRecord();
|
|
$expected = $record->toArray();
|
|
$expected['datetime'] = new UTCDateTime((int) floor(((float) $record->datetime->format('U.u')) * 1000));
|
|
|
|
$collection->expects($this->once())
|
|
->method('insertOne')
|
|
->with($expected);
|
|
|
|
$handler = new MongoDBHandler($client, 'db', 'collection');
|
|
$handler->handle($record);
|
|
}
|
|
|
|
public function testHandleWithDriverManager()
|
|
{
|
|
$manager = new Manager('mongodb://localhost:27017');
|
|
$handler = new MongoDBHandler($manager, 'test', 'monolog');
|
|
$record = $this->getRecord();
|
|
|
|
try {
|
|
$handler->handle($record);
|
|
} catch (\RuntimeException $e) {
|
|
$this->markTestSkipped('Could not connect to MongoDB server on mongodb://localhost:27017');
|
|
}
|
|
}
|
|
}
|