diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index b2a135f4..39e8ca41 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -259,12 +259,14 @@ class Builder implements LoggerAwareInterface /** * Find a binary required by a plugin. - * @param $binary + * @param string $binary + * @param bool $quiet + * * @return null|string */ - public function findBinary($binary) + public function findBinary($binary, $quiet = false) { - return $this->commandExecutor->findBinary($binary, $this->buildPath); + return $this->commandExecutor->findBinary($binary, $quiet = false); } /** diff --git a/PHPCI/Helper/BaseCommandExecutor.php b/PHPCI/Helper/BaseCommandExecutor.php index 5435d597..bd948834 100644 --- a/PHPCI/Helper/BaseCommandExecutor.php +++ b/PHPCI/Helper/BaseCommandExecutor.php @@ -9,9 +9,9 @@ namespace PHPCI\Helper; -use \PHPCI\Logging\BuildLogger; +use Exception; +use PHPCI\Logging\BuildLogger; use Psr\Log\LogLevel; -use PHPCI\Helper\Lang; /** * Handles running system commands with variables. @@ -20,7 +20,7 @@ use PHPCI\Helper\Lang; abstract class BaseCommandExecutor implements CommandExecutor { /** - * @var \PHPCI\Logging\BuildLogger + * @var BuildLogger */ protected $logger; @@ -144,13 +144,12 @@ abstract class BaseCommandExecutor implements CommandExecutor /** * Find a binary required by a plugin. * @param string $binary - * @param null $buildPath + * @param bool $quiet * @return null|string */ - public function findBinary($binary, $buildPath = null) + public function findBinary($binary, $quiet = false) { - $binaryPath = null; - $composerBin = $this->getComposerBinDir(realpath($buildPath)); + $composerBin = $this->getComposerBinDir(realpath($this->buildPath)); if (is_string($binary)) { $binary = array($binary); @@ -161,30 +160,30 @@ abstract class BaseCommandExecutor implements CommandExecutor if (is_dir($composerBin) && is_file($composerBin.'/'.$bin)) { $this->logger->log(Lang::get('found_in_path', $composerBin, $bin), LogLevel::DEBUG); - $binaryPath = $composerBin . '/' . $bin; - break; + return $composerBin . '/' . $bin; } if (is_file($this->rootDir . $bin)) { $this->logger->log(Lang::get('found_in_path', 'root', $bin), LogLevel::DEBUG); - $binaryPath = $this->rootDir . $bin; - break; + return $this->rootDir . $bin; } if (is_file($this->rootDir . 'vendor/bin/' . $bin)) { $this->logger->log(Lang::get('found_in_path', 'vendor/bin', $bin), LogLevel::DEBUG); - $binaryPath = $this->rootDir . 'vendor/bin/' . $bin; - break; + return $this->rootDir . 'vendor/bin/' . $bin; } $findCmdResult = $this->findGlobalBinary($bin); if (is_file($findCmdResult)) { $this->logger->log(Lang::get('found_in_path', '', $bin), LogLevel::DEBUG); - $binaryPath = $findCmdResult; - break; + return $findCmdResult; } } - return $binaryPath; + + if ($quiet) { + return; + } + throw new Exception(Lang::get('could_not_find', implode('/', $binary))); } /** diff --git a/PHPCI/Helper/CommandExecutor.php b/PHPCI/Helper/CommandExecutor.php index bacd1a2e..4f0028eb 100644 --- a/PHPCI/Helper/CommandExecutor.php +++ b/PHPCI/Helper/CommandExecutor.php @@ -26,10 +26,13 @@ interface CommandExecutor /** * Find a binary required by a plugin. * @param string $binary - * @param string $buildPath the current build path + * @param bool $quiet Returns null instead of throwing an execption. + * * @return null|string + * + * @throws \Exception when no binary has been found and $quiet is false. */ - public function findBinary($binary, $buildPath = null); + public function findBinary($binary, $quiet = false); /** * Set the buildPath property. diff --git a/PHPCI/Plugin/Codeception.php b/PHPCI/Plugin/Codeception.php index 7d7fd81e..9a651bec 100644 --- a/PHPCI/Plugin/Codeception.php +++ b/PHPCI/Plugin/Codeception.php @@ -133,12 +133,6 @@ class Codeception implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin } else { $codecept = $this->phpci->findBinary('codecept'); - if (!$codecept) { - $this->phpci->logFailure(Lang::get('could_not_find', 'codecept')); - - return false; - } - $cmd = 'cd "%s" && ' . $codecept . ' run -c "%s" --tap ' . $this->args; if (IS_WIN) { diff --git a/PHPCI/Plugin/Composer.php b/PHPCI/Plugin/Composer.php index 3930f3d6..87a558fe 100644 --- a/PHPCI/Plugin/Composer.php +++ b/PHPCI/Plugin/Composer.php @@ -81,11 +81,6 @@ class Composer implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin { $composerLocation = $this->phpci->findBinary(array('composer', 'composer.phar')); - if (!$composerLocation) { - $this->phpci->logFailure(Lang::get('could_not_find', 'composer')); - return false; - } - $cmd = ''; if (IS_WIN) { diff --git a/PHPCI/Plugin/Pdepend.php b/PHPCI/Plugin/Pdepend.php index d1039554..73b669c1 100644 --- a/PHPCI/Plugin/Pdepend.php +++ b/PHPCI/Plugin/Pdepend.php @@ -79,15 +79,10 @@ class Pdepend implements \PHPCI\Plugin $pdepend = $this->phpci->findBinary('pdepend'); - if (!$pdepend) { - $this->phpci->logFailure(Lang::get('could_not_find', 'pdepend')); - return false; - } - $cmd = $pdepend . ' --summary-xml="%s" --jdepend-chart="%s" --overview-pyramid="%s" %s "%s"'; $this->removeBuildArtifacts(); - + // If we need to ignore directories if (count($this->phpci->ignore)) { $ignore = ' --ignore=' . implode(',', $this->phpci->ignore); diff --git a/PHPCI/Plugin/Phing.php b/PHPCI/Plugin/Phing.php index 2bf64cca..3a5bd834 100644 --- a/PHPCI/Plugin/Phing.php +++ b/PHPCI/Plugin/Phing.php @@ -81,11 +81,6 @@ class Phing implements \PHPCI\Plugin { $phingExecutable = $this->phpci->findBinary('phing'); - if (!$phingExecutable) { - $this->phpci->logFailure(Lang::get('could_not_find', 'phing')); - return false; - } - $cmd[] = $phingExecutable . ' -f ' . $this->getBuildFilePath(); if ($this->getPropertyFile()) { diff --git a/PHPCI/Plugin/PhpCodeSniffer.php b/PHPCI/Plugin/PhpCodeSniffer.php index df276eac..877d24c6 100644 --- a/PHPCI/Plugin/PhpCodeSniffer.php +++ b/PHPCI/Plugin/PhpCodeSniffer.php @@ -149,11 +149,6 @@ class PhpCodeSniffer implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin $phpcs = $this->phpci->findBinary('phpcs'); - if (!$phpcs) { - $this->phpci->logFailure(PHPCI\Helper\Lang::get('could_not_find', 'phpcs')); - return false; - } - $this->phpci->logExecOutput(false); $cmd = $phpcs . ' --report=json %s %s %s %s %s "%s"'; diff --git a/PHPCI/Plugin/PhpCpd.php b/PHPCI/Plugin/PhpCpd.php index c663db5a..6433308f 100644 --- a/PHPCI/Plugin/PhpCpd.php +++ b/PHPCI/Plugin/PhpCpd.php @@ -90,18 +90,13 @@ class PhpCpd implements \PHPCI\Plugin $phpcpd = $this->phpci->findBinary('phpcpd'); - if (!$phpcpd) { - $this->phpci->logFailure(Lang::get('could_not_find', 'phpcpd')); - return false; - } - $tmpfilename = tempnam('/tmp', 'phpcpd'); $cmd = $phpcpd . ' --log-pmd "%s" %s "%s"'; $success = $this->phpci->executeCommand($cmd, $tmpfilename, $ignore, $this->path); print $this->phpci->getLastOutput(); - + list($errorCount, $data) = $this->processReport(file_get_contents($tmpfilename)); $this->build->storeMeta('phpcpd-warnings', $errorCount); $this->build->storeMeta('phpcpd-data', $data); diff --git a/PHPCI/Plugin/PhpCsFixer.php b/PHPCI/Plugin/PhpCsFixer.php index 1374bc71..e07db718 100644 --- a/PHPCI/Plugin/PhpCsFixer.php +++ b/PHPCI/Plugin/PhpCsFixer.php @@ -69,11 +69,6 @@ class PhpCsFixer implements \PHPCI\Plugin $phpcsfixer = $this->phpci->findBinary('php-cs-fixer'); - if (!$phpcsfixer) { - $this->phpci->logFailure(Lang::get('could_not_find', 'php-cs-fixer')); - return false; - } - $cmd = $phpcsfixer . ' fix . %s %s %s'; $success = $this->phpci->executeCommand($cmd, $this->verbose, $this->diff, $this->level); diff --git a/PHPCI/Plugin/PhpDocblockChecker.php b/PHPCI/Plugin/PhpDocblockChecker.php index 9f71afef..7fb0fe11 100644 --- a/PHPCI/Plugin/PhpDocblockChecker.php +++ b/PHPCI/Plugin/PhpDocblockChecker.php @@ -104,11 +104,6 @@ class PhpDocblockChecker implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin // Check that the binary exists: $checker = $this->phpci->findBinary('phpdoccheck'); - if (!$checker) { - $this->phpci->logFailure(PHPCI\Helper\Lang::get('could_not_find', 'phpdoccheck')); - return false; - } - // Build ignore string: $ignore = ''; if (count($this->ignore)) { diff --git a/PHPCI/Plugin/PhpLoc.php b/PHPCI/Plugin/PhpLoc.php index 65398e05..76887493 100644 --- a/PHPCI/Plugin/PhpLoc.php +++ b/PHPCI/Plugin/PhpLoc.php @@ -80,11 +80,6 @@ class PhpLoc implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin $phploc = $this->phpci->findBinary('phploc'); - if (!$phploc) { - $this->phpci->logFailure(PHPCI\Helper\Lang::get('could_not_find', 'phploc')); - return false; - } - $success = $this->phpci->executeCommand($phploc . ' %s "%s"', $ignore, $this->directory); $output = $this->phpci->getLastOutput(); diff --git a/PHPCI/Plugin/PhpMessDetector.php b/PHPCI/Plugin/PhpMessDetector.php index 21c171cb..b02f5f09 100644 --- a/PHPCI/Plugin/PhpMessDetector.php +++ b/PHPCI/Plugin/PhpMessDetector.php @@ -121,11 +121,6 @@ class PhpMessDetector implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin $phpmdBinaryPath = $this->phpci->findBinary('phpmd'); - if (!$phpmdBinaryPath) { - $this->phpci->logFailure(PHPCI\Helper\Lang::get('could_not_find', 'phpmd')); - return false; - } - $this->executePhpMd($phpmdBinaryPath); list($errorCount, $data) = $this->processReport(trim($this->phpci->getLastOutput())); diff --git a/PHPCI/Plugin/PhpParallelLint.php b/PHPCI/Plugin/PhpParallelLint.php index fdaeaf65..8b7528de 100644 --- a/PHPCI/Plugin/PhpParallelLint.php +++ b/PHPCI/Plugin/PhpParallelLint.php @@ -78,11 +78,6 @@ class PhpParallelLint implements \PHPCI\Plugin $phplint = $this->phpci->findBinary('parallel-lint'); - if (!$phplint) { - $this->phpci->logFailure(Lang::get('could_not_find', 'parallel-lint')); - return false; - } - $cmd = $phplint . ' %s "%s"'; $success = $this->phpci->executeCommand( $cmd, diff --git a/PHPCI/Plugin/PhpSpec.php b/PHPCI/Plugin/PhpSpec.php index 681a5a6c..e468a718 100644 --- a/PHPCI/Plugin/PhpSpec.php +++ b/PHPCI/Plugin/PhpSpec.php @@ -59,11 +59,6 @@ class PhpSpec implements PHPCI\Plugin $phpspec = $this->phpci->findBinary(array('phpspec', 'phpspec.php')); - if (!$phpspec) { - $this->phpci->logFailure(PHPCI\Helper\Lang::get('could_not_find', 'phpspec')); - return false; - } - $success = $this->phpci->executeCommand($phpspec . ' --format=junit --no-code-generation run'); $output = $this->phpci->getLastOutput(); diff --git a/PHPCI/Plugin/PhpUnit.php b/PHPCI/Plugin/PhpUnit.php index 70e0e74f..7fb626df 100644 --- a/PHPCI/Plugin/PhpUnit.php +++ b/PHPCI/Plugin/PhpUnit.php @@ -197,15 +197,8 @@ class PhpUnit implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin chdir($this->phpci->buildPath.'/'.$this->runFrom); } - $phpunit = $this->phpci->findBinary('phpunit'); - if (!$phpunit) { - $this->phpci->logFailure(PHPCI\Helper\Lang::get('could_not_find', 'phpunit')); - return false; - } - - $cmd = $phpunit . ' --tap %s -c "%s" ' . $this->coverage . $this->path; $success = $this->phpci->executeCommand($cmd, $this->args, $this->phpci->buildPath . $configPath); @@ -232,11 +225,6 @@ class PhpUnit implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin $phpunit = $this->phpci->findBinary('phpunit'); - if (!$phpunit) { - $this->phpci->logFailure(PHPCI\Helper\Lang::get('could_not_find', 'phpunit')); - return false; - } - $cmd = $phpunit . ' --tap %s "%s"'; $success = $this->phpci->executeCommand($cmd, $this->args, $this->phpci->buildPath . $directory); chdir($curdir); diff --git a/PHPCI/Plugin/Xmpp.php b/PHPCI/Plugin/Xmpp.php index add778cb..0362aa85 100644 --- a/PHPCI/Plugin/Xmpp.php +++ b/PHPCI/Plugin/Xmpp.php @@ -148,12 +148,7 @@ class XMPP implements \PHPCI\Plugin */ public function execute() { - $sendxmpp = $this->phpci->findBinary('/usr/bin/sendxmpp'); - - if (!$sendxmpp) { - $this->phpci->logFailure('Could not find sendxmpp.'); - return false; - } + $sendxmpp = $this->phpci->findBinary('sendxmpp'); /* * Without recipients we can't send notification diff --git a/Tests/PHPCI/Helper/CommandExecutorTest.php b/Tests/PHPCI/Helper/CommandExecutorTest.php index 4cf14887..5d5dc08b 100644 --- a/Tests/PHPCI/Helper/CommandExecutorTest.php +++ b/Tests/PHPCI/Helper/CommandExecutorTest.php @@ -27,7 +27,8 @@ class CommandExecutorTest extends \PHPUnit_Framework_TestCase } parent::setUp(); $mockBuildLogger = $this->prophesize('PHPCI\Logging\BuildLogger'); - $this->testedExecutor = new UnixCommandExecutor($mockBuildLogger->reveal(), __DIR__ . "/"); + $class = IS_WIN ? 'PHPCI\Helper\WindowsCommandExecutor' : 'PHPCI\Helper\UnixCommandExecutor'; + $this->testedExecutor = new $class($mockBuildLogger->reveal(), __DIR__ . "/"); } public function testGetLastOutput_ReturnsOutputOfCommand() @@ -63,4 +64,20 @@ class CommandExecutorTest extends \PHPUnit_Framework_TestCase $returnValue = $this->testedExecutor->findBinary($thisFileName); $this->assertEquals(__DIR__ . "/" . $thisFileName, $returnValue); } + + /** + * @expectedException \Exception + * @expectedMessageRegex WorldWidePeace + */ + public function testFindBinary_ThrowsWhenNotFound() + { + $thisFileName = "WorldWidePeace"; + $this->testedExecutor->findBinary($thisFileName); + } + + public function testFindBinary_ReturnsNullWihQuietArgument() + { + $thisFileName = "WorldWidePeace"; + $this->assertNull($this->testedExecutor->findBinary($thisFileName, true)); + } }