Skip to content

Commit

Permalink
Custom binary - add basic validation that right binary is used
Browse files Browse the repository at this point in the history
Prevent errors like:

phpunit:
    binary: ../../vendor/bin/phploc
  • Loading branch information
zdenekdrahos committed Sep 23, 2017
1 parent ee40286 commit 300eb60
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ private function loadConfig($directory, $isUserDirectory = false)
public function getCustomBinary($tool)
{
$binary = $this->path("{$tool}.binary");
return $binary ? escapePath($binary) : $binary;
if ($binary) {
$filename = basename($binary);
if (is_bool(strpos($filename, "{$tool}"))) {
throw new \RuntimeException("Invalid '{$tool}' binary ('{$tool}' not found in '{$binary}')");
}
return escapePath($binary);
}
return null;
}

public function value($path)
Expand Down
8 changes: 8 additions & 0 deletions tests/Config/.phpqa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ phpcpd:

# Php file extensions to parse.
extensions: php,inc,module

# invalid binary
phpunit:
binary: non-existent-binary

# binary for other tool
phpmetrics:
binary: ../../phpqa
16 changes: 16 additions & 0 deletions tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ public function testUseCwdIfNoDirectoryIsSpecified()
$config->loadUserConfig('');
}

public function testThrowExceptionWhenBinaryDoesNotExist()
{
$config = new Config();
$config->loadUserConfig(__DIR__);
$this->shouldStopPhpqa();
$config->getCustomBinary('phpunit');
}

public function testThrowExceptionWhenWrongBinaryIsUsed()
{
$config = new Config();
$config->loadUserConfig(__DIR__);
$this->shouldStopPhpqa();
$config->getCustomBinary('phpmetrics');
}

private function shouldStopPhpqa()
{
$this->setExpectedException('Exception');
Expand Down

0 comments on commit 300eb60

Please sign in to comment.