Fix LoggerTest

fixes #5561
This commit is contained in:
Marius Hein 2014-02-11 15:01:55 +01:00
parent 9b45898ed9
commit b78eb52732

View File

@ -29,188 +29,168 @@
namespace Tests\Icinga\Application; namespace Tests\Icinga\Application;
require_once 'Zend/Log.php'; // @codingStandardsIgnoreStart
require_once 'Zend/Config.php'; require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCase.php');
require_once 'Zend/Log/Writer/Mock.php'; // @codingStandardsIgnoreEnd
require_once 'Zend/Log/Writer/Null.php';
require_once 'Zend/Log/Filter/Priority.php';
require_once realpath(__DIR__ . '/../../../../../library/Icinga/Application/Logger.php'); use \Zend_Config;
require_once realpath(__DIR__ . '/../../../../../library/Icinga/Exception/ConfigurationError.php'); use Icinga\Application\Logger;
use Icinga\Test\BaseTestCase;
use \Icinga\Application\Logger;
/** /**
* Test class for Logger * Test class for Logger
* *
* @backupStaticAttributes enabled * @backupStaticAttributes enabled
**/ **/
class LoggerTest extends \PHPUnit_Framework_TestCase class LoggerTest extends BaseTestCase
{ {
private $timeZone; private $tempDir;
protected function setUp() private $logTarget;
private $debugTarget;
public function setUp()
{ {
date_default_timezone_set('GMT'); $this->tempDir = tempnam(sys_get_temp_dir(), 'icingaweb-log');
unlink($this->tempDir); // tempnam create the file automatically
if (!is_dir($this->tempDir)) {
mkdir($this->tempDir, 0755);
}
$this->debugTarget = $this->tempDir . '/debug.log';
$this->logTarget = $this->tempDir . '/application.log';
$loggingConfigurationArray = array(
'enable' => 1,
'type' => 'stream',
'verbose' => 1,
'target' => $this->logTarget,
'debug' => array(
'enable' => 1,
'type' => 'stream',
'target' => $this->debugTarget
)
);
$loggingConfiguration = new Zend_Config($loggingConfigurationArray);
Logger::reset(); Logger::reset();
} Logger::create($loggingConfiguration);
public function testOverwrite()
{
$cfg1 = new \Zend_Config(
array(
'debug' => array('enable' => 0),
'type' => 'mock',
'target' => 'target2'
)
);
$cfg2 = new \Zend_Config(
array(
'debug' => array(
'enable' => 1,
'type'=>'mock',
'target'=>'target3'
),
'type' => 'mock',
'target' => 'target4'
)
);
$logger = new Logger($cfg1);
$writers = $logger->getWriters();
$this->assertEquals(1, count($writers));
$logger = new Logger($cfg1);
$writers2 = $logger->getWriters();
$this->assertEquals(1, count($writers));
$this->assertNotEquals($writers[0], $writers2[0]);
$logger = new Logger($cfg2);
$writers2 = $logger->getWriters();
$this->assertEquals(2, count($writers2));
}
public function testFormatMessage()
{
$message = Logger::formatMessage(array('Testmessage'));
$this->assertEquals('Testmessage', $message);
$message = Logger::formatMessage(array('Testmessage %s %s', 'test1', 'test2'));
$this->assertEquals('Testmessage test1 test2', $message);
$message = Logger::formatMessage(array('Testmessage %s', array('test1', 'test2')));
$this->assertEquals('Testmessage '.json_encode(array('test1', 'test2')), $message);
}
public function testLoggingOutput()
{
$cfg1 = new \Zend_Config(
array(
'debug' => array('enable' => 0),
'type' => 'mock',
'target' => 'target2'
)
);
$logger = Logger::create($cfg1);
$writers = $logger->getWriters();
$logger->warn('Warning');
$logger->error('Error');
$logger->info('Info');
$logger->debug('Debug');
$writer = $writers[0];
$this->assertEquals(2, count($writer->events));
$this->assertEquals($writer->events[0]['message'], 'Warning');
$this->assertEquals($writer->events[1]['message'], 'Error');
} }
public function testLogQueuing() public function tearDown()
{ {
$cfg1 = new \Zend_Config( if (file_exists($this->debugTarget)) {
array( unlink($this->debugTarget);
'debug' => array('enable' => 0), }
'type' => 'mock',
'target' => 'target2'
)
);
Logger::warn('Warning'); if (file_exists($this->logTarget)) {
Logger::error('Error'); unlink($this->logTarget);
Logger::info('Info'); }
Logger::debug('Debug');
$logger = Logger::create($cfg1); rmdir($this->tempDir);
$writers = $logger->getWriters();
$writer = $writers[0];
$this->assertEquals(2, count($writer->events));
$this->assertEquals($writer->events[0]['message'], 'Warning');
$this->assertEquals($writer->events[1]['message'], 'Error');
} }
public function testDebugLogErrorCatching() private function getLogData()
{ {
$cfg1 = new \Zend_Config( return array(
array( explode(PHP_EOL, file_get_contents($this->logTarget)),
'debug' => array( explode(PHP_EOL, file_get_contents($this->debugTarget))
'enable' => 1,
'type' => 'Invalid',
'target' => '...'
),
'type' => 'mock',
'target' => 'target2'
)
);
$logger = Logger::create($cfg1);
$writers = $logger->getWriters();
$this->assertEquals(1, count($writers));
$this->assertEquals(1, count($writers[0]->events));
$this->assertEquals(
'Could not add log writer of type "Invalid". Type does not exist.',
$writers[0]->events[0]['message']
); );
} }
public function testNotLoggedMessagesQueue() /**
* Test error messages
*/
public function testLoggingErrorMessages()
{ {
$cfg1 = new \Zend_Config( Logger::error('test-error-1');
array( Logger::error('test-error-2');
'debug' => array(
'enable' => 0,
'type' => 'Invalid',
'target' => '...'
),
'type' => 'invalid',
'target' => 'target2'
)
);
$logger = Logger::create($cfg1); $this->assertFileExists($this->logTarget);
$this->assertFileExists($this->debugTarget);
list($main, $debug) = $this->getLogData();
$this->assertCount(3, $main);
$this->assertCount(3, $debug);
$this->assertContains(' ERR (3): test-error-1', $main[0]);
$this->assertContains(' ERR (3): test-error-2', $main[1]);
$this->assertContains(' ERR (3): test-error-1', $debug[0]);
$this->assertContains(' ERR (3): test-error-2', $debug[1]);
}
/**
* Test debug log and difference between error and debug messages
*/
public function testLoggingDebugMessages()
{
Logger::debug('test-debug-1');
Logger::error('test-error-1');
Logger::debug('test-debug-2');
$this->assertFileExists($this->logTarget);
$this->assertFileExists($this->debugTarget);
list($main, $debug) = $this->getLogData();
$this->assertCount(2, $main);
$this->assertCount(4, $debug);
$this->assertContains(' ERR (3): test-error-1', $main[0]);
$this->assertContains(' DEBUG (7): test-debug-1', $debug[0]);
$this->assertContains(' ERR (3): test-error-1', $debug[1]);
$this->assertContains(' DEBUG (7): test-debug-2', $debug[2]);
}
public function testLoggingQueueIfNoWriterAvailable()
{
Logger::reset();
Logger::error('test-error-1');
Logger::debug('test-debug-1');
Logger::error('test-error-2');
list($main, $debug) = $this->getLogData();
$this->assertCount(1, $main);
$this->assertCount(1, $debug);
$this->assertTrue(Logger::hasErrorsOccurred()); $this->assertTrue(Logger::hasErrorsOccurred());
$queue = Logger::getQueue(); $queue = Logger::getQueue();
$this->assertCount(2, $queue); $this->assertCount(3, $queue);
$this->assertSame( $this->assertEquals(
'Could not add log writer of type "Invalid". Type does not exist.', array(
$queue[0][0], 'test-error-1',
'Log message of an invalid writer' 3
),
$queue[0]
); );
$this->assertSame(0, $queue[0][1], 'Log level "fatal"'); $this->assertEquals(
array(
$this->assertSame( 'test-debug-1',
'Could not flush logs to output. An exception was thrown: No writers were added', 7
$queue[1][0], ),
'Log message that no writer was added to logger' $queue[1]
); );
$this->assertSame(0, $queue[1][1], 'Log level "fatal"'); $this->assertEquals(
array(
'test-error-2',
3
),
$queue[2]
);
} }
} }