Skip to content
This repository has been archived by the owner on Mar 30, 2018. It is now read-only.

Support for optionally configuring phpunit executable #155

Merged
merged 4 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -19,6 +19,6 @@ public function testFinderCanLocatePhpunitExecutable()
{
$finder = new PhpunitExecutableFinder();
$result = $finder->find();
$this->assertRegExp('%phpunit(\\.bat|\\.phar)?$%', $result);
$this->assertRegExp('%phpunit.*(\\.bat|\\.phar)?$%', $result);
}
}