MongoDBHandlerTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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'));
  25. $collection = $this->getMock('stdClass', array('save'));
  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('save')
  42. ->with($expected);
  43. $handler = new MongoDBHandler($mongo, 'DB', 'Collection');
  44. $handler->handle($record);
  45. }
  46. }
  47. if (!class_exists('Mongo')) {
  48. class Mongo
  49. {
  50. public function selectCollection() {}
  51. }
  52. }