Add test for Icinga/Logger/Writer/StreamWriter

refs #6011
This commit is contained in:
Johannes Meyer 2014-04-22 14:31:57 +02:00
parent 45d7864198
commit 54a5e996bb
6 changed files with 58 additions and 60 deletions

View File

@ -4,7 +4,7 @@
namespace Icinga\Logger;
use \Zend_Config;
use Zend_Config;
/**
* Abstract class for writers that write messages to a log

View File

@ -4,8 +4,8 @@
namespace Icinga\Logger;
use \Exception;
use \Zend_Config;
use Exception;
use Zend_Config;
use Icinga\Exception\ConfigurationError;
/**

View File

@ -4,7 +4,8 @@
namespace Icinga\Logger\Writer;
use \Zend_Config;
use Exception;
use Zend_Config;
use Icinga\Logger\Logger;
use Icinga\Logger\LogWriter;
use Icinga\Application\Config;
@ -87,11 +88,17 @@ class StreamWriter extends LogWriter
* Write a message to the stream
*
* @param string $text The message to write
*
* @throws Exception In case write acess to the stream failed
*/
protected function write($text)
{
$fd = fopen($this->stream, 'a');
fwrite($fd, $text . PHP_EOL);
if ($fd === false || fwrite($fd, $text . PHP_EOL) === false) {
throw new Exception('Failed to write to log file "' . $this->stream . '"');
}
fclose($fd);
}
}

View File

@ -4,8 +4,8 @@
namespace Icinga\Logger\Writer;
use \Exception;
use \Zend_Config;
use Exception;
use Zend_Config;
use Icinga\Logger\Logger;
use Icinga\Logger\LogWriter;
use Icinga\Exception\ConfigurationError;

View File

@ -1,53 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Tests\Icinga\Application;
use \Zend_Config;
use Icinga\Logger\Logger;
use Icinga\Test\BaseTestCase;
class LoggerTest extends BaseTestCase
{
public function testLogfileCreation()
{
$target = tempnam(sys_get_temp_dir(), 'log');
unlink($target);
new Logger(
new Zend_Config(
array(
'enable' => true,
'level' => Logger::$ERROR,
'type' => 'stream',
'target' => $target
)
)
);
$this->assertFileExists($target, 'Logger did not create the log file');
unlink($target);
}
/**
* @depends testLogfileCreation
*/
public function testLoggingErrorMessages()
{
$target = tempnam(sys_get_temp_dir(), 'log');
unlink($target);
$logger = new Logger(
new Zend_Config(
array(
'enable' => true,
'level' => Logger::$ERROR,
'type' => 'stream',
'target' => $target
)
)
);
$logger->log('This is a test error', Logger::$ERROR);
$log = file_get_contents($target);
unlink($target);
$this->assertContains('This is a test error', $log, 'Log does not contain the error "This is a test error"');
}
}

View File

@ -0,0 +1,44 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Tests\Icinga\Logger\Writer;
use Zend_Config;
use Icinga\Logger\Logger;
use Icinga\Test\BaseTestCase;
use Icinga\Logger\Writer\StreamWriter;
class LoggerTest extends BaseTestCase
{
public function setUp()
{
parent::setUp();
$this->target = tempnam(sys_get_temp_dir(), 'log');
}
public function tearDown()
{
parent::tearDown();
unlink($this->target);
}
public function testWhetherStreamWriterCreatesMissingFiles()
{
new StreamWriter(new Zend_Config(array('target' => $this->target)));
$this->assertFileExists($this->target, 'StreamWriter does not create missing files on initialization');
}
/**
* @depends testWhetherStreamWriterCreatesMissingFiles
*/
public function testWhetherStreamWriterWritesMessages()
{
$writer = new StreamWriter(new Zend_Config(array('target' => $this->target)));
$writer->log(Logger::$ERROR, 'This is a test error');
$log = file_get_contents($this->target);
$this->assertContains('This is a test error', $log, 'StreamWriter does not write log messages');
}
}