From 38de168e1a1f02fda51b31c62d0917c42e17dff6 Mon Sep 17 00:00:00 2001 From: pkly Date: Fri, 18 Sep 2026 10:02:39 +0200 Subject: [PATCH 1/2] Update method to protected --- src/ServiceMockHelperTrait.php | 64 ++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/ServiceMockHelperTrait.php b/src/ServiceMockHelperTrait.php index 7ee6339..459102a 100644 --- a/src/ServiceMockHelperTrait.php +++ b/src/ServiceMockHelperTrait.php @@ -291,36 +291,6 @@ private function __generateReturnValues(): bool ->isEmpty(); } - /** - * Create a test double that is deliberately NOT registered with the TestCase. - * - * It is a full MockObject, so expects() is available on it, but PHPUnit neither verifies it nor - * complains about it having no expectations. It is registered later, on the first - * getMockedService() call for it, which is the point at which the test takes ownership of it. - * - * @param class-string $type - */ - private function __createUnregisteredMock( - string $type - ): MockObject { - // arguments are passed positionally on purpose: PHPUnit marks its API as - // @no-named-arguments, so parameter names are not covered by its BC promise - $double = new MockGenerator()->testDouble( - $type, - true, // $mockObject - [], // $methods - [], // $arguments - '', // $mockClassName - false, // $callOriginalConstructor - false, // $callOriginalClone - $this->__generateReturnValues(), // $returnValueGeneration - ); - - assert($double instanceof MockObject); - - return $double; - } - /** * @param class-string $class * @param array $definedParameters @@ -400,7 +370,7 @@ private function __resolveMethodParameters( } $key = $this->__doubleKey($typeName, $name); - $double = $state['doubles'][$key] ??= $this->__createUnregisteredMock($typeName); + $double = $state['doubles'][$key] ??= $this->createUnregisteredMock($typeName); $state['parameters'][] = [ 'name' => $name, @@ -434,6 +404,38 @@ private function __getRequiredMethods( return $methods; } + /** + * Create a test double that is deliberately NOT registered with the TestCase. + * + * It is a full MockObject, so expects() is available on it, but PHPUnit neither verifies it nor + * complains about it having no expectations. It is registered later, on the first + * getMockedService() call for it, which is the point at which the test takes ownership of it. + * + * @template T of object + * @param class-string $type + * @return T&MockObject + */ + protected function createUnregisteredMock( + string $type + ): MockObject { + // arguments are passed positionally on purpose: PHPUnit marks its API as + // @no-named-arguments, so parameter names are not covered by its BC promise + $double = new MockGenerator()->testDouble( + $type, + true, // $mockObject + [], // $methods + [], // $arguments + '', // $mockClassName + false, // $callOriginalConstructor + false, // $callOriginalClone + $this->__generateReturnValues(), // $returnValueGeneration + ); + + assert($double instanceof MockObject); + + return $double; + } + /** * Fetch a mock for one of the dependencies of a service created by the trait. * From 7b775f0482960126a9e7d31f3bc478edab5412b2 Mon Sep 17 00:00:00 2001 From: pkly Date: Fri, 18 Sep 2026 10:05:31 +0200 Subject: [PATCH 2/2] Add extra assert --- src/ServiceMockHelperTrait.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ServiceMockHelperTrait.php b/src/ServiceMockHelperTrait.php index 459102a..84bc96d 100644 --- a/src/ServiceMockHelperTrait.php +++ b/src/ServiceMockHelperTrait.php @@ -412,7 +412,9 @@ private function __getRequiredMethods( * getMockedService() call for it, which is the point at which the test takes ownership of it. * * @template T of object + * * @param class-string $type + * * @return T&MockObject */ protected function createUnregisteredMock( @@ -432,6 +434,7 @@ protected function createUnregisteredMock( ); assert($double instanceof MockObject); + assert($double instanceof $type); return $double; }