diff --git a/src/Monolog/Handler/MongoDBHandler.php b/src/Monolog/Handler/MongoDBHandler.php index 210bb19f..5a59201a 100644 --- a/src/Monolog/Handler/MongoDBHandler.php +++ b/src/Monolog/Handler/MongoDBHandler.php @@ -29,8 +29,12 @@ class MongoDBHandler extends AbstractProcessingHandler { private $mongoCollection; - public function __construct(\Mongo $mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true) + public function __construct($mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true) { + if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo)) { + throw new \InvalidArgumentException('MongoClient or Mongo instance required'); + } + $this->mongoCollection = $mongo->selectCollection($database, $collection); parent::__construct($level, $bubble); diff --git a/tests/Monolog/Handler/MongoDBHandlerTest.php b/tests/Monolog/Handler/MongoDBHandlerTest.php index 687dd53b..ce3433e1 100644 --- a/tests/Monolog/Handler/MongoDBHandlerTest.php +++ b/tests/Monolog/Handler/MongoDBHandlerTest.php @@ -16,6 +16,14 @@ use Monolog\Logger; class MongoDBHandlerTest extends TestCase { + /** + * @expectedException InvalidArgumentException + */ + public function testConstructorShouldThrowExceptionForInvalidMongo() + { + new MongoDBHandler(new \stdClass(), 'DB', 'Collection'); + } + public function testHandle() { $mongo = $this->getMock('Mongo', array('selectCollection'));