Skip to content

Commit 85613f0

Browse files
a-barzantiBilge
authored andcommitted
Added automatic CountableProviderRecords wrapping for countable iterators.
1 parent c7c6d8a commit 85613f0

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Porter.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
99
use ScriptFUSION\Porter\Collection\CountableMappedRecords;
1010
use ScriptFUSION\Porter\Collection\CountablePorterRecords;
11+
use ScriptFUSION\Porter\Collection\CountableProviderRecords;
1112
use ScriptFUSION\Porter\Collection\FilteredRecords;
1213
use ScriptFUSION\Porter\Collection\MappedRecords;
1314
use ScriptFUSION\Porter\Collection\PorterRecords;
@@ -62,8 +63,7 @@ public function import(ImportSpecification $specification)
6263
$records = $this->fetch($specification->getResource(), $specification->getCacheAdvice());
6364

6465
if (!$records instanceof ProviderRecords) {
65-
// Wrap Iterator in ProviderRecords.
66-
$records = new ProviderRecords($records, $specification->getResource());
66+
$records = $this->createProviderRecords($records, $specification->getResource());
6767
}
6868

6969
if ($specification->getFilter()) {
@@ -103,6 +103,15 @@ public function importOne(ImportSpecification $specification)
103103
return $one;
104104
}
105105

106+
private function createProviderRecords(\Iterator $records, ProviderResource $resource)
107+
{
108+
if ($records instanceof \Countable) {
109+
return new CountableProviderRecords($records, count($records), $resource);
110+
}
111+
112+
return new ProviderRecords($records, $resource);
113+
}
114+
106115
private function createPorterRecords(RecordCollection $records, ImportSpecification $specification)
107116
{
108117
if ($records instanceof \Countable) {

test/Integration/Porter/PorterTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,24 @@ public function testImport()
119119
{
120120
$records = $this->porter->import($this->specification);
121121

122+
self::assertInstanceOf(PorterRecords::class, $records);
123+
self::assertNotSame($this->specification, $records->getSpecification());
124+
self::assertInstanceOf(CountableProviderRecords::class, $records->getPreviousCollection());
125+
self::assertSame('foo', $records->current());
126+
}
127+
128+
public function testNonCountableIteratorImport()
129+
{
130+
$this->provider->shouldReceive('fetch')->andReturnUsing(function () {
131+
yield 'foo';
132+
});
133+
134+
$records = $this->porter->import($this->specification);
135+
122136
self::assertInstanceOf(PorterRecords::class, $records);
123137
self::assertNotSame($this->specification, $records->getSpecification());
124138
self::assertInstanceOf(ProviderRecords::class, $records->getPreviousCollection());
139+
self::assertNotInstanceOf(CountableProviderRecords::class, $records->getPreviousCollection());
125140
self::assertSame('foo', $records->current());
126141
}
127142

@@ -145,6 +160,23 @@ public function testImportCountableRecords()
145160
self::assertCount($count, $records);
146161
}
147162

163+
public function testImportAndMapNonCountableRecords()
164+
{
165+
$iterateOne = function () {
166+
yield 'foo';
167+
};
168+
$records = $this->porter->import(
169+
(new StaticDataImportSpecification(
170+
new ProviderRecords($iterateOne(), $this->resource)
171+
))->setMapping(\Mockery::mock(Mapping::class))
172+
);
173+
174+
self::assertInstanceOf(MappedRecords::class, $records->getPreviousCollection());
175+
self::assertInstanceOf(\Iterator::class, $records);
176+
self::assertNotInstanceOf(CountableMappedRecords::class, $records->getPreviousCollection());
177+
self::assertNotInstanceOf(\Countable::class, $records);
178+
}
179+
148180
/**
149181
* Tests that when the resource is countable the count is propagated to the outermost collection via a mapped
150182
* collection.

0 commit comments

Comments
 (0)