Skip to content

Commit 5f8411e

Browse files
authored
fix: Make VersionParser throw the correct exception upon invalid version (#58)
Currently it throws an `InvalidArgumentException` but the abstract test framework package provides an `InvalidVersion` exception, which is what is documented for `TestFrameworkAdapter::getVersion()`.
1 parent 9ebb8f7 commit 5f8411e

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

src/CodeceptionAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
use function explode;
4444
use function implode;
4545
use Infection\AbstractTestFramework\Coverage\TestLocation;
46+
use Infection\AbstractTestFramework\InvalidVersion;
4647
use Infection\AbstractTestFramework\MemoryUsageAware;
4748
use Infection\AbstractTestFramework\TestFrameworkAdapter;
4849
use Infection\StreamWrapper\IncludeInterceptor;
4950
use Infection\TestFramework\Codeception\Coverage\JUnitTestCaseSorter;
50-
use InvalidArgumentException;
5151
use function is_string;
5252
use const LOCK_EX;
5353
use Phar;
@@ -237,7 +237,7 @@ public function getVersion(): string
237237

238238
try {
239239
$version = $this->versionParser->parse($process->getOutput());
240-
} catch (InvalidArgumentException $e) {
240+
} catch (InvalidVersion) {
241241
$version = 'unknown';
242242
}
243243

src/VersionParser.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535

3636
namespace Infection\TestFramework\Codeception;
3737

38-
use InvalidArgumentException;
38+
use Infection\AbstractTestFramework\InvalidVersion;
3939
use function preg_match;
40+
use function sprintf;
4041

4142
/**
4243
* @internal
@@ -45,15 +46,34 @@ class VersionParser
4546
{
4647
private const VERSION_REGEX = '/(?<version>[0-9]+\.[0-9]+\.?[0-9]*)(?<prerelease>-[0-9a-zA-Z.]+)?(?<build>\+[0-9a-zA-Z.]+)?/';
4748

49+
/**
50+
* @throws InvalidVersion
51+
*/
4852
public function parse(string $content): string
4953
{
5054
$matches = [];
5155
$matched = preg_match(self::VERSION_REGEX, $content, $matches) > 0;
5256

5357
if (!$matched) {
54-
throw new InvalidArgumentException('Parameter does not contain a valid SemVer (sub)string.');
58+
throw self::createInvalidVersion(
59+
CodeceptionAdapter::NAME,
60+
$content,
61+
);
5562
}
5663

5764
return $matches[0];
5865
}
66+
67+
private static function createInvalidVersion(
68+
string $testFrameworkName,
69+
string $version,
70+
): InvalidVersion {
71+
return new InvalidVersion(
72+
sprintf(
73+
'Could not recognise the test framework version for %s for the value "%s".',
74+
$testFrameworkName,
75+
$version,
76+
),
77+
);
78+
}
5979
}

tests/phpunit/Adapter/VersionParserTest.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535

3636
namespace Infection\Tests\TestFramework\Codeception\Adapter;
3737

38+
use Infection\AbstractTestFramework\InvalidVersion;
3839
use Infection\TestFramework\Codeception\VersionParser;
39-
use InvalidArgumentException;
4040
use PHPUnit\Framework\TestCase;
4141

4242
final class VersionParserTest extends TestCase
@@ -60,16 +60,13 @@ public function test_it_parses_version_from_string(string $content, string $expe
6060

6161
public function test_it_throws_exception_when_content_has_no_version_substring(): void
6262
{
63-
try {
64-
$this->versionParser->parse('abc');
65-
66-
$this->fail();
67-
} catch (InvalidArgumentException $exception) {
68-
$this->assertSame(
69-
'Parameter does not contain a valid SemVer (sub)string.',
70-
$exception->getMessage(),
71-
);
72-
}
63+
$this->expectExceptionObject(
64+
new InvalidVersion(
65+
'Could not recognise the test framework version for codeception for the value "abc".',
66+
),
67+
);
68+
69+
$this->versionParser->parse('abc');
7370
}
7471

7572
/**

0 commit comments

Comments
 (0)