From aacd874ebe5cedfa489e4c4971eb22d1cbb21ff7 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 9 Aug 2024 00:38:16 +0100 Subject: [PATCH] fix --- src/Expectations/OppositeExpectation.php | 25 ++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index f424f373..47641e8f 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -24,6 +24,7 @@ use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\ExpectationFailedException; use ReflectionMethod; use ReflectionProperty; +use stdClass; /** * @internal @@ -233,15 +234,19 @@ final class OppositeExpectation { $methods = is_array($methods) ? $methods : [$methods]; + $state = new stdClass(); + return Targeted::make( $this->original, - function (ObjectDescription $object) use ($methods): bool { + function (ObjectDescription $object) use ($methods, &$state): bool { $reflectionMethods = isset($object->reflectionClass) ? Reflection::getMethodsFromReflectionClass($object->reflectionClass, ReflectionMethod::IS_PUBLIC) : []; foreach ($reflectionMethods as $reflectionMethod) { if (! in_array($reflectionMethod->name, $methods, true)) { + $state->contains = 'public function '.$reflectionMethod->name; + return false; } } @@ -251,7 +256,7 @@ final class OppositeExpectation $methods === [] ? 'not to have public methods' : sprintf("not to have public methods besides '%s'", implode("', '", $methods)), - FileLineFinder::where(fn (string $line): bool => str_contains($line, 'public function')), + FileLineFinder::where(fn (string $line): bool => str_contains($line, $state->contains)), ); } @@ -264,15 +269,19 @@ final class OppositeExpectation { $methods = is_array($methods) ? $methods : [$methods]; + $state = new stdClass(); + return Targeted::make( $this->original, - function (ObjectDescription $object) use ($methods): bool { + function (ObjectDescription $object) use ($methods, &$state): bool { $reflectionMethods = isset($object->reflectionClass) ? Reflection::getMethodsFromReflectionClass($object->reflectionClass, ReflectionMethod::IS_PROTECTED) : []; foreach ($reflectionMethods as $reflectionMethod) { if (! in_array($reflectionMethod->name, $methods, true)) { + $state->contains = 'protected function '.$reflectionMethod->name; + return false; } } @@ -282,7 +291,7 @@ final class OppositeExpectation $methods === [] ? 'not to have protected methods' : sprintf("not to have protected methods besides '%s'", implode("', '", $methods)), - FileLineFinder::where(fn (string $line): bool => str_contains($line, 'protected function')), + FileLineFinder::where(fn (string $line): bool => str_contains($line, $state->contains)), ); } @@ -295,15 +304,19 @@ final class OppositeExpectation { $methods = is_array($methods) ? $methods : [$methods]; + $state = new stdClass(); + return Targeted::make( $this->original, - function (ObjectDescription $object) use ($methods): bool { + function (ObjectDescription $object) use ($methods, &$state): bool { $reflectionMethods = isset($object->reflectionClass) ? Reflection::getMethodsFromReflectionClass($object->reflectionClass, ReflectionMethod::IS_PRIVATE) : []; foreach ($reflectionMethods as $reflectionMethod) { if (! in_array($reflectionMethod->name, $methods, true)) { + $state->contains = 'private function '.$reflectionMethod->name; + return false; } } @@ -313,7 +326,7 @@ final class OppositeExpectation $methods === [] ? 'not to have private methods' : sprintf("not to have private methods besides '%s'", implode("', '", $methods)), - FileLineFinder::where(fn (string $line): bool => str_contains($line, 'private function')), + FileLineFinder::where(fn (string $line): bool => str_contains($line, $state->contains)), ); }