diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index c53fd670..c1203b13 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -116,7 +116,7 @@ class Builder implements LoggerAwareInterface, BuildLogger $this->store = Store\Factory::getStore('Build'); $this->pluginExecutor = new Plugin\Util\Executor($this->buildPluginFactory($build), $this); - $this->commandExecutor = new CommandExecutor($this, $this->quiet, $this->verbose); + $this->commandExecutor = new CommandExecutor($this, PHPCI_DIR, $this->quiet, $this->verbose); } /** @@ -228,6 +228,16 @@ class Builder implements LoggerAwareInterface, BuildLogger return $this->commandExecutor->getLastOutput(); } + /** + * Find a binary required by a plugin. + * @param $binary + * @return null|string + */ + public function findBinary($binary) + { + return $this->commandExecutor->findBinary($binary); + } + /** * Add an entry to the build log. * @param string|string[] $message @@ -363,39 +373,6 @@ class Builder implements LoggerAwareInterface, BuildLogger return true; } - /** - * Find a binary required by a plugin. - * @param $binary - * @return null|string - */ - public function findBinary($binary) - { - if (is_string($binary)) { - $binary = array($binary); - } - - foreach ($binary as $bin) { - // Check project root directory: - if (is_file(PHPCI_DIR . $bin)) { - return PHPCI_DIR . $bin; - } - - // Check Composer bin dir: - if (is_file(PHPCI_DIR . 'vendor/bin/' . $bin)) { - return PHPCI_DIR . 'vendor/bin/' . $bin; - } - - // Use "which" - $which = trim(shell_exec('which ' . $bin)); - - if (!empty($which)) { - return $which; - } - } - - return null; - } - /** * Sets a logger instance on the object * diff --git a/PHPCI/Helper/CommandExecutor.php b/PHPCI/Helper/CommandExecutor.php index b7fc3494..bce980e7 100644 --- a/PHPCI/Helper/CommandExecutor.php +++ b/PHPCI/Helper/CommandExecutor.php @@ -24,18 +24,27 @@ class CommandExecutor protected $lastOutput; + /** + * The path which findBinary will look in. + * @var string + */ + protected $rootDir; + /** * @param BuildLogger $logger + * @param $rootDir * @param bool $quiet * @param bool $verbose */ - public function __construct(BuildLogger $logger, &$quiet = false, &$verbose = false) + public function __construct(BuildLogger $logger, $rootDir, &$quiet = false, &$verbose = false) { $this->logger = $logger; $this->quiet = $quiet; $this->verbose = $verbose; $this->lastOutput = array(); + + $this->rootDir = $rootDir; } /** @@ -76,4 +85,37 @@ class CommandExecutor { return implode(PHP_EOL, $this->lastOutput); } + + /** + * Find a binary required by a plugin. + * @param $binary + * @return null|string + */ + public function findBinary($binary) + { + if (is_string($binary)) { + $binary = array($binary); + } + + foreach ($binary as $bin) { + // Check project root directory: + if (is_file($this->rootDir . $bin)) { + return $this->rootDir . $bin; + } + + // Check Composer bin dir: + if (is_file($this->rootDir . 'vendor/bin/' . $bin)) { + return $this->rootDir . 'vendor/bin/' . $bin; + } + + // Use "which" + $which = trim(shell_exec('which ' . $bin)); + + if (!empty($which)) { + return $which; + } + } + + return null; + } } diff --git a/Tests/PHPCI/Helper/CommandExecutorTest.php b/Tests/PHPCI/Helper/CommandExecutorTest.php index cfdcc6cf..c50fc0be 100644 --- a/Tests/PHPCI/Helper/CommandExecutorTest.php +++ b/Tests/PHPCI/Helper/CommandExecutorTest.php @@ -16,7 +16,7 @@ class CommandExecutorTest extends ProphecyTestCase { parent::setUp(); $mockBuildLogger = $this->prophesize('\PHPCI\BuildLogger'); - $this->testedExecutor = new CommandExecutor($mockBuildLogger->reveal()); + $this->testedExecutor = new CommandExecutor($mockBuildLogger->reveal(), __DIR__ . "/"); } public function testGetLastOutput_ReturnsOutputOfCommand() @@ -45,4 +45,11 @@ class CommandExecutorTest extends ProphecyTestCase $returnValue = $this->testedExecutor->executeCommand(array('eerfdcvcho "%s"', 'Hello World')); $this->assertFalse($returnValue); } + + public function testFindBinary_ReturnsPathInSpecifiedRoot() + { + $thisFileName = "CommandExecutorTest.php"; + $returnValue = $this->testedExecutor->findBinary($thisFileName); + $this->assertEquals(__DIR__ . "/" . $thisFileName, $returnValue); + } } \ No newline at end of file