Skip to content

Commit 1190ba5

Browse files
authored
[Core] Add support for Waiter in ResultMockFactory (#504)
* Add support for Waiter in ResultMockFactory * minor
1 parent d894459 commit 1190ba5

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

src/Test/ResultMockFactory.php

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use AsyncAws\Core\Response;
88
use AsyncAws\Core\Result;
99
use AsyncAws\Core\Test\Http\SimpleMockedResponse;
10+
use AsyncAws\Core\Waiter;
1011
use Symfony\Component\HttpClient\MockHttpClient;
1112

1213
/**
@@ -67,11 +68,7 @@ public static function create(string $class, array $data = [])
6768
}
6869
}
6970

70-
$reflectionClass = new \ReflectionClass(Response::class);
71-
$response = $reflectionClass->newInstanceWithoutConstructor();
72-
$property = $reflectionClass->getProperty('resolveResult');
73-
$property->setAccessible(true);
74-
$property->setValue($response, true);
71+
$response = self::getResponseObject();
7572

7673
// Make sure the Result is initialized
7774
$reflectionClass = new \ReflectionClass(Result::class);
@@ -96,6 +93,44 @@ public static function create(string $class, array $data = [])
9693
return $object;
9794
}
9895

96+
/**
97+
* Instantiate a Waiter class with a final state.
98+
*
99+
* @template T
100+
* @psalm-param class-string<T> $class
101+
*
102+
* @return Result|T
103+
*/
104+
public static function waiter(string $class, string $finalState)
105+
{
106+
if (Result::class !== $class) {
107+
$parent = get_parent_class($class);
108+
if (false === $parent || Waiter::class !== $parent) {
109+
throw new \LogicException(sprintf('The "%s::%s" can only be used for classes that extend "%s"', __CLASS__, __METHOD__, Waiter::class));
110+
}
111+
}
112+
113+
if (Waiter::STATE_SUCCESS !== $finalState && Waiter::STATE_FAILURE !== $finalState) {
114+
throw new \LogicException(sprintf('The state passed to "%s::%s" must be "%s" or "%s".', __CLASS__, __METHOD__, Waiter::STATE_SUCCESS, Waiter::STATE_FAILURE));
115+
}
116+
117+
$response = self::getResponseObject();
118+
119+
$reflectionClass = new \ReflectionClass(Waiter::class);
120+
$propertyResponse = $reflectionClass->getProperty('response');
121+
$propertyResponse->setAccessible(true);
122+
123+
$propertyState = $reflectionClass->getProperty('finalState');
124+
$propertyState->setAccessible(true);
125+
126+
$reflectionClass = new \ReflectionClass($class);
127+
$result = $reflectionClass->newInstanceWithoutConstructor();
128+
$propertyResponse->setValue($result, $response);
129+
$propertyState->setValue($result, $finalState);
130+
131+
return $result;
132+
}
133+
99134
/**
100135
* Try to add some values to the properties not defined in $data.
101136
*
@@ -199,4 +234,15 @@ private static function addPropertiesOnResult(\ReflectionClass $reflectionClass,
199234
$property->setValue($object, $inputMock);
200235
}
201236
}
237+
238+
private static function getResponseObject(): Response
239+
{
240+
$reflectionClass = new \ReflectionClass(Response::class);
241+
$response = $reflectionClass->newInstanceWithoutConstructor();
242+
$property = $reflectionClass->getProperty('resolveResult');
243+
$property->setAccessible(true);
244+
$property->setValue($response, true);
245+
246+
return $response;
247+
}
202248
}

0 commit comments

Comments
 (0)