* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team */ // {{{ICINGA_LICENSE_HEADER}}} namespace Tests\Icinga\Application; require_once 'Zend/Log.php'; require_once 'Zend/Config.php'; require_once 'Zend/Log/Writer/Mock.php'; require_once 'Zend/Log/Writer/Null.php'; require_once 'Zend/Log/Filter/Priority.php'; require_once realpath(__DIR__ . '/../../../../../library/Icinga/Application/Logger.php'); require_once realpath(__DIR__ . '/../../../../../library/Icinga/Exception/ConfigurationError.php'); use \Icinga\Application\Logger; /** * Test class for Logger * * @backupStaticAttributes enabled **/ class LoggerTest extends \PHPUnit_Framework_TestCase { private $timeZone; protected function setUp() { date_default_timezone_set('GMT'); Logger::reset(); } 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() { $cfg1 = new \Zend_Config( array( 'debug' => array('enable' => 0), 'type' => 'mock', 'target' => 'target2' ) ); Logger::warn('Warning'); Logger::error('Error'); Logger::info('Info'); Logger::debug('Debug'); $logger = Logger::create($cfg1); $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() { $cfg1 = new \Zend_Config( array( 'debug' => array( '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() { $cfg1 = new \Zend_Config( array( 'debug' => array( 'enable' => 0, 'type' => 'Invalid', 'target' => '...' ), 'type' => 'invalid', 'target' => 'target2' ) ); $logger = Logger::create($cfg1); $this->assertTrue(Logger::hasErrorsOccurred()); $queue = Logger::getQueue(); $this->assertCount(2, $queue); $this->assertSame( 'Could not add log writer of type "Invalid". Type does not exist.', $queue[0][0], 'Log message of an invalid writer' ); $this->assertSame(0, $queue[0][1], 'Log level "fatal"'); $this->assertSame( 'Could not flush logs to output. An exception was thrown: No writers were added', $queue[1][0], 'Log message that no writer was added to logger' ); $this->assertSame(0, $queue[1][1], 'Log level "fatal"'); } }