Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Logger tests

  • Loading branch information...
commit 477d2cc998878a03bb42a875b4dcf6c5df42c9e8 1 parent 7206604
@TomNomNom authored
View
16 Library/Logger.php
@@ -51,42 +51,42 @@ protected function getLevelString($level){
public function emerge($msg){
if ($this->level < self::LEVEL_EMERGE) return;
- $this->log($msg.PHP_EOL, self::LEVEL_EMERGE);
+ $this->log($msg, self::LEVEL_EMERGE);
}
public function alert($msg){
if ($this->level < self::LEVEL_ALERT) return;
- $this->log($msg.PHP_EOL, self::LEVEL_ALERT);
+ $this->log($msg, self::LEVEL_ALERT);
}
public function crit($msg){
if ($this->level < self::LEVEL_CRIT) return;
- $this->log($msg.PHP_EOL, self::LEVEL_CRIT);
+ $this->log($msg, self::LEVEL_CRIT);
}
public function err($msg){
if ($this->level < self::LEVEL_ERR) return;
- $this->log($msg.PHP_EOL, self::LEVEL_ERR);
+ $this->log($msg, self::LEVEL_ERR);
}
public function warn($msg){
if ($this->level < self::LEVEL_WARN) return;
- $this->log($msg.PHP_EOL, self::LEVEL_WARN);
+ $this->log($msg, self::LEVEL_WARN);
}
public function notice($msg){
if ($this->level < self::LEVEL_NOTICE) return;
- $this->log($msg.PHP_EOL, self::LEVEL_NOTICE);
+ $this->log($msg, self::LEVEL_NOTICE);
}
public function info($msg){
if ($this->level < self::LEVEL_INFO) return;
- $this->log($msg.PHP_EOL, self::LEVEL_INFO);
+ $this->log($msg, self::LEVEL_INFO);
}
public function debug($msg){
if ($this->level < self::LEVEL_DEBUG) return;
- $this->log($msg.PHP_EOL, self::LEVEL_DEBUG);
+ $this->log($msg, self::LEVEL_DEBUG);
}
}
View
2  Library/Logger/File.php
@@ -19,7 +19,7 @@ protected function log($msg, $level){
$level = $this->getLevelString($level);
$date = $this->getDateString(time());
- $line = "[{$date}] {$level}: {$msg}";
+ $line = "[{$date}] {$level}: {$msg}\n";
fputs($this->handle, $line, strlen($line));
}
View
140 Library/Test/LoggerTest.php
@@ -0,0 +1,140 @@
+<?php
+namespace Test;
+
+// We're testing the functionality of the abstract logger; so we just test the mock directly
+class LoggerTest extends \PHPUnit_Framework_TestCase {
+ public function testLevelEmerge(){
+ $l = new Mock\Logger("EMERGE");
+
+ $l->emerge('one');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_EMERGE);
+
+ // Check next level up doesn't log
+ $l->alert('two');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_EMERGE);
+ }
+
+ public function testLevelAlert(){
+ $l = new Mock\Logger("ALERT");
+
+ $l->alert('one');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_ALERT);
+
+ // Check next level up doesn't log
+ $l->crit('two');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_ALERT);
+
+ // Check one level below does log
+ $l->emerge('three');
+ $this->assertEquals(trim($l->getLastMsg()), 'three');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_EMERGE);
+ }
+
+ public function testLevelCrit(){
+ $l = new Mock\Logger("CRIT");
+
+ $l->crit('one');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_CRIT);
+
+ // Check next level up doesn't log
+ $l->err('two');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_CRIT);
+
+ // Check one level below does log
+ $l->alert('three');
+ $this->assertEquals(trim($l->getLastMsg()), 'three');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_ALERT);
+ }
+
+ public function testLevelErr(){
+ $l = new Mock\Logger("ERR");
+
+ $l->err('one');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_ERR);
+
+ // Check next level up doesn't log
+ $l->warn('two');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_ERR);
+
+ // Check one level below does log
+ $l->crit('three');
+ $this->assertEquals(trim($l->getLastMsg()), 'three');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_CRIT);
+ }
+
+ public function testLevelWarn(){
+ $l = new Mock\Logger("WARN");
+
+ $l->warn('one');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_WARN);
+
+ // Check next level up doesn't log
+ $l->notice('two');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_WARN);
+
+ // Check one level below does log
+ $l->err('three');
+ $this->assertEquals(trim($l->getLastMsg()), 'three');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_ERR);
+ }
+
+ public function testLevelNotice(){
+ $l = new Mock\Logger("NOTICE");
+
+ $l->notice('one');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_NOTICE);
+
+ // Check next level up doesn't log
+ $l->info('two');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_NOTICE);
+
+ // Check one level below does log
+ $l->warn('three');
+ $this->assertEquals(trim($l->getLastMsg()), 'three');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_WARN);
+ }
+
+ public function testLevelInfo(){
+ $l = new Mock\Logger("INFO");
+
+ $l->info('one');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_INFO);
+
+ // Check next level up doesn't log
+ $l->debug('two');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_INFO);
+
+ // Check one level below does log
+ $l->notice('three');
+ $this->assertEquals(trim($l->getLastMsg()), 'three');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_NOTICE);
+ }
+
+ public function testLevelDebug(){
+ $l = new Mock\Logger("DEBUG");
+
+ $l->debug('one');
+ $this->assertEquals(trim($l->getLastMsg()), 'one');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_DEBUG);
+
+ // Check one level below does log
+ $l->info('two');
+ $this->assertEquals(trim($l->getLastMsg()), 'two');
+ $this->assertEquals(trim($l->getLastLevel()), \Logger::LEVEL_INFO);
+ }
+}
+
View
20 Library/Test/Mock/Logger.php
@@ -0,0 +1,20 @@
+<?php
+namespace Test\Mock;
+
+class Logger extends \Logger {
+ protected $lastMsg = '';
+ protected $lastLevel = null;
+
+ public function log($msg, $level){
+ $this->lastMsg = $msg;
+ $this->lastLevel = $level;
+ }
+
+ public function getLastMsg(){
+ return $this->lastMsg;
+ }
+ public function getLastLevel(){
+ return $this->lastLevel;
+ }
+}
+
View
1  Test/CompatibilityTest.php
@@ -1,5 +1,4 @@
<?php
-namespace Test;
class CompatibilityTest extends \PHPUnit_Framework_TestCase {
public function testCurl(){
View
5 phpunit.xml
@@ -1,7 +1,10 @@
<phpunit bootstrap="Include/Init.php">
<testsuites>
- <testsuite name="tvyv">
+ <testsuite name="Core">
<directory>Test</directory>
</testsuite>
+ <testsuite name="Library">
+ <directory>Library/Test</directory>
+ </testsuite>
</testsuites>
</phpunit>
Please sign in to comment.
Something went wrong with that request. Please try again.