diff --git a/PHPCI/Helper/LoggerConfig.php b/PHPCI/Helper/LoggerConfig.php index 014805f2..de1b2a67 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; } /** @@ -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); diff --git a/PHPCI/Plugin/PhpUnit.php b/PHPCI/Plugin/PhpUnit.php index 9b905fb6..fde1b2fa 100755 --- a/PHPCI/Plugin/PhpUnit.php +++ b/PHPCI/Plugin/PhpUnit.php @@ -128,7 +128,7 @@ class PhpUnit implements \PHPCI\Plugin protected function runDir($dirPath) { if (is_array($dirPath)) { - return $this->recurseArg($dirPath, array($this, "runConfigFile")); + return $this->recurseArg($dirPath, array($this, "runDir")); } else { $curdir = getcwd(); chdir($this->phpci->buildPath); diff --git a/Tests/PHPCI/Helper/LoggerConfigTest.php b/Tests/PHPCI/Helper/LoggerConfigTest.php new file mode 100644 index 00000000..95ea3d62 --- /dev/null +++ b/Tests/PHPCI/Helper/LoggerConfigTest.php @@ -0,0 +1,75 @@ +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); + } +} + \ No newline at end of file diff --git a/Tests/PHPCI/Plugin/EmailTest.php b/Tests/PHPCI/Plugin/EmailTest.php index 1343068c..84618001 100644 --- a/Tests/PHPCI/Plugin/EmailTest.php +++ b/Tests/PHPCI/Plugin/EmailTest.php @@ -1,32 +1,33 @@ mockBuild = $this->getMock( - '\PHPCI\Model\Build', - array('getLog', 'getStatus'), - array(), - "mockBuild", - false - ); + '\PHPCI\Model\Build', + array('getLog', 'getStatus'), + array(), + "mockBuild", + false + ); $this->mockBuild->expects($this->any()) ->method('getLog') @@ -56,27 +57,33 @@ class EmailTest extends \PHPUnit_Framework_TestCase ->method('getStatus') ->will($this->returnValue(\PHPCI\Model\Build::STATUS_SUCCESS)); - $this->mockCiBuilder = $this->getMock( - '\PHPCI\Builder', - array('getSystemConfig', - 'getBuildProjectTitle', - 'getBuild', - 'log'), - array(), - "mockBuilder", - false - ); + $this->mockCiBuilder = $this->getMock( + '\PHPCI\Builder', + array( + 'getSystemConfig', + 'getBuildProjectTitle', + 'getBuild', + 'log' + ), + array(), + "mockBuilder_email", + false + ); $this->mockCiBuilder->buildPath = "/"; $this->mockCiBuilder->expects($this->any()) ->method('getSystemConfig') ->with('phpci') - ->will($this->returnValue(array( - 'email_settings' => array( - 'from_address' => "test-from-address@example.com" + ->will( + $this->returnValue( + array( + 'email_settings' => array( + 'from_address' => "test-from-address@example.com" + ) + ) ) - ))); + ); $this->mockCiBuilder->expects($this->any()) ->method('getBuildProjectTitle') ->will($this->returnValue('Test-Project')); @@ -85,53 +92,55 @@ class EmailTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue($this->mockBuild)); $this->mockMailer = $this->getMock( - '\Swift_Mailer', - array('send'), - array(), - "mockMailer", - false - ); + '\Swift_Mailer', + array('send'), + array(), + "mockMailer", + false + ); - $this->loadEmailPluginWithOptions(); - } + $this->loadEmailPluginWithOptions(); + } - protected function loadEmailPluginWithOptions($arrOptions = array()) - { - $this->testedEmailPlugin = new EmailPlugin( + protected function loadEmailPluginWithOptions($arrOptions = array()) + { + $this->testedEmailPlugin = new EmailPlugin( $this->mockCiBuilder, $this->mockBuild, $this->mockMailer, $arrOptions ); - } + } - /** - * @covers PHPUnit::execute - */ - public function testExecute_ReturnsFalseWithoutArgs() - { - $returnValue = $this->testedEmailPlugin->execute(); + /** + * @covers PHPUnit::execute + */ + public function testExecute_ReturnsFalseWithoutArgs() + { + $returnValue = $this->testedEmailPlugin->execute(); // As no addresses will have been mailed as non are configured. - $expectedReturn = false; + $expectedReturn = false; - $this->assertEquals($expectedReturn, $returnValue); - } + $this->assertEquals($expectedReturn, $returnValue); + } - /** - * @covers PHPUnit::execute - */ - public function testExecute_BuildsBasicEmails() - { - $this->loadEmailPluginWithOptions(array( - 'addresses' => array('test-receiver@example.com') - )); + /** + * @covers PHPUnit::execute + */ + public function testExecute_BuildsBasicEmails() + { + $this->loadEmailPluginWithOptions( + array( + 'addresses' => array('test-receiver@example.com') + ) + ); /** @var \Swift_Message $actualMail */ $actualMail = null; $this->catchMailPassedToSend($actualMail); - $returnValue = $this->testedEmailPlugin->execute(); - $expectedReturn = true; + $returnValue = $this->testedEmailPlugin->execute(); + $expectedReturn = true; $this->assertSystemMail( 'test-receiver@example.com', @@ -141,14 +150,14 @@ class EmailTest extends \PHPUnit_Framework_TestCase $actualMail ); - $this->assertEquals($expectedReturn, $returnValue); + $this->assertEquals($expectedReturn, $returnValue); - } + } /** - * @covers PHPUnit::sendEmail - */ + * @covers PHPUnit::sendEmail + */ public function testSendEmail_CallsMailerSend() { $this->mockMailer->expects($this->once()) @@ -157,8 +166,8 @@ class EmailTest extends \PHPUnit_Framework_TestCase } /** - * @covers PHPUnit::sendEmail - */ + * @covers PHPUnit::sendEmail + */ public function testSendEmail_BuildsAMessageObject() { $subject = "Test mail"; @@ -172,8 +181,8 @@ class EmailTest extends \PHPUnit_Framework_TestCase } /** - * @covers PHPUnit::sendEmail - */ + * @covers PHPUnit::sendEmail + */ public function testSendEmail_BuildsExpectedMessage() { $subject = "Test mail"; @@ -200,7 +209,7 @@ class EmailTest extends \PHPUnit_Framework_TestCase } /** - * @param \Swift_Message $actualMail passed by ref and populated with + * @param \Swift_Message $actualMail passed by ref and populated with * the message object the mock mailer * receives. */ @@ -227,14 +236,17 @@ class EmailTest extends \PHPUnit_Framework_TestCase * @param string $expectedSubject * @param \Swift_Message $actualMail */ - protected function assertSystemMail($expectedToAddress, - $expectedFromAddress, - $expectedBody, - $expectedSubject, - $actualMail) - { - if (! ($actualMail instanceof \Swift_Message)) { - $type = is_object($actualMail) ? get_class($actualMail) : gettype($actualMail); + protected function assertSystemMail( + $expectedToAddress, + $expectedFromAddress, + $expectedBody, + $expectedSubject, + $actualMail + ) { + if (!($actualMail instanceof \Swift_Message)) { + $type = is_object($actualMail) ? get_class($actualMail) : gettype( + $actualMail + ); throw new \Exception("Expected Swift_Message got " . $type); } $this->assertEquals( diff --git a/Tests/PHPCI/Plugin/PHPUnitTest.php b/Tests/PHPCI/Plugin/PHPUnitTest.php index 5809ac67..89b24694 100644 --- a/Tests/PHPCI/Plugin/PHPUnitTest.php +++ b/Tests/PHPCI/Plugin/PHPUnitTest.php @@ -1,71 +1,73 @@ mockCiBuilder = $this->getMock( - '\PHPCI\Builder', - array(), - array(), - "mockBuilder", - false - ); - $this->mockCiBuilder->buildPath = "/"; + /** + * @var \PHPUnit_Framework_MockObject_MockObject $mockCiBuilder + */ + protected $mockBuild; + + public function setUp() + { + $this->mockCiBuilder = $this->getMock( + '\PHPCI\Builder', + array('findBinary', 'executeCommand'), + array(), + "mockBuilder_phpUnit", + false + ); + $this->mockCiBuilder->buildPath = "/"; $this->mockBuild = $this->getMock( - '\PHPCI\Model\Build', - array(), - array(), - "MockBuild", - false - ); + '\PHPCI\Model\Build', + array(), + array(), + "MockBuild", + false + ); - $this->loadPhpUnitWithOptions(); - } + $this->loadPhpUnitWithOptions(); + } - protected function loadPhpUnitWithOptions($arrOptions = array()) - { - $this->testedPhpUnit = new PHPUnit($this->mockCiBuilder,$this->mockBuild, $arrOptions); - } + protected function loadPhpUnitWithOptions($arrOptions = array()) + { + $this->testedPhpUnit = new PhpUnit($this->mockCiBuilder, $this->mockBuild, $arrOptions); + } /** * @param \PHPUnit_Framework_MockObject_Matcher_Invocation $expectation */ - protected function expectFindBinaryToBeCalled($expectation) { + protected function expectFindBinaryToBeCalled($expectation) + { $this->mockCiBuilder->expects($expectation) - ->method("findBinary") - ->will($this->returnValue("phpunit")); + ->method("findBinary") + ->will($this->returnValue("phpunit")); } /** @@ -74,89 +76,97 @@ class PHPUnitTest extends \PHPUnit_Framework_TestCase public function expectExectuteCommandToBeCalled($expectation) { $this->mockCiBuilder->expects($expectation) - ->method("executeCommand"); + ->method("executeCommand"); } - /** - * @covers PHPUnit::execute - */ - public function testExecute_ReturnsTrueWithoutArgs() - { - $returnValue = $this->testedPhpUnit->execute(); - $expectedReturn = true; + /** + * @covers PHPUnit::execute + */ + public function testExecute_ReturnsTrueWithoutArgs() + { + $returnValue = $this->testedPhpUnit->execute(); + $expectedReturn = true; - $this->assertEquals($expectedReturn, $returnValue); - } + $this->assertEquals($expectedReturn, $returnValue); + } - /** - * @covers PHPUnit::execute - * @covers PHPUnit::runDir - */ - public function testExecute_CallsExecuteCommandOnceWhenGivenStringDirectory() - { - chdir('/'); + /** + * @covers PHPUnit::execute + * @covers PHPUnit::runDir + */ + public function testExecute_CallsExecuteCommandOnceWhenGivenStringDirectory() + { + chdir('/'); - $this->loadPhpUnitWithOptions(array( - 'directory' => "Fake/Test/Path" - )); - - $this->expectFindBinaryToBeCalled($this->once()); - $this->expectExectuteCommandToBeCalled($this->once()); - - $returnValue = $this->testedPhpUnit->execute(); - } - - /** - * @covers PHPUnit::execute - * @covers PHPUnit::runConfigFile - */ - public function testExecute_CallsExecuteCommandOnceWhenGivenStringConfig() - { - chdir('/'); - - $this->loadPhpUnitWithOptions(array( - 'config' => "Fake/Test/config.xml" - )); + $this->loadPhpUnitWithOptions( + array( + 'directory' => "Fake/Test/Path" + ) + ); $this->expectFindBinaryToBeCalled($this->once()); $this->expectExectuteCommandToBeCalled($this->once()); $returnValue = $this->testedPhpUnit->execute(); - } + } - /** - * @covers PHPUnit::execute - * @covers PHPUnit::runDir - */ - public function testExecute_CallsExecuteCommandManyTimesWhenGivenArrayDirectory() - { - chdir('/'); + /** + * @covers PHPUnit::execute + * @covers PHPUnit::runConfigFile + */ + public function testExecute_CallsExecuteCommandOnceWhenGivenStringConfig() + { + chdir('/'); - $this->loadPhpUnitWithOptions(array( - 'directory' => array(0, 1) - )); + $this->loadPhpUnitWithOptions( + array( + 'config' => "Fake/Test/config.xml" + ) + ); - $this->mockCiBuilder->expects($this->at(0))->method("executeCommand"); - $this->mockCiBuilder->expects($this->at(1))->method("executeCommand"); + $this->expectFindBinaryToBeCalled($this->once()); + $this->expectExectuteCommandToBeCalled($this->once()); - $returnValue = $this->testedPhpUnit->execute(); - } + $returnValue = $this->testedPhpUnit->execute(); + } - /** - * @covers PHPUnit::execute - * @covers PHPUnit::runConfigFile - */ - public function testExecute_CallsExecuteCommandManyTimesWhenGivenArrayConfig() - { - chdir('/'); + /** + * @covers PHPUnit::execute + * @covers PHPUnit::runDir + */ + public function testExecute_CallsExecuteCommandManyTimesWhenGivenArrayDirectory() + { + chdir('/'); - $this->loadPhpUnitWithOptions(array( - 'config' => array(0, 1) - )); + $this->loadPhpUnitWithOptions( + array( + 'directory' => array("dir1", "dir2") + ) + ); - $this->mockCiBuilder->expects($this->at(0))->method("executeCommand"); - $this->mockCiBuilder->expects($this->at(1))->method("executeCommand"); + $this->expectFindBinaryToBeCalled($this->exactly(2)); + $this->expectExectuteCommandToBeCalled($this->exactly(2)); - $returnValue = $this->testedPhpUnit->execute(); - } + $returnValue = $this->testedPhpUnit->execute(); + } + + /** + * @covers PHPUnit::execute + * @covers PHPUnit::runConfigFile + */ + public function testExecute_CallsExecuteCommandManyTimesWhenGivenArrayConfig() + { + chdir('/'); + + $this->loadPhpUnitWithOptions( + array( + 'config' => array("configfile1.xml", "configfile2.xml") + ) + ); + + $this->expectFindBinaryToBeCalled($this->exactly(2)); + $this->expectExectuteCommandToBeCalled($this->exactly(2)); + + $returnValue = $this->testedPhpUnit->execute(); + } } \ No newline at end of file diff --git a/console b/console index 3358dce7..f855f5ab 100644 --- a/console +++ b/console @@ -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();