Skip to content

Commit

Permalink
Added automatic CountableProviderRecords wrapping for countable itera…
Browse files Browse the repository at this point in the history
…tors.
  • Loading branch information
a-barzanti authored and Bilge committed Oct 14, 2016
1 parent c7c6d8a commit 85613f0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Porter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
use ScriptFUSION\Porter\Collection\CountableMappedRecords;
use ScriptFUSION\Porter\Collection\CountablePorterRecords;
use ScriptFUSION\Porter\Collection\CountableProviderRecords;
use ScriptFUSION\Porter\Collection\FilteredRecords;
use ScriptFUSION\Porter\Collection\MappedRecords;
use ScriptFUSION\Porter\Collection\PorterRecords;
Expand Down Expand Up @@ -62,8 +63,7 @@ public function import(ImportSpecification $specification)
$records = $this->fetch($specification->getResource(), $specification->getCacheAdvice());

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

if ($specification->getFilter()) {
Expand Down Expand Up @@ -103,6 +103,15 @@ public function importOne(ImportSpecification $specification)
return $one;
}

private function createProviderRecords(\Iterator $records, ProviderResource $resource)
{
if ($records instanceof \Countable) {
return new CountableProviderRecords($records, count($records), $resource);
}

return new ProviderRecords($records, $resource);
}

private function createPorterRecords(RecordCollection $records, ImportSpecification $specification)
{
if ($records instanceof \Countable) {
Expand Down
32 changes: 32 additions & 0 deletions test/Integration/Porter/PorterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,24 @@ public function testImport()
{
$records = $this->porter->import($this->specification);

self::assertInstanceOf(PorterRecords::class, $records);
self::assertNotSame($this->specification, $records->getSpecification());
self::assertInstanceOf(CountableProviderRecords::class, $records->getPreviousCollection());
self::assertSame('foo', $records->current());
}

public function testNonCountableIteratorImport()
{
$this->provider->shouldReceive('fetch')->andReturnUsing(function () {
yield 'foo';
});

$records = $this->porter->import($this->specification);

self::assertInstanceOf(PorterRecords::class, $records);
self::assertNotSame($this->specification, $records->getSpecification());
self::assertInstanceOf(ProviderRecords::class, $records->getPreviousCollection());
self::assertNotInstanceOf(CountableProviderRecords::class, $records->getPreviousCollection());
self::assertSame('foo', $records->current());
}

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

public function testImportAndMapNonCountableRecords()
{
$iterateOne = function () {
yield 'foo';
};
$records = $this->porter->import(
(new StaticDataImportSpecification(
new ProviderRecords($iterateOne(), $this->resource)
))->setMapping(\Mockery::mock(Mapping::class))
);

self::assertInstanceOf(MappedRecords::class, $records->getPreviousCollection());
self::assertInstanceOf(\Iterator::class, $records);
self::assertNotInstanceOf(CountableMappedRecords::class, $records->getPreviousCollection());
self::assertNotInstanceOf(\Countable::class, $records);
}

/**
* Tests that when the resource is countable the count is propagated to the outermost collection via a mapped
* collection.
Expand Down

0 comments on commit 85613f0

Please sign in to comment.