MongoDBHandlerTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use MongoDB\Driver\Manager;
  12. use Monolog\TestCase;
  13. use Monolog\Formatter\NormalizerFormatter;
  14. class MongoDBHandlerTest extends TestCase
  15. {
  16. /**
  17. * @expectedException InvalidArgumentException
  18. */
  19. public function testConstructorShouldThrowExceptionForInvalidMongo()
  20. {
  21. new MongoDBHandler(new \stdClass, 'db', 'collection');
  22. }
  23. public function testHandleWithLibraryClient()
  24. {
  25. if (!(class_exists('MongoDB\Client'))) {
  26. $this->markTestSkipped('mongodb/mongodb not installed');
  27. }
  28. $mongodb = $this->getMockBuilder('MongoDB\Client')
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $collection = $this->getMockBuilder('MongoDB\Collection')
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $mongodb->expects($this->once())
  35. ->method('selectCollection')
  36. ->with('db', 'collection')
  37. ->will($this->returnValue($collection));
  38. $record = $this->getRecord();
  39. $expected = $record;
  40. $expected['datetime'] = $record['datetime']->format(NormalizerFormatter::SIMPLE_DATE);
  41. $collection->expects($this->once())
  42. ->method('insertOne')
  43. ->with($expected);
  44. $handler = new MongoDBHandler($mongodb, 'db', 'collection');
  45. $handler->handle($record);
  46. }
  47. public function testHandleWithDriverManager()
  48. {
  49. if (!(class_exists('MongoDB\Driver\Manager'))) {
  50. $this->markTestSkipped('ext-mongodb not installed');
  51. }
  52. /* This can become a unit test once ManagerInterface can be mocked.
  53. * See: https://jira.mongodb.org/browse/PHPC-378
  54. */
  55. $mongodb = new Manager('mongodb://localhost:27017');
  56. $handler = new MongoDBHandler($mongodb, 'test', 'monolog');
  57. $record = $this->getRecord();
  58. try {
  59. $handler->handle($record);
  60. } catch (\RuntimeException $e) {
  61. $this->markTestSkipped('Could not connect to MongoDB server on mongodb://localhost:27017');
  62. }
  63. }
  64. }