From 6ad38c079b82bd799576125164d0906f3a0d6e41 Mon Sep 17 00:00:00 2001 From: Marko Petrovic Date: Fri, 12 Jan 2024 16:32:57 +0100 Subject: [PATCH 1/8] add test case to prove issue --- .../unit/Framework/Assert/AssertNotEqualsTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/unit/Framework/Assert/AssertNotEqualsTest.php diff --git a/tests/unit/Framework/Assert/AssertNotEqualsTest.php b/tests/unit/Framework/Assert/AssertNotEqualsTest.php new file mode 100644 index 00000000000..74880bf156f --- /dev/null +++ b/tests/unit/Framework/Assert/AssertNotEqualsTest.php @@ -0,0 +1,14 @@ +assertNotEquals($b, $a); + } +} From f8dd0abed86524e4fa25b22ae86ac259e02f5382 Mon Sep 17 00:00:00 2001 From: Marko Petrovic Date: Fri, 12 Jan 2024 18:22:27 +0100 Subject: [PATCH 2/8] better test --- tests/unit/Framework/Assert/AssertNotEqualsTest.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/unit/Framework/Assert/AssertNotEqualsTest.php b/tests/unit/Framework/Assert/AssertNotEqualsTest.php index 74880bf156f..499d3f7b344 100644 --- a/tests/unit/Framework/Assert/AssertNotEqualsTest.php +++ b/tests/unit/Framework/Assert/AssertNotEqualsTest.php @@ -2,13 +2,22 @@ namespace PHPUnit\Framework; +use PHPUnit\Framework\Constraint\IsEqual; +use PHPUnit\Framework\Constraint\LogicalNot; + class AssertNotEqualsTest extends TestCase { public function testConfusingMessagesForLogicalNot() { + $expectedMessage = "Failed asserting that 'test contains something' is not equal to 'test contains something'."; $a = 'test contains something'; $b = 'test contains something'; - - $this->assertNotEquals($b, $a); + $constraint = new LogicalNot(new IsEqual($a)); + try { + Assert::assertThat($b, $constraint); + } catch (\Exception $e) { + $actualMessage = $e->getMessage(); + $this->assertSame($expectedMessage, $actualMessage); + } } } From a400f5d017b92d1c1e19111d4444983f176879ef Mon Sep 17 00:00:00 2001 From: Marko Petrovic Date: Fri, 12 Jan 2024 18:42:31 +0100 Subject: [PATCH 3/8] add better test --- src/Framework/Constraint/Operator/LogicalNot.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Framework/Constraint/Operator/LogicalNot.php b/src/Framework/Constraint/Operator/LogicalNot.php index 327f85bfe07..86635a1ea51 100644 --- a/src/Framework/Constraint/Operator/LogicalNot.php +++ b/src/Framework/Constraint/Operator/LogicalNot.php @@ -49,6 +49,9 @@ public static function negate(string $string): string ]; preg_match('/(\'[\w\W]*\')([\w\W]*)("[\w\W]*")/i', $string, $matches); + if (count($matches) === 0) { + preg_match('/(\'[\w\W]*\')([\w\W]*)(\'[\w\W]*\')/i', $string, $matches); + } $positives = array_map(static function (string $s) { From 3802132bf00eb177a375173d38fe62be592cdccf Mon Sep 17 00:00:00 2001 From: Marko Petrovic Date: Fri, 12 Jan 2024 18:48:40 +0100 Subject: [PATCH 4/8] run psalm and cs-fixer --- .../Constraint/Operator/LogicalNot.php | 1 + .../Framework/Assert/AssertNotEqualsTest.php | 23 +++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Framework/Constraint/Operator/LogicalNot.php b/src/Framework/Constraint/Operator/LogicalNot.php index 86635a1ea51..aca799c3d79 100644 --- a/src/Framework/Constraint/Operator/LogicalNot.php +++ b/src/Framework/Constraint/Operator/LogicalNot.php @@ -49,6 +49,7 @@ public static function negate(string $string): string ]; preg_match('/(\'[\w\W]*\')([\w\W]*)("[\w\W]*")/i', $string, $matches); + if (count($matches) === 0) { preg_match('/(\'[\w\W]*\')([\w\W]*)(\'[\w\W]*\')/i', $string, $matches); } diff --git a/tests/unit/Framework/Assert/AssertNotEqualsTest.php b/tests/unit/Framework/Assert/AssertNotEqualsTest.php index 499d3f7b344..45466e0aff2 100644 --- a/tests/unit/Framework/Assert/AssertNotEqualsTest.php +++ b/tests/unit/Framework/Assert/AssertNotEqualsTest.php @@ -1,21 +1,30 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace PHPUnit\Framework; +use Exception; use PHPUnit\Framework\Constraint\IsEqual; use PHPUnit\Framework\Constraint\LogicalNot; class AssertNotEqualsTest extends TestCase { - public function testConfusingMessagesForLogicalNot() + public function testConfusingMessagesForLogicalNot(): void { $expectedMessage = "Failed asserting that 'test contains something' is not equal to 'test contains something'."; - $a = 'test contains something'; - $b = 'test contains something'; - $constraint = new LogicalNot(new IsEqual($a)); + $a = 'test contains something'; + $b = 'test contains something'; + $constraint = new LogicalNot(new IsEqual($a)); + try { Assert::assertThat($b, $constraint); - } catch (\Exception $e) { + } catch (Exception $e) { $actualMessage = $e->getMessage(); $this->assertSame($expectedMessage, $actualMessage); } From 89e52f414ac3707ee66e0589f2f02888dcb16a8f Mon Sep 17 00:00:00 2001 From: Marko Petrovic Date: Fri, 12 Jan 2024 19:04:38 +0100 Subject: [PATCH 5/8] run psalm and cs-fixer --- src/Framework/Constraint/Operator/LogicalNot.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Framework/Constraint/Operator/LogicalNot.php b/src/Framework/Constraint/Operator/LogicalNot.php index aca799c3d79..f52ba6f84c4 100644 --- a/src/Framework/Constraint/Operator/LogicalNot.php +++ b/src/Framework/Constraint/Operator/LogicalNot.php @@ -14,6 +14,7 @@ use function preg_match; use function preg_quote; use function preg_replace; +use PHPUnit\Framework\ExpectationFailedException; /** * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit @@ -105,8 +106,10 @@ public function precedence(): int * constraint is met, false otherwise. * * @param mixed $other value or object to evaluate + * + * @throws ExpectationFailedException */ - protected function matches($other): bool + protected function matches(mixed $other): bool { return !$this->constraint()->evaluate($other, '', true); } From 3cc417f7f13c090530205e1535e2302179ac204c Mon Sep 17 00:00:00 2001 From: Marko Petrovic Date: Fri, 12 Jan 2024 19:09:48 +0100 Subject: [PATCH 6/8] revert accidental change --- src/Framework/Constraint/Operator/LogicalNot.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Framework/Constraint/Operator/LogicalNot.php b/src/Framework/Constraint/Operator/LogicalNot.php index f52ba6f84c4..de324a9d0b1 100644 --- a/src/Framework/Constraint/Operator/LogicalNot.php +++ b/src/Framework/Constraint/Operator/LogicalNot.php @@ -55,10 +55,10 @@ public static function negate(string $string): string preg_match('/(\'[\w\W]*\')([\w\W]*)(\'[\w\W]*\')/i', $string, $matches); } - $positives = array_map(static function (string $s) - { - return '/\\b' . preg_quote($s, '/') . '/'; - }, $positives); + $positives = array_map( + static fn (string $s) => '/\\b' . preg_quote($s, '/') . '/', + $positives, + ); if (count($matches) > 0) { $nonInput = $matches[2]; From 3332238cbfba8047ffd8ea115876a8cbe8d3a5e9 Mon Sep 17 00:00:00 2001 From: Marko Petrovic Date: Fri, 12 Jan 2024 19:22:48 +0100 Subject: [PATCH 7/8] merge into main --- src/Framework/Constraint/Constraint.php | 2 +- src/Framework/Constraint/Operator/LogicalNot.php | 2 +- src/Framework/Constraint/Operator/Operator.php | 2 +- src/Framework/Constraint/Operator/UnaryOperator.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Framework/Constraint/Constraint.php b/src/Framework/Constraint/Constraint.php index 31a26f6b87f..7702281a4f8 100644 --- a/src/Framework/Constraint/Constraint.php +++ b/src/Framework/Constraint/Constraint.php @@ -21,7 +21,7 @@ /** * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit */ -abstract readonly class Constraint implements Countable, SelfDescribing +abstract class Constraint implements Countable, SelfDescribing { /** * Evaluates the constraint for parameter $other. diff --git a/src/Framework/Constraint/Operator/LogicalNot.php b/src/Framework/Constraint/Operator/LogicalNot.php index 91da7e271cd..de324a9d0b1 100644 --- a/src/Framework/Constraint/Operator/LogicalNot.php +++ b/src/Framework/Constraint/Operator/LogicalNot.php @@ -19,7 +19,7 @@ /** * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit */ -final readonly class LogicalNot extends UnaryOperator +final class LogicalNot extends UnaryOperator { public static function negate(string $string): string { diff --git a/src/Framework/Constraint/Operator/Operator.php b/src/Framework/Constraint/Operator/Operator.php index 409766531c1..1195156e036 100644 --- a/src/Framework/Constraint/Operator/Operator.php +++ b/src/Framework/Constraint/Operator/Operator.php @@ -12,7 +12,7 @@ /** * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit */ -abstract readonly class Operator extends Constraint +abstract class Operator extends Constraint { /** * Returns the name of this operator. diff --git a/src/Framework/Constraint/Operator/UnaryOperator.php b/src/Framework/Constraint/Operator/UnaryOperator.php index 99c29f5e08e..afa62815c5e 100644 --- a/src/Framework/Constraint/Operator/UnaryOperator.php +++ b/src/Framework/Constraint/Operator/UnaryOperator.php @@ -14,7 +14,7 @@ /** * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit */ -abstract readonly class UnaryOperator extends Operator +abstract class UnaryOperator extends Operator { private Constraint $constraint; From 6622bfdd137ab9f902a14f58e0b0db05dbfebf75 Mon Sep 17 00:00:00 2001 From: Marko Petrovic Date: Fri, 12 Jan 2024 19:26:47 +0100 Subject: [PATCH 8/8] revert --- src/Framework/Constraint/Constraint.php | 2 +- src/Framework/Constraint/Operator/LogicalNot.php | 2 +- src/Framework/Constraint/Operator/Operator.php | 2 +- src/Framework/Constraint/Operator/UnaryOperator.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Framework/Constraint/Constraint.php b/src/Framework/Constraint/Constraint.php index 7702281a4f8..31a26f6b87f 100644 --- a/src/Framework/Constraint/Constraint.php +++ b/src/Framework/Constraint/Constraint.php @@ -21,7 +21,7 @@ /** * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit */ -abstract class Constraint implements Countable, SelfDescribing +abstract readonly class Constraint implements Countable, SelfDescribing { /** * Evaluates the constraint for parameter $other. diff --git a/src/Framework/Constraint/Operator/LogicalNot.php b/src/Framework/Constraint/Operator/LogicalNot.php index de324a9d0b1..91da7e271cd 100644 --- a/src/Framework/Constraint/Operator/LogicalNot.php +++ b/src/Framework/Constraint/Operator/LogicalNot.php @@ -19,7 +19,7 @@ /** * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit */ -final class LogicalNot extends UnaryOperator +final readonly class LogicalNot extends UnaryOperator { public static function negate(string $string): string { diff --git a/src/Framework/Constraint/Operator/Operator.php b/src/Framework/Constraint/Operator/Operator.php index 1195156e036..409766531c1 100644 --- a/src/Framework/Constraint/Operator/Operator.php +++ b/src/Framework/Constraint/Operator/Operator.php @@ -12,7 +12,7 @@ /** * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit */ -abstract class Operator extends Constraint +abstract readonly class Operator extends Constraint { /** * Returns the name of this operator. diff --git a/src/Framework/Constraint/Operator/UnaryOperator.php b/src/Framework/Constraint/Operator/UnaryOperator.php index afa62815c5e..99c29f5e08e 100644 --- a/src/Framework/Constraint/Operator/UnaryOperator.php +++ b/src/Framework/Constraint/Operator/UnaryOperator.php @@ -14,7 +14,7 @@ /** * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit */ -abstract class UnaryOperator extends Operator +abstract readonly class UnaryOperator extends Operator { private Constraint $constraint;