diff --git a/src/PhpGitHooks/Application/PhpUnit/PhpUnitHandler.php b/src/PhpGitHooks/Application/PhpUnit/PhpUnitHandler.php index abb561b..7f68007 100644 --- a/src/PhpGitHooks/Application/PhpUnit/PhpUnitHandler.php +++ b/src/PhpGitHooks/Application/PhpUnit/PhpUnitHandler.php @@ -15,7 +15,7 @@ class PhpUnitHandler extends ToolHandler { /** @var PhpUnitProcessBuilder */ - private $phpUnitProcessBuilder; + protected $phpUnitProcessBuilder; /** * @param OutputHandlerInterface $outputHandler @@ -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 @@ -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'); diff --git a/src/PhpGitHooks/Application/PhpUnit/PhpUnitRandomizerHandler.php b/src/PhpGitHooks/Application/PhpUnit/PhpUnitRandomizerHandler.php index 7ac4332..ee39b4f 100644 --- a/src/PhpGitHooks/Application/PhpUnit/PhpUnitRandomizerHandler.php +++ b/src/PhpGitHooks/Application/PhpUnit/PhpUnitRandomizerHandler.php @@ -4,4 +4,11 @@ class PhpUnitRandomizerHandler extends PhpUnitHandler { + /** + * @return \Symfony\Component\Process\ProcessBuilder + */ + protected function processBuilder() + { + return $this->phpUnitProcessBuilder->getProcessBuilder($this->getBinPath('phpunit-randomizer')); + } } diff --git a/src/PhpGitHooks/Infrastructure/CodeSniffer/CodeSnifferHandler.php b/src/PhpGitHooks/Infrastructure/CodeSniffer/CodeSnifferHandler.php index 6bc9e9b..d9f1c38 100644 --- a/src/PhpGitHooks/Infrastructure/CodeSniffer/CodeSnifferHandler.php +++ b/src/PhpGitHooks/Infrastructure/CodeSniffer/CodeSnifferHandler.php @@ -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(); diff --git a/src/PhpGitHooks/Infrastructure/Common/ProcessBuilderInterface.php b/src/PhpGitHooks/Infrastructure/Common/ProcessBuilderInterface.php index e39cd79..eb271ca 100644 --- a/src/PhpGitHooks/Infrastructure/Common/ProcessBuilderInterface.php +++ b/src/PhpGitHooks/Infrastructure/Common/ProcessBuilderInterface.php @@ -12,9 +12,11 @@ interface ProcessBuilderInterface { /** + * @param string $bin + * * @return ProcessBuilder */ - public function getProcessBuilder(); + public function getProcessBuilder($bin); /** * @param Process $process diff --git a/src/PhpGitHooks/Infrastructure/Common/ToolHandler.php b/src/PhpGitHooks/Infrastructure/Common/ToolHandler.php index 1e05dfa..1ef232e 100644 --- a/src/PhpGitHooks/Infrastructure/Common/ToolHandler.php +++ b/src/PhpGitHooks/Infrastructure/Common/ToolHandler.php @@ -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; + } + } } /** @@ -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; + } } diff --git a/src/PhpGitHooks/Infrastructure/JsonLint/JsonLintHandler.php b/src/PhpGitHooks/Infrastructure/JsonLint/JsonLintHandler.php index 06f1e7f..7036264 100644 --- a/src/PhpGitHooks/Infrastructure/JsonLint/JsonLintHandler.php +++ b/src/PhpGitHooks/Infrastructure/JsonLint/JsonLintHandler.php @@ -34,7 +34,7 @@ public function run(array $messages) $processBuilder = new ProcessBuilder( array( 'php', - 'bin/jsonlint', + $this->getBinPath('jsonlint'), $file, ) ); diff --git a/src/PhpGitHooks/Infrastructure/PhpCsFixer/PhpCsFixerHandler.php b/src/PhpGitHooks/Infrastructure/PhpCsFixer/PhpCsFixerHandler.php index 4412bb1..e1c9e0f 100644 --- a/src/PhpGitHooks/Infrastructure/PhpCsFixer/PhpCsFixerHandler.php +++ b/src/PhpGitHooks/Infrastructure/PhpCsFixer/PhpCsFixerHandler.php @@ -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, diff --git a/src/PhpGitHooks/Infrastructure/PhpMD/PhpMDHandler.php b/src/PhpGitHooks/Infrastructure/PhpMD/PhpMDHandler.php index 8bd95ad..234a77d 100644 --- a/src/PhpGitHooks/Infrastructure/PhpMD/PhpMDHandler.php +++ b/src/PhpGitHooks/Infrastructure/PhpMD/PhpMDHandler.php @@ -56,7 +56,7 @@ public function run(array $messages) $processBuilder = new ProcessBuilder( array( 'php', - 'bin/phpmd', + $this->getBinPath('phpmd'), $file, 'text', 'PmdRules.xml', diff --git a/src/PhpGitHooks/Infrastructure/PhpUnit/PhpUnitProcessBuilder.php b/src/PhpGitHooks/Infrastructure/PhpUnit/PhpUnitProcessBuilder.php index 259cb86..1958688 100644 --- a/src/PhpGitHooks/Infrastructure/PhpUnit/PhpUnitProcessBuilder.php +++ b/src/PhpGitHooks/Infrastructure/PhpUnit/PhpUnitProcessBuilder.php @@ -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)); } /** diff --git a/src/PhpGitHooks/Infrastructure/PhpUnit/PhpUnitRandomizerProcessBuilder.php b/src/PhpGitHooks/Infrastructure/PhpUnit/PhpUnitRandomizerProcessBuilder.php index 5abf327..186dd4a 100644 --- a/src/PhpGitHooks/Infrastructure/PhpUnit/PhpUnitRandomizerProcessBuilder.php +++ b/src/PhpGitHooks/Infrastructure/PhpUnit/PhpUnitRandomizerProcessBuilder.php @@ -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')); } }