Skip to content

Commit 24f7b50

Browse files
committed
Add test for keep original data after Finder::last() called
1 parent be860ef commit 24f7b50

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tests/FinderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
use ArrayIterator;
88
use ArrayLookup\Finder;
99
use ArrayObject;
10+
use DateTime;
11+
use DateTimeImmutable;
12+
use DateTimeInterface;
1013
use Generator;
1114
use PHPUnit\Framework\Attributes\DataProvider;
1215
use PHPUnit\Framework\TestCase;
16+
use stdClass;
1317

18+
use function current;
1419
use function str_contains;
1520

1621
final class FinderTest extends TestCase
@@ -254,6 +259,21 @@ public static function lastReturnKeyResortKeyDataProvider(): array
254259
];
255260
}
256261

262+
public function testLastKeepCurrentOriginalData(): void
263+
{
264+
$obj = new stdClass();
265+
$obj->data = new ArrayIterator([new DateTime('now'), new DateTimeImmutable('now'), new stdClass()]);
266+
267+
// get last
268+
$last = Finder::last($obj->data, static fn(object $datum): bool => $datum instanceof DateTimeInterface);
269+
$this->assertInstanceOf(DateTimeImmutable::class, $last);
270+
271+
// keep first record not changed from data
272+
$this->assertInstanceOf(DateTime::class, $obj->data[0]);
273+
$arrayCopy = $obj->data->getArrayCopy();
274+
$this->assertSame(DateTime::class, current($arrayCopy)::class);
275+
}
276+
257277
#[DataProvider('rowsDataProvider')]
258278
public function testRows(iterable $data, callable $filter, array $expected): void
259279
{

0 commit comments

Comments
 (0)