Skip to content

Commit

Permalink
Support for optionally configuring phpunit executable (humbug#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-mueller authored and Marouane TABBABI committed Aug 4, 2017
1 parent 12a63d5 commit 2e63afa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
53 changes: 50 additions & 3 deletions src/Adapter/Phpunit/Process/PhpunitExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
use Humbug\Process\AbstractExecutableFinder;
use Humbug\Process\ComposerExecutableFinder;
use Humbug\Exception\RuntimeException;
use Humbug\Config;
use Humbug\Config\JsonParser;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\PhpExecutableFinder;

class PhpunitExecutableFinder extends AbstractExecutableFinder
{

/**
* @var Config
*/
private $config;

/**
* @return string
*/
Expand Down Expand Up @@ -65,14 +72,15 @@ private function findComposer()
*/
private function findPhpunit()
{
$probable = ['phpunit', 'phpunit.phar'];
$probable = $this->getExecutableNames();
$dir = $this->getPhpunitExecutablePath();
$finder = new ExecutableFinder;
foreach ($probable as $name) {
if ($path = $finder->find($name, null, [getcwd()])) {
if ($path = $finder->find($name, null, [$dir])) {
return $this->makeExecutable($path);
}
}
$result = $this->searchNonExecutables($probable, [getcwd()]);
$result = $this->searchNonExecutables($probable, [$dir]);
if (!is_null($result)) {
return $result;
}
Expand Down Expand Up @@ -102,4 +110,43 @@ protected function makeExecutable($path)
return sprintf('%s %s', $phpFinder->find(), $path);
}
}

private function setConfig()
{
$config = (new JsonParser())->parseFile();
$this->config = new Config($config);
}

/**
* @return Config
*/
private function getConfig()
{
if (is_null($this->config)) {
$this->setConfig();
}
return $this->config;
}

/**
* @return array
*/
private function getExecutableNames()
{
if ($this->getConfig()->isPhpunitConfigured()) {
return [basename($this->getConfig()->getPhpunitConfig()->phar)];
}
return ['phpunit', 'phpunit.phar'];
}

/**
* @return string
*/
private function getPhpunitExecutablePath()
{
if ($this->getConfig()->isPhpunitConfigured()) {
return dirname($this->getConfig()->getPhpunitConfig()->phar);
}
return getcwd();
}
}
23 changes: 23 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ public function getSource()
return $this->config->source;
}

public function getPhpunitConfig()
{
if (!isset($this->config->phpunit)) {
throw new JsonConfigException(
'Phpunit destination is not included in configuration file'
);
}
if (!isset($this->config->phpunit->phar)) {
throw new JsonConfigException(
'full path of phpunit executable is not included in configuration file'
);
}

return $this->config->phpunit;
}

/**
* @return bool
*/
public function isPhpunitConfigured() {
return isset($this->config->phpunit);
}

public function getTimeout()
{
if (!isset($this->config->timeout)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public function testFinderCanLocatePhpunitExecutable()
{
$finder = new PhpunitExecutableFinder();
$result = $finder->find();
$this->assertRegExp('%phpunit(\\.bat|\\.phar)?$%', $result);
$this->assertRegExp('%phpunit.*(\\.bat|\\.phar)?$%', $result);
}
}

0 comments on commit 2e63afa

Please sign in to comment.