From 4f1d81e6fde4f3f8d9876ad176ba8acd6c058268 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Thu, 28 Nov 2013 21:04:27 +0000 Subject: [PATCH 1/5] update loggerConfig constructor to take array by default to make unit testing simpler. --- PHPCI/Helper/LoggerConfig.php | 29 +++++++++++++++++++++-------- console | 2 +- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/PHPCI/Helper/LoggerConfig.php b/PHPCI/Helper/LoggerConfig.php index 014805f2..1e130e1a 100644 --- a/PHPCI/Helper/LoggerConfig.php +++ b/PHPCI/Helper/LoggerConfig.php @@ -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; } /** diff --git a/console b/console index 3358dce7..baa0e13a 100644 --- a/console +++ b/console @@ -20,7 +20,7 @@ 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(); From f1052443ab18ab6a0772a85decd7398e1d234cb0 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Thu, 28 Nov 2013 21:17:01 +0000 Subject: [PATCH 2/5] add basic type checking for LoggerConfig::GetFor --- Tests/PHPCI/Helper/LoggerConfigTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Tests/PHPCI/Helper/LoggerConfigTest.php diff --git a/Tests/PHPCI/Helper/LoggerConfigTest.php b/Tests/PHPCI/Helper/LoggerConfigTest.php new file mode 100644 index 00000000..a18f504a --- /dev/null +++ b/Tests/PHPCI/Helper/LoggerConfigTest.php @@ -0,0 +1,19 @@ +getFor("something"); + $this->assertInstanceOf('\Psr\Log\LoggerInterface', $logger); + } + + public function testGetFor_ReturnsMonologInstance() + { + $config = new \PHPCI\Helper\LoggerConfig(array()); + $logger = $config->getFor("something"); + $this->assertInstanceOf('\Monolog\Logger', $logger); + } +} + \ No newline at end of file From c8dc8a67b7e70a283f6196d9b53deb0fb85ff465 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Thu, 28 Nov 2013 21:18:11 +0000 Subject: [PATCH 3/5] Fix casing in LoggerConfig. --- PHPCI/Helper/LoggerConfig.php | 2 +- console | 8 ++++---- phpci.yml | 20 ++++++++++---------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/PHPCI/Helper/LoggerConfig.php b/PHPCI/Helper/LoggerConfig.php index 1e130e1a..de1b2a67 100644 --- a/PHPCI/Helper/LoggerConfig.php +++ b/PHPCI/Helper/LoggerConfig.php @@ -46,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); diff --git a/console b/console index baa0e13a..f855f5ab 100644 --- a/console +++ b/console @@ -24,11 +24,11 @@ $loggerConfig = \PHPCI\Helper\LoggerConfig::newFromFile(__DIR__ . "/loggerconfig $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(); diff --git a/phpci.yml b/phpci.yml index 42b2dba3..52b13952 100644 --- a/phpci.yml +++ b/phpci.yml @@ -2,7 +2,6 @@ build_settings: verbose: false ignore: - "vendor" - - "Tests" - "PHPCI/Command" # PHPMD complains about un-used parameters, but they are required. - "public/install.php" # PHPCS really doesn't like PHP mixed with HTML (and so it shouldn't) irc: @@ -13,14 +12,15 @@ build_settings: test: php_mess_detector: - php_code_sniffer: - standard: "PSR2" - php_loc: +phpunit: + directory: + "Tests/" -success: - irc: - message: "Build Success. %PROJECT_TITLE% - %COMMIT% - %BUILD_URI%" -failure: - irc: - message: "Build Failed. %PROJECT_TITLE% - %COMMIT% - %BUILD_URI%" \ No newline at end of file +#success: +# irc: +# message: "Build Success. %PROJECT_TITLE% - %COMMIT% - %BUILD_URI%" + +#failure: +# irc: +# message: "Build Failed. %PROJECT_TITLE% - %COMMIT% - %BUILD_URI%" \ No newline at end of file From ee9871e4a8e8b10bf4abee1878f514eb2db5cec8 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Thu, 28 Nov 2013 21:36:56 +0000 Subject: [PATCH 4/5] add tests to LoggerConfig for handler attaching. --- Tests/PHPCI/Helper/LoggerConfigTest.php | 60 ++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/Tests/PHPCI/Helper/LoggerConfigTest.php b/Tests/PHPCI/Helper/LoggerConfigTest.php index a18f504a..95ea3d62 100644 --- a/Tests/PHPCI/Helper/LoggerConfigTest.php +++ b/Tests/PHPCI/Helper/LoggerConfigTest.php @@ -1,19 +1,75 @@ getFor("something"); $this->assertInstanceOf('\Psr\Log\LoggerInterface', $logger); } public function testGetFor_ReturnsMonologInstance() { - $config = new \PHPCI\Helper\LoggerConfig(array()); + $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); + } } \ No newline at end of file From 4b16077428e8e67f3af720feac5406694076a4dd Mon Sep 17 00:00:00 2001 From: meadsteve Date: Fri, 29 Nov 2013 10:12:22 +0000 Subject: [PATCH 5/5] revert accidental change to phpci.yml --- phpci.yml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/phpci.yml b/phpci.yml index 52b13952..42b2dba3 100644 --- a/phpci.yml +++ b/phpci.yml @@ -2,6 +2,7 @@ build_settings: verbose: false ignore: - "vendor" + - "Tests" - "PHPCI/Command" # PHPMD complains about un-used parameters, but they are required. - "public/install.php" # PHPCS really doesn't like PHP mixed with HTML (and so it shouldn't) irc: @@ -12,15 +13,14 @@ build_settings: test: php_mess_detector: -phpunit: - directory: - "Tests/" + php_code_sniffer: + standard: "PSR2" + php_loc: +success: + irc: + message: "Build Success. %PROJECT_TITLE% - %COMMIT% - %BUILD_URI%" -#success: -# irc: -# message: "Build Success. %PROJECT_TITLE% - %COMMIT% - %BUILD_URI%" - -#failure: -# irc: -# message: "Build Failed. %PROJECT_TITLE% - %COMMIT% - %BUILD_URI%" \ No newline at end of file +failure: + irc: + message: "Build Failed. %PROJECT_TITLE% - %COMMIT% - %BUILD_URI%" \ No newline at end of file