Skip to content

Commit

Permalink
Added missing objects to MappedRecords and FilteredRecords.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Oct 17, 2016
1 parent b29dcb9 commit 677c2d4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/Collection/CountableMappedRecords.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace ScriptFUSION\Porter\Collection;

use ScriptFUSION\Mapper\Mapping;

class CountableMappedRecords extends MappedRecords implements \Countable
{
use CountableRecordsTrait;
Expand All @@ -10,9 +12,9 @@ class CountableMappedRecords extends MappedRecords implements \Countable
* @param int $count
* @param RecordCollection $previousCollection
*/
public function __construct(\Iterator $records, $count, RecordCollection $previousCollection)
public function __construct(\Iterator $records, $count, RecordCollection $previousCollection, Mapping $mapping)
{
parent::__construct($records, $previousCollection);
parent::__construct($records, $previousCollection, $mapping);

$this->setCount($count);
}
Expand Down
18 changes: 17 additions & 1 deletion src/Collection/FilteredRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,21 @@

class FilteredRecords extends RecordCollection
{
// Intentionally empty.
/** @var callable */
private $filter;

public function __construct(\Iterator $records, RecordCollection $previousCollection, callable $filter)
{
parent::__construct($records, $previousCollection);

$this->filter = $filter;
}

/**
* @return callable
*/
public function getFilter()
{
return $this->filter;
}
}
20 changes: 19 additions & 1 deletion src/Collection/MappedRecords.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
<?php
namespace ScriptFUSION\Porter\Collection;

use ScriptFUSION\Mapper\Mapping;

class MappedRecords extends RecordCollection
{
// Intentionally empty.
/** @var Mapping */
private $mapping;

public function __construct(\Iterator $records, RecordCollection $previousCollection, Mapping $mapping)
{
parent::__construct($records, $previousCollection);

$this->mapping = $mapping;
}

/**
* @return Mapping
*/
public function getMapping()
{
return $this->mapping;
}
}
11 changes: 6 additions & 5 deletions src/Porter.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,25 @@ private function filter(ProviderRecords $records, callable $predicate, $context)
}
};

return new FilteredRecords($filter(), $records);
return new FilteredRecords($filter(), $records, $filter);
}

private function map(RecordCollection $records, Mapping $mapping, $context)
{
return $this->createMappedRecords(
$this->getOrCreateMapper()->mapCollection($records, $mapping, $context),
$records
$records,
$mapping
);
}

private function createMappedRecords(\Iterator $records, RecordCollection $previous)
private function createMappedRecords(\Iterator $records, RecordCollection $previous, Mapping $mapping)
{
if ($previous instanceof \Countable) {
return new CountableMappedRecords($records, count($previous), $previous);
return new CountableMappedRecords($records, count($previous), $previous, $mapping);
}

return new MappedRecords($records, $previous);
return new MappedRecords($records, $previous, $mapping);
}

private function applyCacheAdvice(Provider $provider, CacheAdvice $cacheAdvice)
Expand Down
12 changes: 9 additions & 3 deletions test/Integration/Porter/PorterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,24 +251,30 @@ public function testFilter()
);

self::assertInstanceOf(PorterRecords::class, $records);
self::assertInstanceOf(FilteredRecords::class, $records->getPreviousCollection());
self::assertSame([1, 3, 5, 7, 9], iterator_to_array($records));

/** @var FilteredRecords $previous */
self::assertInstanceOf(FilteredRecords::class, $previous = $records->getPreviousCollection());
self::assertNotSame($previous->getFilter(), $this->specification->getFilter(), 'Filter was not cloned.');
}

public function testMap()
{
$records = $this->porter->setMapper(
\Mockery::mock(CollectionMapper::class)
->shouldReceive('mapCollection')
->with(\Mockery::type(\Iterator::class), \Mockery::type(Mapping::class), \Mockery::any())
->with(\Mockery::type(\Iterator::class), $mapping = \Mockery::type(Mapping::class), \Mockery::any())
->once()
->andReturn(new \ArrayIterator($result = ['foo' => 'bar']))
->getMock()
)->import($this->specification->setMapping(\Mockery::mock(Mapping::class)));

self::assertInstanceOf(PorterRecords::class, $records);
self::assertInstanceOf(MappedRecords::class, $records->getPreviousCollection());
self::assertSame($result, iterator_to_array($records));

/** @var MappedRecords $previous */
self::assertInstanceOf(MappedRecords::class, $previous = $records->getPreviousCollection());
self::assertNotSame($mapping, $previous->getMapping(), 'Mapping was not cloned.');
}

public function testApplyCacheAdvice()
Expand Down

0 comments on commit 677c2d4

Please sign in to comment.