diff --git a/src/Adapter/Phpunit/Process/PhpunitExecutableFinder.php b/src/Adapter/Phpunit/Process/PhpunitExecutableFinder.php index f8681fe..fd92e4d 100644 --- a/src/Adapter/Phpunit/Process/PhpunitExecutableFinder.php +++ b/src/Adapter/Phpunit/Process/PhpunitExecutableFinder.php @@ -13,6 +13,8 @@ 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; @@ -20,6 +22,11 @@ class PhpunitExecutableFinder extends AbstractExecutableFinder { + /** + * @var Config + */ + private $config; + /** * @return string */ @@ -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; } @@ -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(); + } } diff --git a/src/Config.php b/src/Config.php index 27f3258..2161752 100644 --- a/src/Config.php +++ b/src/Config.php @@ -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)) { diff --git a/tests/Adapter/Phpunit/Process/PhpunitExecutableFinderTest.php b/tests/Adapter/Phpunit/Process/PhpunitExecutableFinderTest.php index db7c421..1a7b03d 100644 --- a/tests/Adapter/Phpunit/Process/PhpunitExecutableFinderTest.php +++ b/tests/Adapter/Phpunit/Process/PhpunitExecutableFinderTest.php @@ -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); } }