Skip to content

Commit 677c2d4

Browse files
committed
Added missing objects to MappedRecords and FilteredRecords.
1 parent b29dcb9 commit 677c2d4

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

src/Collection/CountableMappedRecords.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace ScriptFUSION\Porter\Collection;
33

4+
use ScriptFUSION\Mapper\Mapping;
5+
46
class CountableMappedRecords extends MappedRecords implements \Countable
57
{
68
use CountableRecordsTrait;
@@ -10,9 +12,9 @@ class CountableMappedRecords extends MappedRecords implements \Countable
1012
* @param int $count
1113
* @param RecordCollection $previousCollection
1214
*/
13-
public function __construct(\Iterator $records, $count, RecordCollection $previousCollection)
15+
public function __construct(\Iterator $records, $count, RecordCollection $previousCollection, Mapping $mapping)
1416
{
15-
parent::__construct($records, $previousCollection);
17+
parent::__construct($records, $previousCollection, $mapping);
1618

1719
$this->setCount($count);
1820
}

src/Collection/FilteredRecords.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,21 @@
33

44
class FilteredRecords extends RecordCollection
55
{
6-
// Intentionally empty.
6+
/** @var callable */
7+
private $filter;
8+
9+
public function __construct(\Iterator $records, RecordCollection $previousCollection, callable $filter)
10+
{
11+
parent::__construct($records, $previousCollection);
12+
13+
$this->filter = $filter;
14+
}
15+
16+
/**
17+
* @return callable
18+
*/
19+
public function getFilter()
20+
{
21+
return $this->filter;
22+
}
723
}

src/Collection/MappedRecords.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
<?php
22
namespace ScriptFUSION\Porter\Collection;
33

4+
use ScriptFUSION\Mapper\Mapping;
5+
46
class MappedRecords extends RecordCollection
57
{
6-
// Intentionally empty.
8+
/** @var Mapping */
9+
private $mapping;
10+
11+
public function __construct(\Iterator $records, RecordCollection $previousCollection, Mapping $mapping)
12+
{
13+
parent::__construct($records, $previousCollection);
14+
15+
$this->mapping = $mapping;
16+
}
17+
18+
/**
19+
* @return Mapping
20+
*/
21+
public function getMapping()
22+
{
23+
return $this->mapping;
24+
}
725
}

src/Porter.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,25 @@ private function filter(ProviderRecords $records, callable $predicate, $context)
140140
}
141141
};
142142

143-
return new FilteredRecords($filter(), $records);
143+
return new FilteredRecords($filter(), $records, $filter);
144144
}
145145

146146
private function map(RecordCollection $records, Mapping $mapping, $context)
147147
{
148148
return $this->createMappedRecords(
149149
$this->getOrCreateMapper()->mapCollection($records, $mapping, $context),
150-
$records
150+
$records,
151+
$mapping
151152
);
152153
}
153154

154-
private function createMappedRecords(\Iterator $records, RecordCollection $previous)
155+
private function createMappedRecords(\Iterator $records, RecordCollection $previous, Mapping $mapping)
155156
{
156157
if ($previous instanceof \Countable) {
157-
return new CountableMappedRecords($records, count($previous), $previous);
158+
return new CountableMappedRecords($records, count($previous), $previous, $mapping);
158159
}
159160

160-
return new MappedRecords($records, $previous);
161+
return new MappedRecords($records, $previous, $mapping);
161162
}
162163

163164
private function applyCacheAdvice(Provider $provider, CacheAdvice $cacheAdvice)

test/Integration/Porter/PorterTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,24 +251,30 @@ public function testFilter()
251251
);
252252

253253
self::assertInstanceOf(PorterRecords::class, $records);
254-
self::assertInstanceOf(FilteredRecords::class, $records->getPreviousCollection());
255254
self::assertSame([1, 3, 5, 7, 9], iterator_to_array($records));
255+
256+
/** @var FilteredRecords $previous */
257+
self::assertInstanceOf(FilteredRecords::class, $previous = $records->getPreviousCollection());
258+
self::assertNotSame($previous->getFilter(), $this->specification->getFilter(), 'Filter was not cloned.');
256259
}
257260

258261
public function testMap()
259262
{
260263
$records = $this->porter->setMapper(
261264
\Mockery::mock(CollectionMapper::class)
262265
->shouldReceive('mapCollection')
263-
->with(\Mockery::type(\Iterator::class), \Mockery::type(Mapping::class), \Mockery::any())
266+
->with(\Mockery::type(\Iterator::class), $mapping = \Mockery::type(Mapping::class), \Mockery::any())
264267
->once()
265268
->andReturn(new \ArrayIterator($result = ['foo' => 'bar']))
266269
->getMock()
267270
)->import($this->specification->setMapping(\Mockery::mock(Mapping::class)));
268271

269272
self::assertInstanceOf(PorterRecords::class, $records);
270-
self::assertInstanceOf(MappedRecords::class, $records->getPreviousCollection());
271273
self::assertSame($result, iterator_to_array($records));
274+
275+
/** @var MappedRecords $previous */
276+
self::assertInstanceOf(MappedRecords::class, $previous = $records->getPreviousCollection());
277+
self::assertNotSame($mapping, $previous->getMapping(), 'Mapping was not cloned.');
272278
}
273279

274280
public function testApplyCacheAdvice()

0 commit comments

Comments
 (0)