Merge pull request #227 from meadsteve/unit-testing

Unit testing - LoggerConfig
This commit is contained in:
Steve B 2013-11-29 02:17:02 -08:00
commit 4a9103d6eb
3 changed files with 102 additions and 14 deletions

View file

@ -13,18 +13,31 @@ class LoggerConfig {
private $config;
/**
* The file specified is expected to return an array. Where each key
* is the name of a logger. The value of each key should be an array or
* a function that returns an array of LogHandlers.
* @param $logConfigFilePath
* The filepath is expected to return an array which will be
* passed to the normal constructor.
*
* @param string $filePath
* @return LoggerConfig
*/
function __construct($logConfigFilePath) {
if (file_exists($logConfigFilePath)) {
$this->config = require_once($logConfigFilePath);
public static function newFromFile($filePath)
{
if (file_exists($filePath)) {
$configArray = require($filePath);
}
else {
$this->config = array();
$configArray = array();
}
return new self($configArray);
}
/**
* Each key of the array is the name of a logger. The value of
* each key should be an array or a function that returns an
* array of LogHandlers.
* @param array $configArray
*/
function __construct(array $configArray = array()) {
$this->config = $configArray;
}
/**
@ -33,7 +46,7 @@ class LoggerConfig {
* @param $name
* @return Logger
*/
public function GetFor($name) {
public function getFor($name) {
$handlers = $this->getHandlers(self::KEY_AlwaysLoaded);
$handlers = array_merge($handlers, $this->getHandlers($name));
return new Logger($name, $handlers);

View file

@ -0,0 +1,75 @@
<?php
use \PHPCI\Helper\LoggerConfig;
class LoggerConfigTest extends PHPUnit_Framework_TestCase
{
public function testGetFor_ReturnsPSRLogger()
{
$config = new LoggerConfig(array());
$logger = $config->getFor("something");
$this->assertInstanceOf('\Psr\Log\LoggerInterface', $logger);
}
public function testGetFor_ReturnsMonologInstance()
{
$config = new LoggerConfig(array());
$logger = $config->getFor("something");
$this->assertInstanceOf('\Monolog\Logger', $logger);
}
public function testGetFor_AttachesAlwaysPresentHandlers()
{
$expectedHandler = new Monolog\Handler\NullHandler();
$config = new LoggerConfig(array(
LoggerConfig::KEY_AlwaysLoaded => function() use ($expectedHandler) {
return array($expectedHandler);
}
));
/** @var \Monolog\Logger $logger */
$logger = $config->getFor("something");
$actualHandler = $logger->popHandler();
$this->assertEquals($expectedHandler, $actualHandler);
}
public function testGetFor_AttachesSpecificHandlers()
{
$expectedHandler = new Monolog\Handler\NullHandler();
$config = new LoggerConfig(array(
"Specific" => function() use ($expectedHandler) {
return array($expectedHandler);
}
));
/** @var \Monolog\Logger $logger */
$logger = $config->getFor("Specific");
$actualHandler = $logger->popHandler();
$this->assertSame($expectedHandler, $actualHandler);
}
public function testGetFor_IgnoresAlternativeHandlers()
{
$expectedHandler = new Monolog\Handler\NullHandler();
$alternativeHandler = new Monolog\Handler\NullHandler();
$config = new LoggerConfig(array(
"Specific" => function() use ($expectedHandler) {
return array($expectedHandler);
},
"Other" => function() use ($alternativeHandler) {
return array($alternativeHandler);
}
));
/** @var \Monolog\Logger $logger */
$logger = $config->getFor("Specific");
$actualHandler = $logger->popHandler();
$this->assertSame($expectedHandler, $actualHandler);
$this->assertNotSame($alternativeHandler, $actualHandler);
}
}

10
console
View file

@ -20,15 +20,15 @@ use PHPCI\Command\DaemonCommand;
use PHPCI\Command\PollCommand;
use Symfony\Component\Console\Application;
$loggerConfig = new \PHPCI\Helper\LoggerConfig(__DIR__ . "/loggerconfig.php");
$loggerConfig = \PHPCI\Helper\LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php");
$application = new Application();
$application->add(new RunCommand($loggerConfig->GetFor('RunCommand')));
$application->add(new RunCommand($loggerConfig->getFor('RunCommand')));
$application->add(new InstallCommand);
$application->add(new UpdateCommand($loggerConfig->GetFor('UpdateCommand')));
$application->add(new UpdateCommand($loggerConfig->getFor('UpdateCommand')));
$application->add(new GenerateCommand);
$application->add(new DaemonCommand($loggerConfig->GetFor('DaemonCommand')));
$application->add(new PollCommand($loggerConfig->GetFor('PollCommand')));
$application->add(new DaemonCommand($loggerConfig->getFor('DaemonCommand')));
$application->add(new PollCommand($loggerConfig->getFor('PollCommand')));
$application->run();