Skip to content

Commit ab9b111

Browse files
jderussecseiller
andauthored
Improve ResultMockFactory (#342)
* Improve ResultMock * Update src/Core/src/Test/ResultMockFactory.php Co-Authored-By: Clement Seiller <[email protected]> Co-authored-by: Clement Seiller <[email protected]>
1 parent eb3df65 commit ab9b111

File tree

2 files changed

+87
-11
lines changed

2 files changed

+87
-11
lines changed

src/Test/ResultMockFactory.php

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace AsyncAws\Core\Test;
66

7+
use AsyncAws\Core\Response;
78
use AsyncAws\Core\Result;
9+
use AsyncAws\Core\Test\Http\SimpleMockedResponse;
10+
use Symfony\Component\HttpClient\MockHttpClient;
811

912
/**
1013
* An easy way to create Result objects for your tests.
@@ -13,6 +16,36 @@
1316
*/
1417
class ResultMockFactory
1518
{
19+
/**
20+
* Instantiate a Result class that throws exception.
21+
*
22+
* <code>
23+
* ResultMockFactory::createFailing(SendEmailResponse::class, 400, 'invalid value');
24+
* </code>
25+
*
26+
* @template T
27+
* @psalm-param class-string<T> $class
28+
*
29+
* @return Result|T
30+
*/
31+
public static function createFailing(string $class, int $code, ?string $message = null)
32+
{
33+
if (Result::class !== $class) {
34+
$parent = get_parent_class($class);
35+
if (false === $parent || Result::class !== $parent) {
36+
throw new \LogicException(sprintf('The "%s::%s" can only be used for classes that extend "%s"', __CLASS__, __METHOD__, Result::class));
37+
}
38+
}
39+
40+
$httpResponse = new SimpleMockedResponse(\json_encode(['message' => $message]), ['content-type' => 'application/json'], $code);
41+
$client = new MockHttpClient($httpResponse);
42+
$response = new Response($client->request('POST', 'http://localhost'), $client);
43+
44+
$reflectionClass = new \ReflectionClass($class);
45+
46+
return $reflectionClass->newInstance($response);
47+
}
48+
1649
/**
1750
* Instantiate a Result class with some data.
1851
*
@@ -23,26 +56,34 @@ class ResultMockFactory
2356
* @template T
2457
* @psalm-param class-string<T> $class
2558
*
26-
* @return T
59+
* @return Result|T
2760
*/
2861
public static function create(string $class, array $data = [])
2962
{
30-
$parent = get_parent_class($class);
31-
if (false === $parent || Result::class !== $parent) {
32-
throw new \LogicException(sprintf('The "%s::%s" can only be used for classes that extend "%s"', __CLASS__, __METHOD__, Result::class));
63+
if (Result::class !== $class) {
64+
$parent = get_parent_class($class);
65+
if (false === $parent || Result::class !== $parent) {
66+
throw new \LogicException(sprintf('The "%s::%s" can only be used for classes that extend "%s"', __CLASS__, __METHOD__, Result::class));
67+
}
3368
}
3469

35-
$rereflectionClass = new \ReflectionClass($class);
36-
$object = $rereflectionClass->newInstanceWithoutConstructor();
70+
$reflectionClass = new \ReflectionClass(Response::class);
71+
$response = $reflectionClass->newInstanceWithoutConstructor();
72+
$property = $reflectionClass->getProperty('resolveResult');
73+
$property->setAccessible(true);
74+
$property->setValue($response, true);
75+
76+
$reflectionClass = new \ReflectionClass($class);
77+
$object = $reflectionClass->newInstance($response);
3778
$data['initialized'] = true;
3879

3980
foreach ($data as $propertyName => $propertyValue) {
40-
$property = $rereflectionClass->getProperty($propertyName);
81+
$property = $reflectionClass->getProperty($propertyName);
4182
$property->setAccessible(true);
4283
$property->setValue($object, $propertyValue);
4384
}
4485

45-
self::addUndefinedProperties($rereflectionClass, $object, $data);
86+
self::addUndefinedProperties($reflectionClass, $object, $data);
4687

4788
return $object;
4889
}
@@ -52,14 +93,18 @@ public static function create(string $class, array $data = [])
5293
*
5394
* @throws \ReflectionException
5495
*/
55-
private static function addUndefinedProperties(\ReflectionClass $rereflectionClass, $object, array $data): void
96+
private static function addUndefinedProperties(\ReflectionClass $reflectionClass, $object, array $data): void
5697
{
57-
foreach ($rereflectionClass->getProperties(\ReflectionProperty::IS_PRIVATE) as $property) {
98+
foreach ($reflectionClass->getProperties(\ReflectionProperty::IS_PRIVATE) as $property) {
5899
if (\array_key_exists($property->getName(), $data)) {
59100
continue;
60101
}
61102

62-
$getter = $rereflectionClass->getMethod('get' . $property->getName());
103+
if (!$reflectionClass->hasMethod('get' . $property->getName())) {
104+
continue;
105+
}
106+
107+
$getter = $reflectionClass->getMethod('get' . $property->getName());
63108
/** @psalm-suppress PossiblyNullReference */
64109
if (!$getter->hasReturnType() || $getter->getReturnType()->allowsNull()) {
65110
continue;

tests/Unit/Test/ResultMockFactoryTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace AsyncAws\Core\Tests\Unit\Test;
66

7+
use AsyncAws\Core\Exception\Http\ClientException;
8+
use AsyncAws\Core\Result;
79
use AsyncAws\Core\Sts\Result\AssumedRoleUser;
810
use AsyncAws\Core\Sts\Result\AssumeRoleResponse;
911
use AsyncAws\Core\Test\ResultMockFactory;
@@ -52,4 +54,33 @@ public function testCreateWithInvalidClass()
5254
'AssumedRoleId' => 'foo123',
5355
]);
5456
}
57+
58+
public function testCreateWithResult()
59+
{
60+
$result = ResultMockFactory::create(Result::class, []);
61+
62+
self::assertInstanceOf(Result::class, $result);
63+
}
64+
65+
public function testCallToResolveDontFail()
66+
{
67+
$result = ResultMockFactory::create(Result::class, []);
68+
69+
self::assertTrue($result->resolve());
70+
}
71+
72+
public function testCreateFailling()
73+
{
74+
$result = ResultMockFactory::createFailing(Result::class, 400, 'Boom');
75+
76+
$this->expectException(ClientException::class);
77+
$this->expectExceptionCode(400);
78+
$this->expectExceptionMessage('HTTP 400 returned for "http://localhost/".
79+
80+
Boom
81+
82+
');
83+
84+
$result->resolve();
85+
}
5586
}

0 commit comments

Comments
 (0)