From 71925c4316dbf836260e821dde84c42ef8644c41 Mon Sep 17 00:00:00 2001 From: jeff Date: Sat, 3 Oct 2020 20:40:45 +0000 Subject: [PATCH] Add tests for LogicalNot::negate, fixes to negate regex --- src/Framework/Constraint/LogicalNot.php | 2 +- .../Framework/Constraint/LogicalNotTest.php | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/Framework/Constraint/LogicalNot.php b/src/Framework/Constraint/LogicalNot.php index 0822863a403..873c5fd22c3 100644 --- a/src/Framework/Constraint/LogicalNot.php +++ b/src/Framework/Constraint/LogicalNot.php @@ -49,7 +49,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]; diff --git a/tests/unit/Framework/Constraint/LogicalNotTest.php b/tests/unit/Framework/Constraint/LogicalNotTest.php index 0560ae2d6a6..35b8d93ff25 100644 --- a/tests/unit/Framework/Constraint/LogicalNotTest.php +++ b/tests/unit/Framework/Constraint/LogicalNotTest.php @@ -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 { @@ -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( + <<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( + <<fail(); + } }