Skip to content

Commit

Permalink
Escape percent sign in Assert::notSame message (#1690)
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa committed May 11, 2022
1 parent ffb87ca commit 134853c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/TestFramework/VersionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

use function Safe\preg_match;
use function Safe\sprintf;
use function str_replace;
use Webmozart\Assert\Assert;

/**
Expand All @@ -54,7 +55,7 @@ public function parse(string $content): string
Assert::notSame(
$matched,
0,
sprintf('Expected "%s" to be contain a valid SemVer (sub)string value.', $content)
sprintf('Expected "%s" to be contain a valid SemVer (sub)string value.', str_replace('%', '%%', $content))
);

return $matches[0];
Expand Down
17 changes: 14 additions & 3 deletions tests/phpunit/TestFramework/VersionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Infection\TestFramework\VersionParser;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use function Safe\sprintf;

final class VersionParserTest extends TestCase
{
Expand All @@ -61,15 +62,18 @@ public function test_it_parses_version_from_string(string $content, string $expe
$this->assertSame($expectedVersion, $result);
}

public function test_it_throws_exception_when_content_has_no_version_substring(): void
/**
* @dataProvider invalidVersionProvider
*/
public function test_it_throws_exception_when_content_has_no_version_substring(string $content): void
{
try {
$this->versionParser->parse('abc');
$this->versionParser->parse($content);

$this->fail();
} catch (InvalidArgumentException $exception) {
$this->assertSame(
'Expected "abc" to be contain a valid SemVer (sub)string value.',
sprintf('Expected "%s" to be contain a valid SemVer (sub)string value.', $content),
$exception->getMessage()
);
}
Expand Down Expand Up @@ -111,4 +115,11 @@ public function versionProvider(): iterable

yield 'PHPUnit' => ['PHPUnit 7.5.11 by Sebastian Bergmann and contributors.', '7.5.11'];
}

public function invalidVersionProvider(): iterable
{
yield 'ascii-only string' => ['abc'];

yield 'with percent sign' => ['%~'];
}
}

0 comments on commit 134853c

Please sign in to comment.