Skip to content

Commit f09b49c

Browse files
authored
Make sure ResultMockFactory support pagination (#354)
1 parent 64e6e8a commit f09b49c

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/Test/ResultMockFactory.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ public static function create(string $class, array $data = [])
7575

7676
$reflectionClass = new \ReflectionClass($class);
7777
$object = $reflectionClass->newInstance($response);
78-
$data['initialized'] = true;
78+
if (Result::class !== $class) {
79+
self::addPropertiesOnResult($reflectionClass, $object, $class);
80+
}
7981

82+
$data['initialized'] = true;
8083
foreach ($data as $propertyName => $propertyValue) {
8184
$property = $reflectionClass->getProperty($propertyName);
8285
$property->setAccessible(true);
@@ -144,4 +147,49 @@ private static function addUndefinedProperties(\ReflectionClass $reflectionClass
144147
}
145148
}
146149
}
150+
151+
/**
152+
* Set input and aws client to handle pagination.
153+
*/
154+
private static function addPropertiesOnResult(\ReflectionClass $reflectionClass, object $object, string $class): void
155+
{
156+
if (false === $pos = strrpos($class, '\\')) {
157+
throw new \LogicException(sprintf('Expected class "%s" to have a backslash. ', $class));
158+
}
159+
160+
$className = substr($class, $pos + 1);
161+
if ('Output' === substr($className, -6)) {
162+
$classNameWithoutSuffix = substr($className, 0, -6);
163+
} elseif ('Response' === substr($className, -8)) {
164+
$classNameWithoutSuffix = substr($className, 0, -8);
165+
} else {
166+
throw new \LogicException(sprintf('Unknown class suffix: "%s"', $className));
167+
}
168+
169+
if (false === $pos = strrpos($class, '\\', -2 - \strlen($className))) {
170+
throw new \LogicException(sprintf('Expected class "%s" to have more than one backslash. ', $class));
171+
}
172+
173+
$baseNamespace = substr($class, 0, $pos);
174+
if (false === $pos = strrpos($baseNamespace, '\\')) {
175+
throw new \LogicException(sprintf('Expected base namespace "%s" to have a backslash. ', $baseNamespace));
176+
}
177+
178+
$awsClientClass = $baseNamespace . (substr($baseNamespace, $pos)) . 'Client';
179+
$inputClass = $baseNamespace . '\\Input\\' . $classNameWithoutSuffix . 'Request';
180+
181+
if (class_exists($awsClientClass)) {
182+
$awsClientMock = (new \ReflectionClass($awsClientClass))->newInstanceWithoutConstructor();
183+
$property = $reflectionClass->getProperty('awsClient');
184+
$property->setAccessible(true);
185+
$property->setValue($object, $awsClientMock);
186+
}
187+
188+
if (class_exists($inputClass)) {
189+
$inputMock = (new \ReflectionClass($inputClass))->newInstanceWithoutConstructor();
190+
$property = $reflectionClass->getProperty('input');
191+
$property->setAccessible(true);
192+
$property->setValue($object, $inputMock);
193+
}
194+
}
147195
}

tests/Unit/Test/ResultMockFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use AsyncAws\Core\Exception\Http\ClientException;
88
use AsyncAws\Core\Result;
9-
use AsyncAws\Core\Sts\Result\AssumedRoleUser;
109
use AsyncAws\Core\Sts\Result\AssumeRoleResponse;
10+
use AsyncAws\Core\Sts\ValueObject\AssumedRoleUser;
1111
use AsyncAws\Core\Test\ResultMockFactory;
1212
use AsyncAws\Core\Tests\Resources\ExampleResponse;
1313
use PHPUnit\Framework\TestCase;
@@ -49,6 +49,7 @@ public function testCreateWithInvalidParameters()
4949
public function testCreateWithInvalidClass()
5050
{
5151
$this->expectException(\LogicException::class);
52+
$this->expectExceptionMessage('can only be used for classes that extend "AsyncAws\Core\Result"');
5253
ResultMockFactory::create(AssumedRoleUser::class, [
5354
'Arn' => 'arn123',
5455
'AssumedRoleId' => 'foo123',

0 commit comments

Comments
 (0)