Skip to content
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
2 changes: 0 additions & 2 deletions src/Tools/Analyzer/Phpcpd.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Edge\QA\Tools\Analyzer;

use Edge\QA\Tools\GetVersions;

class Phpcpd extends \Edge\QA\Tools\Tool
{
public static $SETTINGS = array(
Expand Down
1 change: 0 additions & 1 deletion src/Tools/Analyzer/Phpstan.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Edge\QA\Tools\Analyzer;

use Edge\QA\OutputMode;
use Edge\QA\Tools\GetVersions;

class Phpstan extends \Edge\QA\Tools\Tool
{
Expand Down
48 changes: 31 additions & 17 deletions src/Tools/GetVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

class GetVersions
{
public function getToolVersion(array $toolSettings)
public function hasToolVersion(array $toolSettings, $operator, $version)
{
$versions = $this->__invoke(['tool' => $toolSettings + ['customBinary' => null]]);
return $versions['tool']['version_normalized'];
$versions = $this->__invoke(['tool' => $toolSettings]);
$toolVersion = $versions['tool']['version_normalized'];
return self::compareVersions($toolVersion, $operator, $version);
}

public function __invoke(array $tools)
Expand Down Expand Up @@ -114,7 +115,7 @@ private function normalizeVersion(array $composerInfo)
if ($composerInfo['version_normalized'] == '9999999-dev') {
return $composerInfo['version'];
}
return preg_replace('/\.0$/s', '', $composerInfo['version_normalized']);
return self::normalizeSemver($composerInfo['version_normalized']);
}

private function groupAuthors(array $composerAuthors)
Expand All @@ -134,8 +135,12 @@ private function loadVersionFromConsoleCommand($command)
{
$process = $this->createSymfonyProcess($command);
$process->run();
$firstLine = $this->getFirstLine($process->getOutput());
return $this->extractVersion($firstLine);
if ($process->getOutput()) {
return self::extractVersionFromConsole($process->getOutput());
} elseif ($process->getErrorOutput()) {
return trim($process->getErrorOutput());
}
return "{$process->getExitCode()} {$process->getExitCodeText()}";
}

private function createSymfonyProcess($command)
Expand All @@ -147,20 +152,29 @@ private function createSymfonyProcess($command)
}
}

private function getFirstLine($string)
public static function extractVersionFromConsole($text)
{
return strtok($string, "\n");
$regexes = [
'semver' => '(\d+\.\d+(\.\d+)?)',
'dev version' => '(\d+\.x)',
];
foreach ($regexes as $regex) {
$match = [];
preg_match($regex, $text, $match);
if ($match) {
return self::normalizeSemver($match[0]);
}
}
return $text;
}

private function extractVersion($text)
public static function normalizeSemver($semver)
{
return str_replace(
array(
' by Sebastian Bergmann and contributors.',
'PHPUnit '
),
'',
$text
);
return preg_replace('/\.0$/s', '', $semver);
}

public static function compareVersions($toolVersion, $operator, $version)
{
return $toolVersion && version_compare(str_replace(".x", "", $toolVersion), $version, $operator);
}
}
6 changes: 4 additions & 2 deletions src/Tools/Tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ protected function saveDynamicConfig($config, $fileExtension)
protected function toolVersionIs($operator, $version)
{
$versions = new GetVersions();
$composerVersion = $versions->getToolVersion(static::$SETTINGS);
return $composerVersion && version_compare($composerVersion, $version, $operator);
$settings = static::$SETTINGS + [
'customBinary' => $this->config->getCustomBinary((string) $this->tool),
];
return $versions->hasToolVersion($settings, $operator, $version);
}

protected function writeln($text)
Expand Down
59 changes: 59 additions & 0 deletions tests/GetVersionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Edge\QA\Tools;

class GetVersionsTest extends \PHPUnit_Framework_TestCase
{
/** @dataProvider provideComposerVersion */
public function testNormalizeVersion($version, $expectedVersion)
{
assertThat(GetVersions::normalizeSemver($version), is($expectedVersion));
}

public function provideComposerVersion()
{
return [
'full semver' => ['2.7.4', '2.7.4'],
'minor release' => ['2.7.0', '2.7'],
'major release' => ['2.0.0', '2.0'],
];
}

/** @dataProvider provideCliVersion */
public function testCliVersion($cliOutput, $expectedVersion)
{
assertThat(GetVersions::extractVersionFromConsole($cliOutput), is($expectedVersion));
}

public function provideCliVersion()
{
return [
'semver at the end' => ['irrelevant 0.12.86', '0.12.86'],
'semver at the start' => ['0.12.86 irrelevant', '0.12.86'],
'semver at 2nd line' => ["first\nirrele 4.8.36 vant\nsecond", '4.8.36'],
'custom format' => ['irrelevant v1.10', '1.10'],
'no space' => ['v1.13.7irrelevant', '1.13.7'],
'dev version' => ['irrelevant 4.x-dev@', '4.x'],
'prefer semver (semver + dev)' => ['v1.13.7 4.x-dev@', '1.13.7'],
'prefer semver (dev + semver)' => ['4.x-dev@ v1.13.7', '1.13.7'],
'no version' => ['irrelevant text', 'irrelevant text'],
];
}

/** @dataProvider provideComparedVersions */
public function testCompareVersions($toolVersion, $operator, $version, $expectedResult)
{
assertThat(GetVersions::compareVersions($toolVersion, $operator, $version), is($expectedResult));
}

public function provideComparedVersions()
{
return [
'no version' => ['', '>', '1', false],
'is lower?' => ['7', '<', '6', false],
'is greater than dev version' => ['4', '>=', '4.x', true],
'semver is greater than' => ['4.1', '>=', '4', true],
'dev version is greater than' => ['4.x', '>=', '4', true],
];
}
}