Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StringMatchesFormatDescription with \r\n #3059

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/Framework/Constraint/StringMatchesFormatDescription.php
Expand Up @@ -29,22 +29,37 @@ public function __construct($string)
{
parent::__construct(
$this->createPatternFromFormat(
\preg_replace('/\r\n/', "\n", $string)
$this->convertNewlines($string)
)
);

$this->string = $string;
}

/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* @param mixed $other Value or object to evaluate.
*
* @return bool
*/
protected function matches($other): bool
{
return parent::matches(
$this->convertNewlines($other)
);
}

protected function failureDescription($other): string
{
return 'string matches format description';
}

protected function additionalFailureDescription($other): string
{
$from = \preg_split('(\r\n|\r|\n)', $this->string);
$to = \preg_split('(\r\n|\r|\n)', $other);
$from = \explode("\n", $this->string);
$to = \explode("\n", $this->convertNewlines($other));

foreach ($from as $index => $line) {
if (isset($to[$index]) && $line !== $to[$index]) {
Expand Down Expand Up @@ -100,4 +115,9 @@ private function createPatternFromFormat(string $string): string

return '/^' . $string . '$/s';
}

private function convertNewlines($text): string
{
return \preg_replace('/\r\n/', "\n", $text);
}
}
50 changes: 44 additions & 6 deletions tests/Framework/Constraint/StringMatchesFormatDescriptionTest.php
Expand Up @@ -10,9 +10,11 @@

namespace PHPUnit\Framework\Constraint;

use PHPUnit\Framework\ExpectationFailedException;

class StringMatchesFormatDescriptionTest extends ConstraintTestCase
{
public function testConstraintStringMatches(): void
public function testConstraintStringMatchesCharacter(): void
{
$constraint = new StringMatchesFormatDescription('*%c*');

Expand All @@ -22,7 +24,7 @@ public function testConstraintStringMatches(): void
$this->assertCount(1, $constraint);
}

public function testConstraintStringMatches2(): void
public function testConstraintStringMatchesString(): void
{
$constraint = new StringMatchesFormatDescription('*%s*');

Expand All @@ -32,7 +34,7 @@ public function testConstraintStringMatches2(): void
$this->assertCount(1, $constraint);
}

public function testConstraintStringMatches3(): void
public function testConstraintStringMatchesInteger(): void
{
$constraint = new StringMatchesFormatDescription('*%i*');

Expand All @@ -42,7 +44,7 @@ public function testConstraintStringMatches3(): void
$this->assertCount(1, $constraint);
}

public function testConstraintStringMatches4(): void
public function testConstraintStringMatchesUnsignedInt(): void
{
$constraint = new StringMatchesFormatDescription('*%d*');

Expand All @@ -52,7 +54,7 @@ public function testConstraintStringMatches4(): void
$this->assertCount(1, $constraint);
}

public function testConstraintStringMatches5(): void
public function testConstraintStringMatchesHexadecimal(): void
{
$constraint = new StringMatchesFormatDescription('*%x*');

Expand All @@ -62,7 +64,7 @@ public function testConstraintStringMatches5(): void
$this->assertCount(1, $constraint);
}

public function testConstraintStringMatches6(): void
public function testConstraintStringMatchesFloat(): void
{
$constraint = new StringMatchesFormatDescription('*%f*');

Expand All @@ -71,4 +73,40 @@ public function testConstraintStringMatches6(): void
$this->assertEquals('matches PCRE pattern "/^\*[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?\*$/s"', $constraint->toString());
$this->assertCount(1, $constraint);
}

public function testConstraintStringMatchesNewline(): void
{
$constraint = new StringMatchesFormatDescription("\r\n");

$this->assertFalse($constraint->evaluate("*\r\n", '', true));
$this->assertTrue($constraint->evaluate("\r\n", '', true));
$this->assertEquals("matches PCRE pattern \"/^\n$/s\"", $constraint->toString());
$this->assertCount(1, $constraint);
}

public function testFailureMessageWithNewlines(): void
{
$constraint = new StringMatchesFormatDescription("%c\nfoo\n%c");

try
{
$constraint->evaluate("*\nbar\n*");
$this->fail('Expected ExpectationFailedException, but it was not thrown.');
}
catch (ExpectationFailedException $e)
{
$expected = <<<EOD
Failed asserting that string matches format description.
--- Expected
+++ Actual
@@ @@
*
-foo
+bar
*

EOD;
$this->assertEquals($expected, $e->getMessage());
}
}
}