From dc4a4b456c6fbf71efa00975f6dd4abeb669465a Mon Sep 17 00:00:00 2001 From: Dmitriy Makarov Date: Thu, 4 Aug 2016 20:19:04 +0300 Subject: [PATCH] Test for logger, flush method (#12041) * Added test for logger, flush method - testFlushWithoutDispatcher - testFlushWitDispatcherAndDefaultParam - testFlushWitDispatcherAndDefinedParam * Used 'yii\\log\\Dispatcher' instead of Dispatcher::class as parameter for the getMock method --- tests/framework/log/LoggerTest.php | 52 +++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/framework/log/LoggerTest.php b/tests/framework/log/LoggerTest.php index a791f8e..cf7db01 100644 --- a/tests/framework/log/LoggerTest.php +++ b/tests/framework/log/LoggerTest.php @@ -18,9 +18,15 @@ class LoggerTest extends TestCase */ protected $logger; + /** + * @var Dispatcher + */ + protected $dispatcher; + protected function setUp() { $this->logger = new Logger(); + $this->dispatcher = $this->getMock('yii\\log\\Dispatcher', ['dispatch']); } /** @@ -56,7 +62,7 @@ class LoggerTest extends TestCase $this->assertEquals('application', $this->logger->messages[0][2]); $this->assertEquals([ 'file' => __FILE__, - 'line' => 52, + 'line' => 58, 'function' => 'log', 'class' => get_class($this->logger), 'type' => '->' @@ -74,4 +80,48 @@ class LoggerTest extends TestCase $logger->expects($this->exactly(1))->method('flush'); $logger->log('test1', Logger::LEVEL_INFO); } + + /** + * @covers yii\log\Logger::Flush() + */ + public function testFlushWithoutDispatcher() + { + $dispatcher = $this->getMock('\stdClass'); + $dispatcher->expects($this->never())->method($this->anything()); + + $this->logger->messages = ['anything']; + $this->logger->dispatcher = $dispatcher; + $this->logger->flush(); + $this->assertEmpty($this->logger->messages); + } + + /** + * @covers yii\log\Logger::Flush() + */ + public function testFlushWithDispatcherAndDefaultParam() + { + $message = ['anything']; + $this->dispatcher->expects($this->once()) + ->method('dispatch')->with($this->equalTo($message), $this->equalTo(false)); + + $this->logger->messages = $message; + $this->logger->dispatcher = $this->dispatcher; + $this->logger->flush(); + $this->assertEmpty($this->logger->messages); + } + + /** + * @covers yii\log\Logger::Flush() + */ + public function testFlushWithDispatcherAndDefinedParam() + { + $message = ['anything']; + $this->dispatcher->expects($this->once()) + ->method('dispatch')->with($this->equalTo($message), $this->equalTo(true)); + + $this->logger->messages = $message; + $this->logger->dispatcher = $this->dispatcher; + $this->logger->flush(true); + $this->assertEmpty($this->logger->messages); + } }