Skip to content

Commit bf4f7a6

Browse files
committed
Added CountableAsyncProviderRecords test.
Added AsyncTransformer + PorterAware test. Fixed bug where async transformer was not cloned due to type mismatch.
1 parent 441b309 commit bf4f7a6

4 files changed

Lines changed: 59 additions & 12 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scriptfusion/porter",
3-
"description": "Data import abstraction library.",
3+
"description": "Scalable and efficient data import abstraction for APIs.",
44
"authors": [
55
{
66
"name": "Bilge",

src/Specification/Specification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __clone()
5050
{
5151
$transformers = $this->transformers;
5252
$this->clearTransformers()->addTransformers(array_map(
53-
static function (Transformer $transformer): Transformer {
53+
static function (AnysyncTransformer $transformer): AnysyncTransformer {
5454
return clone $transformer;
5555
},
5656
$transformers

test/Integration/Porter/PorterAsyncTest.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66
use Amp\Iterator;
77
use Amp\Loop;
88
use Amp\Producer;
9+
use ScriptFUSION\Porter\Collection\AsyncRecordCollection;
10+
use ScriptFUSION\Porter\Collection\CountableAsyncPorterRecords;
11+
use ScriptFUSION\Porter\Collection\CountableAsyncProviderRecords;
912
use ScriptFUSION\Porter\Connector\AsyncConnector;
1013
use ScriptFUSION\Porter\Connector\ConnectorOptions;
1114
use ScriptFUSION\Porter\ForeignResourceException;
1215
use ScriptFUSION\Porter\ImportException;
1316
use ScriptFUSION\Porter\IncompatibleProviderException;
1417
use ScriptFUSION\Porter\Porter;
18+
use ScriptFUSION\Porter\PorterAware;
1519
use ScriptFUSION\Porter\Provider\Provider;
1620
use ScriptFUSION\Porter\Specification\AsyncImportSpecification;
21+
use ScriptFUSION\Porter\Transform\AsyncTransformer;
1722
use ScriptFUSION\Porter\Transform\FilterTransformer;
1823
use 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
}

test/Integration/Porter/PorterSyncTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace ScriptFUSIONTest\Integration\Porter;
55

66
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
7+
use ScriptFUSION\Porter\Collection\CountablePorterRecords;
78
use ScriptFUSION\Porter\Collection\FilteredRecords;
89
use ScriptFUSION\Porter\Collection\PorterRecords;
910
use ScriptFUSION\Porter\Collection\ProviderRecords;
@@ -13,12 +14,11 @@
1314
use ScriptFUSION\Porter\Connector\ImportConnector;
1415
use ScriptFUSION\Porter\Connector\Recoverable\RecoverableExceptionHandler;
1516
use ScriptFUSION\Porter\Connector\Recoverable\StatelessRecoverableExceptionHandler;
17+
use ScriptFUSION\Porter\ForeignResourceException;
1618
use ScriptFUSION\Porter\ImportException;
1719
use ScriptFUSION\Porter\IncompatibleProviderException;
1820
use ScriptFUSION\Porter\PorterAware;
19-
use ScriptFUSION\Porter\ForeignResourceException;
2021
use ScriptFUSION\Porter\Provider\AsyncProvider;
21-
use ScriptFUSION\Porter\Provider\Provider;
2222
use ScriptFUSION\Porter\ProviderNotFoundException;
2323
use ScriptFUSION\Porter\Specification\ImportSpecification;
2424
use ScriptFUSION\Porter\Specification\StaticDataImportSpecification;
@@ -46,7 +46,7 @@ public function testImport(): void
4646
}
4747

4848
/**
49-
* Tests that when the resource is countable the count is propagated to the outermost collection.
49+
* Tests that when the resource is countable, the count is propagated to the outermost collection.
5050
*/
5151
public function testImportCountableRecords(): void
5252
{
@@ -59,7 +59,7 @@ public function testImportCountableRecords(): void
5959
self::assertCount($count, $first);
6060

6161
// Outermost collection.
62-
self::assertInstanceOf(\Countable::class, $records);
62+
self::assertInstanceOf(CountablePorterRecords::class, $records);
6363
self::assertCount($count, $records);
6464
}
6565

@@ -102,10 +102,10 @@ public function testPorterAwareTransformer(): void
102102
$this->specification->addTransformer(
103103
\Mockery::mock(implode(',', [Transformer::class, PorterAware::class]))
104104
->shouldReceive('setPorter')
105-
->with($this->porter)
106-
->once()
105+
->with($this->porter)
106+
->once()
107107
->shouldReceive('transform')
108-
->andReturn(\Mockery::mock(RecordCollection::class))
108+
->andReturn(\Mockery::mock(RecordCollection::class))
109109
->getMock()
110110
)
111111
);

0 commit comments

Comments
 (0)