From 298ab003c509258f85883064319b0c9ce62d4e77 Mon Sep 17 00:00:00 2001 From: Kurt Thiemann Date: Wed, 26 Nov 2025 12:30:50 +0100 Subject: [PATCH 1/2] apply where, order, and limit in the right order in TestDriver --- src/Driver/Test/TestTable.php | 24 ++++++++++-------------- test/tests/TestDriverTest.php | 9 +++++++++ 2 files changed, 19 insertions(+), 14 deletions(-) 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..730b7a9 100644 --- a/test/tests/TestDriverTest.php +++ b/test/tests/TestDriverTest.php @@ -564,6 +564,15 @@ public function testDeleteQuery(): void $this->assertNull($model); } + public function testOrderAfterFilter(): 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(); From 9edddf8aae1ff316c2c3d7d7741b4455857e2377 Mon Sep 17 00:00:00 2001 From: Kurt Thiemann Date: Wed, 26 Nov 2025 12:38:01 +0100 Subject: [PATCH 2/2] update test name --- test/tests/TestDriverTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tests/TestDriverTest.php b/test/tests/TestDriverTest.php index 730b7a9..fff588c 100644 --- a/test/tests/TestDriverTest.php +++ b/test/tests/TestDriverTest.php @@ -564,7 +564,7 @@ public function testDeleteQuery(): void $this->assertNull($model); } - public function testOrderAfterFilter(): void + public function testOrderBeforeLimit(): void { $result = TestModel::select(order: ["number" => OrderField::DESCENDING], limit: 3);