Browse Source

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
tags/2.0.10
Dmitriy Makarov 8 years ago committed by Alexander Makarov
parent
commit
dc4a4b456c
  1. 52
      tests/framework/log/LoggerTest.php

52
tests/framework/log/LoggerTest.php

@ -18,9 +18,15 @@ class LoggerTest extends TestCase
*/ */
protected $logger; protected $logger;
/**
* @var Dispatcher
*/
protected $dispatcher;
protected function setUp() protected function setUp()
{ {
$this->logger = new Logger(); $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('application', $this->logger->messages[0][2]);
$this->assertEquals([ $this->assertEquals([
'file' => __FILE__, 'file' => __FILE__,
'line' => 52, 'line' => 58,
'function' => 'log', 'function' => 'log',
'class' => get_class($this->logger), 'class' => get_class($this->logger),
'type' => '->' 'type' => '->'
@ -74,4 +80,48 @@ class LoggerTest extends TestCase
$logger->expects($this->exactly(1))->method('flush'); $logger->expects($this->exactly(1))->method('flush');
$logger->log('test1', Logger::LEVEL_INFO); $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);
}
} }

Loading…
Cancel
Save