44
55namespace AsyncAws \Core \Test ;
66
7+ use AsyncAws \Core \Response ;
78use 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.
1316 */
1417class 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 ;
0 commit comments