From 432ff221c67c2ba8e379e0d0bde74795f41c4f29 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 23 Sep 2024 14:08:21 +0100 Subject: [PATCH] fix: missing `!=` and `!==` on new `toUseStrictEquality` arch expectation --- src/Expectation.php | 6 +++--- src/Expectations/OppositeExpectation.php | 4 ++-- src/Logging/Converter.php | 2 +- src/Support/Closure.php | 2 +- .../Fixtures/Arch/ToUseStrictEquality/NotStrictEquality.php | 4 ++++ tests/Fixtures/Arch/ToUseStrictEquality/StrictEquality.php | 4 ++++ 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index ec48ed4f..3cdc43b8 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -380,7 +380,7 @@ private function getExpectationClosure(string $name): Closure if (self::hasExtend($name)) { $extend = self::$extends[$name]->bindTo($this, Expectation::class); - if ($extend != false) { + if ($extend != false) { // @pest-arch-ignore-line return $extend; } } @@ -522,9 +522,9 @@ public function toUseStrictEquality(): ArchExpectation { return Targeted::make( $this, - fn (ObjectDescription $object): bool => ! str_contains((string) file_get_contents($object->path), ' == '), // @pest-arch-ignore-line + fn (ObjectDescription $object): bool => ! str_contains((string) file_get_contents($object->path), ' == ') && ! str_contains((string) file_get_contents($object->path), ' != '), 'to use strict equality', - FileLineFinder::where(fn (string $line): bool => str_contains($line, ' == ')), + FileLineFinder::where(fn (string $line): bool => str_contains($line, ' == ') || str_contains($line, ' != ')), ); } diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index ff1810ad..ea7328b9 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -159,9 +159,9 @@ public function toUseStrictEquality(): ArchExpectation { return Targeted::make( $this->original, - fn (ObjectDescription $object): bool => ! str_contains((string) file_get_contents($object->path), ' === '), + fn (ObjectDescription $object): bool => ! str_contains((string) file_get_contents($object->path), ' === ') && ! str_contains((string) file_get_contents($object->path), ' !== '), 'to use strict equality', - FileLineFinder::where(fn (string $line): bool => str_contains($line, ' === ')), + FileLineFinder::where(fn (string $line): bool => str_contains($line, ' === ') || str_contains($line, ' !== ')), ); } diff --git a/src/Logging/Converter.php b/src/Logging/Converter.php index 1c98bd93..5ac17a5a 100644 --- a/src/Logging/Converter.php +++ b/src/Logging/Converter.php @@ -150,7 +150,7 @@ public function getTestSuiteName(TestSuite $testSuite): string { if ($testSuite instanceof TestSuiteForTestMethodWithDataProvider) { $firstTest = $this->getFirstTest($testSuite); - if ($firstTest != null) { + if ($firstTest instanceof \PHPUnit\Event\Code\TestMethod) { return $this->getTestMethodNameWithoutDatasetSuffix($firstTest); } } diff --git a/src/Support/Closure.php b/src/Support/Closure.php index 2ebbd88b..e96ec29e 100644 --- a/src/Support/Closure.php +++ b/src/Support/Closure.php @@ -26,7 +26,7 @@ public static function bind(?BaseClosure $closure, ?object $newThis, object|stri $closure = BaseClosure::bind($closure, $newThis, $newScope); - if ($closure === null) { + if (! $closure instanceof \Closure) { throw ShouldNotHappen::fromMessage('Could not bind closure.'); } diff --git a/tests/Fixtures/Arch/ToUseStrictEquality/NotStrictEquality.php b/tests/Fixtures/Arch/ToUseStrictEquality/NotStrictEquality.php index d35c9e48..ad692142 100644 --- a/tests/Fixtures/Arch/ToUseStrictEquality/NotStrictEquality.php +++ b/tests/Fixtures/Arch/ToUseStrictEquality/NotStrictEquality.php @@ -14,5 +14,9 @@ public function test(): void if ($a == $b) { echo 'Equal'; } + + if ($a != $b) { + echo 'Equal'; + } } } diff --git a/tests/Fixtures/Arch/ToUseStrictEquality/StrictEquality.php b/tests/Fixtures/Arch/ToUseStrictEquality/StrictEquality.php index 68646b73..f97f233c 100644 --- a/tests/Fixtures/Arch/ToUseStrictEquality/StrictEquality.php +++ b/tests/Fixtures/Arch/ToUseStrictEquality/StrictEquality.php @@ -14,5 +14,9 @@ public function test(): void if ($a === $b) { echo 'Equal'; } + + if ($a !== $b) { + echo 'Equal'; + } } }