66use Amp \Iterator ;
77use Amp \Loop ;
88use Amp \Producer ;
9+ use ScriptFUSION \Porter \Collection \AsyncRecordCollection ;
10+ use ScriptFUSION \Porter \Collection \CountableAsyncPorterRecords ;
11+ use ScriptFUSION \Porter \Collection \CountableAsyncProviderRecords ;
912use ScriptFUSION \Porter \Connector \AsyncConnector ;
1013use ScriptFUSION \Porter \Connector \ConnectorOptions ;
1114use ScriptFUSION \Porter \ForeignResourceException ;
1215use ScriptFUSION \Porter \ImportException ;
1316use ScriptFUSION \Porter \IncompatibleProviderException ;
1417use ScriptFUSION \Porter \Porter ;
18+ use ScriptFUSION \Porter \PorterAware ;
1519use ScriptFUSION \Porter \Provider \Provider ;
1620use ScriptFUSION \Porter \Specification \AsyncImportSpecification ;
21+ use ScriptFUSION \Porter \Transform \AsyncTransformer ;
1722use ScriptFUSION \Porter \Transform \FilterTransformer ;
1823use ScriptFUSIONTest \MockFactory ;
1924
@@ -48,6 +53,30 @@ public function testImportOneAsync(): \Generator
4853 self ::assertSame (['foo ' ], yield $ this ->porter ->importOneAsync ($ this ->specification ));
4954 }
5055
56+ /**
57+ * Tests that when the resource is countable, the count is propagated to the outermost collection and the records
58+ * are intact.
59+ */
60+ public function testImportCountableAsyncRecords (): \Generator
61+ {
62+ $ this ->resource ->shouldReceive ('fetchAsync ' )->andReturn (
63+ new CountableAsyncProviderRecords (Iterator \fromIterable ([$ record = ['foo ' ]]), $ count = 123 , $ this ->resource )
64+ );
65+
66+ $ records = $ this ->porter ->importAsync ($ this ->specification );
67+
68+ // Innermost collection.
69+ self ::assertInstanceOf (\Countable::class, $ first = $ records ->findFirstCollection ());
70+ self ::assertCount ($ count , $ first );
71+
72+ // Outermost collection.
73+ self ::assertInstanceOf (CountableAsyncPorterRecords::class, $ records );
74+ self ::assertCount ($ count , $ records );
75+
76+ self ::assertTrue (yield $ records ->advance ());
77+ self ::assertSame ($ record , $ records ->getCurrent ());
78+ }
79+
5180 /**
5281 * Tests that when importOne receives multiple records from a resource, an exception is thrown.
5382 */
@@ -62,7 +91,7 @@ public function testImportOneOfManyAsync(): \Generator
6291 /**
6392 * Tests that when importing from a provider that does not implement AsyncProvider, an exception is thrown.
6493 */
65- public function testImportIncompatibleProvider (): \Generator
94+ public function testImportIncompatibleProviderAsync (): \Generator
6695 {
6796 $ this ->registerProvider (\Mockery::mock (Provider::class), $ providerName = 'foo ' );
6897
@@ -73,7 +102,7 @@ public function testImportIncompatibleProvider(): \Generator
73102 /**
74103 * Tests that when a resource's provider class name does not match the provider an exception is thrown.
75104 */
76- public function testImportForeignResource (): \Generator
105+ public function testImportForeignResourceAsync (): \Generator
77106 {
78107 // Replace existing provider with a different one.
79108 $ this ->registerProvider (MockFactory::mockProvider (), \get_class ($ this ->provider ));
@@ -85,7 +114,7 @@ public function testImportForeignResource(): \Generator
85114 /**
86115 * Tests that when importing using a connector that exports options, but no clone method, an exception is thrown.
87116 */
88- public function testImportConnectorWithOptions (): void
117+ public function testImportAsyncConnectorWithOptions (): void
89118 {
90119 $ this ->provider ->shouldReceive ('getAsyncConnector ' )
91120 ->andReturn (\Mockery::mock (AsyncConnector::class, ConnectorOptions::class));
@@ -138,4 +167,22 @@ public function testFilterAsync(): void
138167
139168 $ importAndExpect ([7 , 9 ]);
140169 }
170+
171+ /**
172+ * Tests that when an AsyncTransformer is PorterAware it receives the Porter instance that invoked it.
173+ */
174+ public function testPorterAwareAsyncTransformer (): void
175+ {
176+ $ this ->porter ->importAsync (
177+ $ this ->specification ->addTransformer (
178+ \Mockery::mock (implode (', ' , [AsyncTransformer::class, PorterAware::class]))
179+ ->shouldReceive ('setPorter ' )
180+ ->with ($ this ->porter )
181+ ->once ()
182+ ->shouldReceive ('transformAsync ' )
183+ ->andReturn (\Mockery::mock (AsyncRecordCollection::class))
184+ ->getMock ()
185+ )
186+ );
187+ }
141188}
0 commit comments