Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/CodeceptionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

namespace Infection\TestFramework\Codeception;

use Infection\AbstractTestFramework\InvalidVersion;
use function array_filter;
use function array_key_exists;
use function array_map;
Expand Down Expand Up @@ -229,7 +230,7 @@ public function getVersion(): string

try {
$version = $this->versionParser->parse($process->getOutput());
} catch (InvalidArgumentException $e) {
} catch (InvalidVersion) {
$version = 'unknown';
}

Expand Down
57 changes: 57 additions & 0 deletions src/Throwable/InvalidVersionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\TestFramework\Codeception\Throwable;

use Infection\AbstractTestFramework\InvalidVersion;
use function sprintf;

final class InvalidVersionFactory
{
private function __construct()
{
}

public static function create(string $testFrameworkName, string $version): InvalidVersion
{
return new InvalidVersion(
sprintf(
'Could not recognise the test framework version for %s for the value "%s".',
$testFrameworkName,
$version,
),
);
}
}
10 changes: 9 additions & 1 deletion src/VersionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\TestFramework\Codeception;

use Infection\AbstractTestFramework\InvalidVersion;
use Infection\TestFramework\Codeception\Throwable\InvalidVersionFactory;
use InvalidArgumentException;
use function preg_match;

Expand All @@ -45,13 +47,19 @@ class VersionParser
{
private const VERSION_REGEX = '/(?<version>[0-9]+\.[0-9]+\.?[0-9]*)(?<prerelease>-[0-9a-zA-Z.]+)?(?<build>\+[0-9a-zA-Z.]+)?/';

/**
* @throws InvalidVersion
*/
public function parse(string $content): string
{
$matches = [];
$matched = preg_match(self::VERSION_REGEX, $content, $matches) > 0;

if (!$matched) {
throw new InvalidArgumentException('Parameter does not contain a valid SemVer (sub)string.');
throw InvalidVersionFactory::create(
CodeceptionAdapter::NAME,
$content,
);
}

return $matches[0];
Expand Down
19 changes: 8 additions & 11 deletions tests/phpunit/Adapter/VersionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

namespace Infection\Tests\TestFramework\Codeception\Adapter;

use Infection\AbstractTestFramework\InvalidVersion;
use Infection\TestFramework\Codeception\VersionParser;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

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

public function test_it_throws_exception_when_content_has_no_version_substring(): void
{
try {
$this->versionParser->parse('abc');

$this->fail();
} catch (InvalidArgumentException $exception) {
$this->assertSame(
'Parameter does not contain a valid SemVer (sub)string.',
$exception->getMessage(),
);
}
$this->expectExceptionObject(
new InvalidVersion(
'Could not recognise the test framework version for codeception for the value "abc".',
),
);

$this->versionParser->parse('abc');
}

/**
Expand Down
Loading