Update logger to implement accepted PSR log spec

Add tests!
This commit is contained in:
Justin Hileman
2013-01-11 22:48:43 -08:00
parent 7522e2206d
commit 1515f5126b
7 changed files with 521 additions and 130 deletions
+78 -1
View File
@@ -27,11 +27,14 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
public function testConstructor()
{
$logger = new Mustache_Logger_StreamLogger(tmpfile());
$loader = new Mustache_Loader_StringLoader;
$partialsLoader = new Mustache_Loader_ArrayLoader;
$mustache = new Mustache_Engine(array(
'template_class_prefix' => '__whot__',
'cache' => self::$tempDir,
'cache' => self::$tempDir,
'cache_file_mode' => 777,
'logger' => $logger,
'loader' => $loader,
'partials_loader' => $partialsLoader,
'partials' => array(
@@ -45,6 +48,7 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
'charset' => 'ISO-8859-1',
));
$this->assertSame($logger, $mustache->getLogger());
$this->assertSame($loader, $mustache->getLoader());
$this->assertSame($partialsLoader, $mustache->getPartialsLoader());
$this->assertEquals('{{ foo }}', $partialsLoader->load('foo'));
@@ -85,12 +89,17 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
public function testSettingServices()
{
$logger = new Mustache_Logger_StreamLogger(tmpfile());
$loader = new Mustache_Loader_StringLoader;
$tokenizer = new Mustache_Tokenizer;
$parser = new Mustache_Parser;
$compiler = new Mustache_Compiler;
$mustache = new Mustache_Engine;
$this->assertNotSame($logger, $mustache->getLogger());
$mustache->setLogger($logger);
$this->assertSame($logger, $mustache->getLogger());
$this->assertNotSame($loader, $mustache->getLoader());
$mustache->setLoader($loader);
$this->assertSame($loader, $mustache->getLoader());
@@ -222,6 +231,74 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
$mustache->setHelpers('monkeymonkeymonkey');
}
/**
* @expectedException InvalidArgumentException
*/
public function testSetLoggerThrowsExceptions()
{
$mustache = new Mustache_Engine;
$mustache->setLogger(new StdClass);
}
public function testPartialLoadFailLogging()
{
$name = tempnam(sys_get_temp_dir(), 'mustache-test');
$mustache = new Mustache_Engine(array(
'logger' => new Mustache_Logger_StreamLogger($name, Mustache_Logger::WARNING),
'partials' => array(
'foo' => 'FOO',
'bar' => 'BAR',
),
));
$result = $mustache->render('{{> foo }}{{> bar }}{{> baz }}', array());
$this->assertEquals('FOOBAR', $result);
$this->assertContains('WARNING: Partial not found: "baz"', file_get_contents($name));
}
public function testCacheWarningLogging()
{
$name = tempnam(sys_get_temp_dir(), 'mustache-test');
$mustache = new Mustache_Engine(array(
'logger' => new Mustache_Logger_StreamLogger($name, Mustache_Logger::WARNING)
));
$result = $mustache->render('{{ foo }}', array('foo' => 'FOO'));
$this->assertEquals('FOO', $result);
$this->assertContains('WARNING: Template cache disabled, evaluating', file_get_contents($name));
}
public function testLoggingIsNotTooAnnoying()
{
$name = tempnam(sys_get_temp_dir(), 'mustache-test');
$mustache = new Mustache_Engine(array(
'logger' => new Mustache_Logger_StreamLogger($name)
));
$result = $mustache->render('{{ foo }}{{> bar }}', array('foo' => 'FOO'));
$this->assertEquals('FOO', $result);
$this->assertEmpty(file_get_contents($name));
}
public function testVerboseLoggingIsVerbose()
{
$name = tempnam(sys_get_temp_dir(), 'mustache-test');
$mustache = new Mustache_Engine(array(
'logger' => new Mustache_Logger_StreamLogger($name, Mustache_Logger::DEBUG)
));
$result = $mustache->render('{{ foo }}{{> bar }}', array('foo' => 'FOO'));
$this->assertEquals('FOO', $result);
$log = file_get_contents($name);
$this->assertContains("DEBUG: Instantiating template: ", $log);
$this->assertContains("WARNING: Partial not found: \"bar\"", $log);
}
private static function rmdir($path)
{
$path = rtrim($path, '/').'/';
@@ -0,0 +1,60 @@
<?php
/*
* This file is part of Mustache.php.
*
* (c) 2012 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @group unit
*/
class Mustache_Test_Logger_AbstractLoggerTest extends PHPUnit_Framework_TestCase
{
public function testEverything()
{
$logger = new Mustache_Test_Logger_TestLogger;
$logger->emergency('emergency message');
$logger->alert('alert message');
$logger->critical('critical message');
$logger->error('error message');
$logger->warning('warning message');
$logger->notice('notice message');
$logger->info('info message');
$logger->debug('debug message');
$expected = array(
array(Mustache_Logger::EMERGENCY, 'emergency message', array()),
array(Mustache_Logger::ALERT, 'alert message', array()),
array(Mustache_Logger::CRITICAL, 'critical message', array()),
array(Mustache_Logger::ERROR, 'error message', array()),
array(Mustache_Logger::WARNING, 'warning message', array()),
array(Mustache_Logger::NOTICE, 'notice message', array()),
array(Mustache_Logger::INFO, 'info message', array()),
array(Mustache_Logger::DEBUG, 'debug message', array()),
);
$this->assertEquals($expected, $logger->log);
}
}
class Mustache_Test_Logger_TestLogger extends Mustache_Logger_AbstractLogger
{
public $log = array();
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = array())
{
$this->log[] = array($level, $message, $context);
}
}
@@ -0,0 +1,206 @@
<?php
/*
* This file is part of Mustache.php.
*
* (c) 2012 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @group unit
*/
class Mustache_Test_Logger_StreamLoggerTest extends PHPUnit_Framework_TestCase
{
public function testAcceptsFilename()
{
$name = tempnam(sys_get_temp_dir(), 'mustache-test');
$logger = new Mustache_Logger_StreamLogger($name);
$logger->log(Mustache_Logger::CRITICAL, 'message');
$this->assertEquals("CRITICAL: message\n", file_get_contents($name));
}
public function testAcceptsResource()
{
$name = tempnam(sys_get_temp_dir(), 'mustache-test');
$file = fopen($name, 'a');
$logger = new Mustache_Logger_StreamLogger($file);
$logger->log(Mustache_Logger::CRITICAL, 'message');
$this->assertEquals("CRITICAL: message\n", file_get_contents($name));
}
/**
* @expectedException LogicException
*/
public function testPrematurelyClosedStreamThrowsException()
{
$stream = tmpfile();
$logger = new Mustache_Logger_StreamLogger($stream);
fclose($stream);
$logger->log(Mustache_Logger::CRITICAL, 'message');
}
/**
* @dataProvider getLevels
*/
public function testLoggingThresholds($logLevel, $level, $shouldLog)
{
$stream = tmpfile();
$logger = new Mustache_Logger_StreamLogger($stream, $logLevel);
$logger->log($level, "logged");
rewind($stream);
$result = fread($stream, 1024);
if ($shouldLog) {
$this->assertContains("logged", $result);
} else {
$this->assertEmpty($result);
}
}
public function getLevels()
{
// $logLevel, $level, $shouldLog
return array(
// identities
array(Mustache_Logger::EMERGENCY, Mustache_Logger::EMERGENCY, true),
array(Mustache_Logger::ALERT, Mustache_Logger::ALERT, true),
array(Mustache_Logger::CRITICAL, Mustache_Logger::CRITICAL, true),
array(Mustache_Logger::ERROR, Mustache_Logger::ERROR, true),
array(Mustache_Logger::WARNING, Mustache_Logger::WARNING, true),
array(Mustache_Logger::NOTICE, Mustache_Logger::NOTICE, true),
array(Mustache_Logger::INFO, Mustache_Logger::INFO, true),
array(Mustache_Logger::DEBUG, Mustache_Logger::DEBUG, true),
// one above
array(Mustache_Logger::ALERT, Mustache_Logger::EMERGENCY, true),
array(Mustache_Logger::CRITICAL, Mustache_Logger::ALERT, true),
array(Mustache_Logger::ERROR, Mustache_Logger::CRITICAL, true),
array(Mustache_Logger::WARNING, Mustache_Logger::ERROR, true),
array(Mustache_Logger::NOTICE, Mustache_Logger::WARNING, true),
array(Mustache_Logger::INFO, Mustache_Logger::NOTICE, true),
array(Mustache_Logger::DEBUG, Mustache_Logger::INFO, true),
// one below
array(Mustache_Logger::EMERGENCY, Mustache_Logger::ALERT, false),
array(Mustache_Logger::ALERT, Mustache_Logger::CRITICAL, false),
array(Mustache_Logger::CRITICAL, Mustache_Logger::ERROR, false),
array(Mustache_Logger::ERROR, Mustache_Logger::WARNING, false),
array(Mustache_Logger::WARNING, Mustache_Logger::NOTICE, false),
array(Mustache_Logger::NOTICE, Mustache_Logger::INFO, false),
array(Mustache_Logger::INFO, Mustache_Logger::DEBUG, false),
);
}
/**
* @dataProvider getLogMessages
*/
public function testLogging($level, $message, $context, $expected)
{
$stream = tmpfile();
$logger = new Mustache_Logger_StreamLogger($stream, Mustache_Logger::DEBUG);
$logger->log($level, $message, $context);
rewind($stream);
$result = fread($stream, 1024);
$this->assertEquals($expected, $result);
}
public function getLogMessages()
{
// $level, $message, $context, $expected
return array(
array(Mustache_Logger::DEBUG, 'debug message', array(), "DEBUG: debug message\n"),
array(Mustache_Logger::INFO, 'info message', array(), "INFO: info message\n"),
array(Mustache_Logger::NOTICE, 'notice message', array(), "NOTICE: notice message\n"),
array(Mustache_Logger::WARNING, 'warning message', array(), "WARNING: warning message\n"),
array(Mustache_Logger::ERROR, 'error message', array(), "ERROR: error message\n"),
array(Mustache_Logger::CRITICAL, 'critical message', array(), "CRITICAL: critical message\n"),
array(Mustache_Logger::ALERT, 'alert message', array(), "ALERT: alert message\n"),
array(Mustache_Logger::EMERGENCY, 'emergency message', array(), "EMERGENCY: emergency message\n"),
// with context
array(
Mustache_Logger::ERROR,
'error message',
array('name' => 'foo', 'number' => 42),
"ERROR: error message\n"
),
// with interpolation
array(
Mustache_Logger::ERROR,
'error {name}-{number}',
array('name' => 'foo', 'number' => 42),
"ERROR: error foo-42\n"
),
// with iterpolation false positive
array(
Mustache_Logger::ERROR,
'error {nothing}',
array('name' => 'foo', 'number' => 42),
"ERROR: error {nothing}\n"
),
// with interpolation injection
array(
Mustache_Logger::ERROR,
'{foo}',
array('foo' => '{bar}', 'bar' => 'FAIL'),
"ERROR: {bar}\n"
),
);
}
public function testChangeLoggingLevels()
{
$stream = tmpfile();
$logger = new Mustache_Logger_StreamLogger($stream);
$logger->setLevel(Mustache_Logger::ERROR);
$this->assertEquals(Mustache_Logger::ERROR, $logger->getLevel());
$logger->log(Mustache_Logger::WARNING, 'ignore this');
$logger->setLevel(Mustache_Logger::INFO);
$this->assertEquals(Mustache_Logger::INFO, $logger->getLevel());
$logger->log(Mustache_Logger::WARNING, 'log this');
$logger->setLevel(Mustache_Logger::CRITICAL);
$this->assertEquals(Mustache_Logger::CRITICAL, $logger->getLevel());
$logger->log(Mustache_Logger::ERROR, 'ignore this');
rewind($stream);
$result = fread($stream, 1024);
$this->assertEquals("WARNING: log this\n", $result);
}
/**
* @expectedException InvalidArgumentException
*/
public function testThrowsInvalidArgumentExceptionWhenSettingUnknownLevels()
{
$logger = new Mustache_Logger_StreamLogger(tmpfile());
$logger->setLevel('bacon');
}
/**
* @expectedException InvalidArgumentException
*/
public function testThrowsInvalidArgumentExceptionWhenLoggingUnknownLevels()
{
$logger = new Mustache_Logger_StreamLogger(tmpfile());
$logger->log('bacon', 'CODE BACON ERROR!');
}
}