Commit 9df0118b authored by Taddeus Kroes's avatar Taddeus Kroes

Unit tested new Logger functions.

parent 121c77ff
...@@ -42,7 +42,7 @@ class Logger extends Base { ...@@ -42,7 +42,7 @@ class Logger extends Base {
function set_dump_format($format) { function set_dump_format($format) {
if( !in_array($format, self::$allowed_dump_formats) ) if( !in_array($format, self::$allowed_dump_formats) )
throw new InvalidArgumentException(sprintf('', $format)); throw new \InvalidArgumentException(sprintf('', $format));
$this->dump_format = $format; $this->dump_format = $format;
} }
......
...@@ -5,6 +5,7 @@ use WebBasics\Logger; ...@@ -5,6 +5,7 @@ use WebBasics\Logger;
define('NAME', 'Testlogger'); define('NAME', 'Testlogger');
define('FORMAT', '%(level): %(message)'); define('FORMAT', '%(level): %(message)');
define('LOGDIR', 'build/logs/');
define('LOGFILE', 'build/temp.log'); define('LOGFILE', 'build/temp.log');
class LoggerTest extends PHPUnit_Extensions_OutputTestCase { class LoggerTest extends PHPUnit_Extensions_OutputTestCase {
...@@ -12,12 +13,38 @@ class LoggerTest extends PHPUnit_Extensions_OutputTestCase { ...@@ -12,12 +13,38 @@ class LoggerTest extends PHPUnit_Extensions_OutputTestCase {
$this->logger = new Logger(); $this->logger = new Logger();
$this->logger->set_property('name', NAME); $this->logger->set_property('name', NAME);
$this->logger->set_format(FORMAT); $this->logger->set_format(FORMAT);
is_dir('build') || mkdir('build');
} }
function assert_dumps($expected) { function assert_dumps($expected) {
$this->assertEquals($this->logger->dumps(), $expected); $this->assertEquals($this->logger->dumps(), $expected);
} }
function test_set_directory() {
$this->logger->set_directory('logs');
$this->assertAttributeEquals('logs/', 'log_directory', $this->logger);
$this->logger->set_directory('logs/');
$this->assertAttributeEquals('logs/', 'log_directory', $this->logger);
}
function test_set_format() {
$this->logger->set_format('foo');
$this->assertAttributeEquals('foo', 'format', $this->logger);
}
function test_set_dump_format_success() {
$this->logger->set_dump_format('html');
$this->assertAttributeEquals('html', 'dump_format', $this->logger);
}
/**
* @expectedException InvalidArgumentException
*/
function test_set_dump_format_failure() {
$this->logger->set_dump_format('foo');
}
function test_get_format() { function test_get_format() {
$this->assertEquals($this->logger->get_format(), FORMAT); $this->assertEquals($this->logger->get_format(), FORMAT);
} }
...@@ -99,15 +126,20 @@ class LoggerTest extends PHPUnit_Extensions_OutputTestCase { ...@@ -99,15 +126,20 @@ class LoggerTest extends PHPUnit_Extensions_OutputTestCase {
/** /**
* @depends test_process_level * @depends test_process_level
*/ */
function test_dump() { function test_dump_plain() {
$this->logger->warning('test message'); $this->logger->warning('test message');
$this->expectOutputString('WARNING: test message'); $this->expectOutputString('WARNING: test message');
$this->logger->dump(); $this->logger->dump();
} }
function test_handle_exception() { /**
$this->logger->handle_exception(new Exception('test message')); * @depends test_process_level
$this->assertNotEquals($this->logger->dumps(), ''); */
function test_dump_html() {
$this->logger->warning('test message');
$this->logger->set_dump_format('html');
$this->expectOutputString('<strong>Log:</strong><br /><pre>WARNING: test message</pre>');
$this->logger->dump();
} }
function test_save() { function test_save() {
...@@ -117,7 +149,47 @@ class LoggerTest extends PHPUnit_Extensions_OutputTestCase { ...@@ -117,7 +149,47 @@ class LoggerTest extends PHPUnit_Extensions_OutputTestCase {
$this->logger->warning('another test message'); $this->logger->warning('another test message');
$this->logger->save(LOGFILE); $this->logger->save(LOGFILE);
$this->assertStringEqualsFile(LOGFILE, "WARNING: test message\nWARNING: another test message"); $this->assertStringEqualsFile(LOGFILE, "WARNING: test message\nWARNING: another test message");
file_exists(LOGFILE) && unlink(LOGFILE); unlink(LOGFILE);
}
function find_logfile() {
$files = scandir(LOGDIR);
return $files[2];
}
/**
* @depends test_save
*/
function test_dump_file_regular() {
$this->logger->set_directory(LOGDIR);
$this->logger->set_dump_format('file');
$this->logger->warning('test message');
$this->logger->dump();
$filename = $this->find_logfile();
$this->assertStringEqualsFile(LOGDIR . $filename, 'WARNING: test message');
unlink(LOGDIR . $filename);
$this->assertRegExp('/^log_\d{2}-\d{2}-\d{4}_\d{2}-\d{2}-\d{2}.log$/', $filename);
}
/**
* @depends test_dump_file_regular
*/
function test_dump_file_prefix() {
$this->logger->set_directory(LOGDIR);
$this->logger->set_dump_format('file');
$this->logger->warning('test message');
$this->logger->dump('error');
$filename = $this->find_logfile();
$this->assertStringEqualsFile(LOGDIR . $filename, 'WARNING: test message');
unlink(LOGDIR . $filename);
$this->assertRegExp('/^error_\d{2}-\d{2}-\d{4}_\d{2}-\d{2}-\d{2}.log$/', $filename);
}
function test_handle_exception() {
$this->logger->handle_exception(new Exception('test message'));
$this->assertNotEquals($this->logger->dumps(), '');
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment