Skip to content

Commit

Permalink
Merge pull request #55 from mdgbg/bin-auto-discovery
Browse files Browse the repository at this point in the history
Added auto bin dir discovery in cases when we have set config / bin-d…
  • Loading branch information
bruli committed Apr 10, 2016
2 parents 060b9bc + ed3d0c2 commit 08fbde6
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/PhpGitHooks/Application/PhpUnit/PhpUnitHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class PhpUnitHandler extends ToolHandler
{
/** @var PhpUnitProcessBuilder */
private $phpUnitProcessBuilder;
protected $phpUnitProcessBuilder;

/**
* @param OutputHandlerInterface $outputHandler
Expand All @@ -37,7 +37,7 @@ public function run(array $messages)
{
$this->setTitle();

$processBuilder = $this->phpUnitProcessBuilder->getProcessBuilder();
$processBuilder = $this->processBuilder();
$processBuilder->setTimeout(3600);
$phpunit = $processBuilder->getProcess();
$this->phpUnitProcessBuilder
Expand All @@ -49,6 +49,14 @@ public function run(array $messages)
}
}

/**
* @return \Symfony\Component\Process\ProcessBuilder
*/
protected function processBuilder()
{
return $this->phpUnitProcessBuilder->getProcessBuilder($this->getBinPath('phpunit'));
}

private function setTitle()
{
$this->outputHandler->setTitle('Running unit tests');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@

class PhpUnitRandomizerHandler extends PhpUnitHandler
{
/**
* @return \Symfony\Component\Process\ProcessBuilder
*/
protected function processBuilder()
{
return $this->phpUnitProcessBuilder->getProcessBuilder($this->getBinPath('phpunit-randomizer'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ public function run(array $messages)
}

if (false === $this->ignoreFiles->isIgnored($file)) {
$processBuilder = new ProcessBuilder(array('php', 'bin/phpcs', '--standard='.$this->standard, $file));
$processBuilder = new ProcessBuilder(
array(
'php',
$this->getBinPath('phpcs'),
'--standard='.$this->standard,
$file,
)
);
/** @var Process $phpCs */
$phpCs = $processBuilder->getProcess();
$phpCs->run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
interface ProcessBuilderInterface
{
/**
* @param string $bin
*
* @return ProcessBuilder
*/
public function getProcessBuilder();
public function getProcessBuilder($bin);

/**
* @param Process $process
Expand Down
42 changes: 42 additions & 0 deletions src/PhpGitHooks/Infrastructure/Common/ToolHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,40 @@
*/
abstract class ToolHandler
{
const COMPOSER_VENDOR_DIR = '/../../../../../../';
const COMPOSER_INSTALLED_FILE = 'composer/installed.json';

/** @var OutputHandlerInterface */
protected $outputHandler;
/** @var OutputInterface */
protected $output;

/** @var array */
private $tools = array(
'phpcs' => 'squizlabs/php_codesniffer',
'php-cs-fixer' => 'fabpot/php-cs-fixer',
'phpmd' => 'phpmd/phpmd',
'phpunit' => 'phpunit/phpunit',
'phpunit-randomizer' => 'fiunchinho/phpunit-randomizer',
'jsonlint' => 'seld/jsonlint',
);
/** @var array */
private $installedPackages = array();

/**
* @param OutputHandlerInterface $outputHandler
*/
public function __construct(OutputHandlerInterface $outputHandler)
{
$this->outputHandler = $outputHandler;

$installedJson = dirname(__FILE__).self::COMPOSER_VENDOR_DIR.self::COMPOSER_INSTALLED_FILE;
if (file_exists($installedJson)) { // else not installed over composer
$packages = json_decode(file_get_contents($installedJson), true);
foreach ($packages as $package) {
$this->installedPackages[$package['name']] = $package;
}
}
}

/**
Expand All @@ -40,4 +63,23 @@ protected function writeOutputError(\Exception $exceptionClass, $errorText)
ErrorOutput::write($errorText);
throw new $exceptionClass();
}

/**
* @param string $tool
*
* @return string
*/
protected function getBinPath($tool)
{
if (isset($this->installedPackages[$this->tools[$tool]])) {
$package = $this->installedPackages[$this->tools[$tool]];
foreach ($package['bin'] as $bin) {
if (preg_match("#${tool}$#", $bin)) {
return dirname(__FILE__).self::COMPOSER_VENDOR_DIR.$package['name'].DIRECTORY_SEPARATOR.$bin;
}
}
}

return 'bin'.DIRECTORY_SEPARATOR.$tool;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function run(array $messages)
$processBuilder = new ProcessBuilder(
array(
'php',
'bin/jsonlint',
$this->getBinPath('jsonlint'),
$file,
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function run(array $messages)
$processBuilder = new ProcessBuilder(
array(
'php',
'bin/php-cs-fixer',
$this->getBinPath('php-cs-fixer'),
'--dry-run',
'fix',
$file,
Expand Down
2 changes: 1 addition & 1 deletion src/PhpGitHooks/Infrastructure/PhpMD/PhpMDHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function run(array $messages)
$processBuilder = new ProcessBuilder(
array(
'php',
'bin/phpmd',
$this->getBinPath('phpmd'),
$file,
'text',
'PmdRules.xml',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
class PhpUnitProcessBuilder implements ProcessBuilderInterface
{
/**
* @param string $bin
*
* @return ProcessBuilder
*/
public function getProcessBuilder()
public function getProcessBuilder($bin)
{
return new ProcessBuilder(array('php', 'bin/phpunit'));
return new ProcessBuilder(array('php', $bin));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
final class PhpUnitRandomizerProcessBuilder extends PhpUnitProcessBuilder
{
/**
* @param string $bin
*
* @return ProcessBuilder
*/
public function getProcessBuilder()
public function getProcessBuilder($bin)
{
return new ProcessBuilder(array('php', 'bin/phpunit-randomizer', '--order', 'rand'));
return new ProcessBuilder(array('php', $bin, '--order', 'rand'));
}
}

0 comments on commit 08fbde6

Please sign in to comment.