Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions src/ServiceMockHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $definedParameters
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -434,6 +404,41 @@ 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<T> $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);
assert($double instanceof $type);

return $double;
}

/**
* Fetch a mock for one of the dependencies of a service created by the trait.
*
Expand Down
Loading