Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and fix the generic types for the ResultMockFactory #1459

Merged
merged 2 commits into from
Jun 23, 2023
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
15 changes: 9 additions & 6 deletions src/Core/src/Test/ResultMockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class ResultMockFactory
* ResultMockFactory::createFailing(SendEmailResponse::class, 400, 'invalid value');
* </code>
*
* @template T
* @template T of Result
*
* @psalm-param class-string<T> $class
*
* @return Result|T
* @return T
*/
public static function createFailing(
string $class,
Expand All @@ -49,6 +49,7 @@ public static function createFailing(
$client = new MockHttpClient($httpResponse);
$response = new Response($client->request('POST', 'http://localhost'), $client, new NullLogger());

/** @psalm-var \ReflectionClass<T> $reflectionClass */
$reflectionClass = new \ReflectionClass($class);

return $reflectionClass->newInstance($response);
Expand All @@ -61,11 +62,11 @@ public static function createFailing(
* ResultMockFactory::create(SendEmailResponse::class, ['MessageId'=>'foo123']);
* </code>
*
* @template T
* @template T of Result
*
* @psalm-param class-string<T> $class
*
* @return Result|T
* @return T
*/
public static function create(string $class, array $data = [])
{
Expand All @@ -83,6 +84,7 @@ public static function create(string $class, array $data = [])
$initializedProperty = $reflectionClass->getProperty('initialized');
$initializedProperty->setAccessible(true);

/** @psalm-var \ReflectionClass<T> $reflectionClass */
$reflectionClass = new \ReflectionClass($class);
$object = $reflectionClass->newInstance($response);
if (Result::class !== $class) {
Expand Down Expand Up @@ -124,11 +126,11 @@ public static function create(string $class, array $data = [])
/**
* Instantiate a Waiter class with a final state.
*
* @template T
* @template T of Waiter
*
* @psalm-param class-string<T> $class
*
* @return Result|T
* @return T
*/
public static function waiter(string $class, string $finalState)
{
Expand All @@ -152,6 +154,7 @@ public static function waiter(string $class, string $finalState)
$propertyState = $reflectionClass->getProperty('finalState');
$propertyState->setAccessible(true);

/** @psalm-var \ReflectionClass<T> $reflectionClass */
$reflectionClass = new \ReflectionClass($class);
$result = $reflectionClass->newInstanceWithoutConstructor();
$propertyResponse->setValue($result, $response);
Expand Down