MongoDBHandlerTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Monolog\TestCase;
  12. use Monolog\Logger;
  13. class MongoDBHandlerTest extends TestCase
  14. {
  15. /**
  16. * @expectedException InvalidArgumentException
  17. */
  18. public function testConstructorShouldThrowExceptionForInvalidMongo()
  19. {
  20. new MongoDBHandler(new \stdClass(), 'DB', 'Collection');
  21. }
  22. public function testHandle()
  23. {
  24. $mongo = $this->getMock('Mongo', array('selectCollection'), array(), '', false);
  25. $collection = $this->getMock('stdClass', array('insert'));
  26. $mongo->expects($this->once())
  27. ->method('selectCollection')
  28. ->with('DB', 'Collection')
  29. ->will($this->returnValue($collection));
  30. $record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34));
  31. $expected = array(
  32. 'message' => 'test',
  33. 'context' => array('data' => '[object] (stdClass: {})', 'foo' => 34),
  34. 'level' => Logger::WARNING,
  35. 'level_name' => 'WARNING',
  36. 'channel' => 'test',
  37. 'datetime' => $record['datetime']->format('Y-m-d H:i:s'),
  38. 'extra' => array(),
  39. );
  40. $collection->expects($this->once())
  41. ->method('insert')
  42. ->with($expected);
  43. $handler = new MongoDBHandler($mongo, 'DB', 'Collection');
  44. $handler->handle($record);
  45. }
  46. public function testHandleWithManager() {
  47. if (!(class_exists('MongoDB\Driver\Manager'))) {
  48. $this->markTestSkipped('mongo extension not installed');
  49. }
  50. $manager = $this->getMock('MongoDB\Driver\Manager', array('executeBulkWrite'), array(), '', false);
  51. $record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34));
  52. $expected = array(
  53. 'message' => 'test',
  54. 'context' => array('data' => '[object] (stdClass: {})', 'foo' => 34),
  55. 'level' => Logger::WARNING,
  56. 'level_name' => 'WARNING',
  57. 'channel' => 'test',
  58. 'datetime' => $record['datetime']->format('Y-m-d H:i:s'),
  59. 'extra' => array(),
  60. );
  61. $bulk = new \MongoDB\Driver\BulkWrite();
  62. $bulk->insert($expected);
  63. $manager->expects($this->once())
  64. ->method('executeBulkWrite')
  65. ->with('DB.Collection', $bulk);
  66. $handler = new MongoDBHandler($manager, 'DB', 'Collection');
  67. $handler->handle($record);
  68. }
  69. }
  70. if (!class_exists('Mongo')) {
  71. class Mongo
  72. {
  73. public function selectCollection()
  74. {
  75. }
  76. }
  77. }