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 for #4368: Add tests for LogicalNot::negate, fixes to negate regex #4479

Closed
wants to merge 2 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Framework/Constraint/LogicalNot.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function negate(string $string): string
'not ',
];

preg_match('/(\'[\w\W]*\')([\w\W]*)("[\w\W]*")/i', $string, $matches);
preg_match('/(^\'[\w\W]*\')?([\w\W]*)(\'[\w\W]*\')$/i', $string, $matches);

if (count($matches) > 0) {
$nonInput = $matches[2];
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/Framework/Constraint/LogicalNotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
*/
namespace PHPUnit\Framework\Constraint;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestFailure;

class LogicalNotTest extends TestCase
{
Expand All @@ -19,4 +22,66 @@ public function testNonRestrictedConstructParameterIsTreatedAsIsEqual(): void

$this->assertSame('is not equal to \'test\'', $constraint->toString());
}

public function testConstraintIsNotEqualStringContainsDoubleQuotes(): void
{
$string = 'a "b" c';
$other = 'a ""b"" c';
$constraint = Assert::logicalNot(
Assert::equalTo($string)
);

$this->assertTrue($constraint->evaluate($other, '', true));
$this->assertFalse($constraint->evaluate($string, '', true));
$this->assertEquals("is not equal to '{$string}'", $constraint->toString());
$this->assertCount(1, $constraint);

try {
$constraint->evaluate($string);
} catch (ExpectationFailedException $e) {
$this->assertEquals(
<<<EOF
Failed asserting that '{$string}' is not equal to '{$string}'.

EOF
,
TestFailure::exceptionToString($e) // Fails here
);

return;
}

$this->fail();
}

public function testConstraintIsNotEqualStringContainsPositiveWords(): void
{
$string = 'a is b';
$other = 'a certainly is b';
$constraint = Assert::logicalNot(
Assert::equalTo($string)
);

$this->assertTrue($constraint->evaluate($other, '', true));
$this->assertFalse($constraint->evaluate($string, '', true));
$this->assertEquals("is not equal to '{$string}'", $constraint->toString()); // Fails here
$this->assertCount(1, $constraint);

try {
$constraint->evaluate($string);
} catch (ExpectationFailedException $e) {
$this->assertEquals(
<<<EOF
Failed asserting that '{$string}' is not equal to '{$string}'.

EOF
,
TestFailure::exceptionToString($e)
);

return;
}

$this->fail();
}
}