fix: make Buffer::nth() positional so it stays correct after deletions
Buffer::nth() resolved the requested position by adding it to the first row key and indexing the rows array by that computed key. That assumes contiguous keys, but Buffer::delete() removes rows with unset() without reindexing, leaving gaps. After a deletion nth() therefore returned the wrong record (or an empty array for a valid position, or a record for an out-of-range position). Resolve the position against the record order with array_values() so nth() matches getRecords() and ResultSet::nth().
This commit is contained in:
1 parent
7f690c67cd
commit
e77df85d0c
2 files changed
+22
-9
No files matched your search
@@ -285,6 +285,24 @@ final class BufferTest extends TestCase
|
||||
self::assertSame(['date' => '2025-02-08', 'temperature' => '3', 'place' => 'Berkeley'], $buffer->nth(0));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_records_by_positional_offset_after_a_middle_record_is_deleted(): void
|
||||
{
|
||||
$buffer = new Buffer(['id', 'name']);
|
||||
$buffer->insert(['1', 'a'], ['2', 'b'], ['3', 'c'], ['4', 'd'], ['5', 'e']);
|
||||
|
||||
// Delete the second record (id = 2), which leaves a gap in the row keys.
|
||||
self::assertSame(1, $buffer->delete(fn (array $row): bool => $row['id'] === '2'));
|
||||
self::assertSame(4, $buffer->recordCount());
|
||||
|
||||
// nth() is positional and must stay consistent with getRecords().
|
||||
self::assertSame(['id' => '1', 'name' => 'a'], $buffer->nth(0));
|
||||
self::assertSame(['id' => '3', 'name' => 'c'], $buffer->nth(1));
|
||||
self::assertSame(['id' => '4', 'name' => 'd'], $buffer->nth(2));
|
||||
self::assertSame(['id' => '5', 'name' => 'e'], $buffer->nth(3));
|
||||
self::assertSame([], $buffer->nth(4));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_can_not_insert_invalid_records(): void
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user