diff --git a/src/Driver/Test/TestTable.php b/src/Driver/Test/TestTable.php index a60bbb6..fc190c9 100644 --- a/src/Driver/Test/TestTable.php +++ b/src/Driver/Test/TestTable.php @@ -56,10 +56,7 @@ public function addEntry(TestTableEntry ...$entry): static */ public function query(Query $query): QueryResult { - $entries = $this->findEntries($query->getWhere(), $query->getLimit()?->start, $query->getLimit()?->length); - if ($order = $query->getOrder()) { - $entries = $this->orderEntries($entries, $order); - } + $entries = $this->findEntries($query->getWhere(), $query->getLimit()?->start, $query->getLimit()?->length, $query->getOrder()); if ($query instanceof SelectQuery) { $clonedEntries = []; @@ -129,9 +126,10 @@ public function groupAndAggregateEntries(array $entries, ?array $group, ?array $ * @param WhereGroup|null $where * @param int|null $offset * @param int|null $limit + * @param array|null $order * @return TestTableEntry[] */ - protected function findEntries(?WhereGroup $where, ?int $offset = null, ?int $limit = null): array + protected function findEntries(?WhereGroup $where, ?int $offset = null, ?int $limit = null, ?array $order = null): array { $entries = []; if ($offset === null) { @@ -141,16 +139,14 @@ protected function findEntries(?WhereGroup $where, ?int $offset = null, ?int $li if (!$entry->matchesWhereGroup($where)) { continue; } - if ($offset > 0) { - $offset--; - continue; - } $entries[] = $entry; - if ($limit !== null && count($entries) >= $limit) { - break; - } } - return $entries; + + if ($order !== null) { + $entries = $this->orderEntries($entries, $order); + } + + return array_slice($entries, $offset, $limit); } /** @@ -228,4 +224,4 @@ public function deleteEntry(TestTableEntry $entry): static } return $this; } -} \ No newline at end of file +} diff --git a/test/tests/TestDriverTest.php b/test/tests/TestDriverTest.php index 1e9c235..fff588c 100644 --- a/test/tests/TestDriverTest.php +++ b/test/tests/TestDriverTest.php @@ -564,6 +564,15 @@ public function testDeleteQuery(): void $this->assertNull($model); } + public function testOrderBeforeLimit(): void + { + $result = TestModel::select(order: ["number" => OrderField::DESCENDING], limit: 3); + + $this->assertEquals(9, $result[0]->number); + $this->assertEquals(8, $result[1]->number); + $this->assertEquals(7, $result[2]->number); + } + protected function tearDown(): void { TestModel::clearTestEntries();