Skip to content

Commit 84bcd0f

Browse files
authored
Merge pull request #29 from aternosorg/test-driver-order
Apply where, order, and limit in the right order in TestDriver
2 parents ccfdcc9 + 9edddf8 commit 84bcd0f

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/Driver/Test/TestTable.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ public function addEntry(TestTableEntry ...$entry): static
5656
*/
5757
public function query(Query $query): QueryResult
5858
{
59-
$entries = $this->findEntries($query->getWhere(), $query->getLimit()?->start, $query->getLimit()?->length);
60-
if ($order = $query->getOrder()) {
61-
$entries = $this->orderEntries($entries, $order);
62-
}
59+
$entries = $this->findEntries($query->getWhere(), $query->getLimit()?->start, $query->getLimit()?->length, $query->getOrder());
6360

6461
if ($query instanceof SelectQuery) {
6562
$clonedEntries = [];
@@ -129,9 +126,10 @@ public function groupAndAggregateEntries(array $entries, ?array $group, ?array $
129126
* @param WhereGroup|null $where
130127
* @param int|null $offset
131128
* @param int|null $limit
129+
* @param array|null $order
132130
* @return TestTableEntry[]
133131
*/
134-
protected function findEntries(?WhereGroup $where, ?int $offset = null, ?int $limit = null): array
132+
protected function findEntries(?WhereGroup $where, ?int $offset = null, ?int $limit = null, ?array $order = null): array
135133
{
136134
$entries = [];
137135
if ($offset === null) {
@@ -141,16 +139,14 @@ protected function findEntries(?WhereGroup $where, ?int $offset = null, ?int $li
141139
if (!$entry->matchesWhereGroup($where)) {
142140
continue;
143141
}
144-
if ($offset > 0) {
145-
$offset--;
146-
continue;
147-
}
148142
$entries[] = $entry;
149-
if ($limit !== null && count($entries) >= $limit) {
150-
break;
151-
}
152143
}
153-
return $entries;
144+
145+
if ($order !== null) {
146+
$entries = $this->orderEntries($entries, $order);
147+
}
148+
149+
return array_slice($entries, $offset, $limit);
154150
}
155151

156152
/**
@@ -228,4 +224,4 @@ public function deleteEntry(TestTableEntry $entry): static
228224
}
229225
return $this;
230226
}
231-
}
227+
}

test/tests/TestDriverTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,15 @@ public function testDeleteQuery(): void
564564
$this->assertNull($model);
565565
}
566566

567+
public function testOrderBeforeLimit(): void
568+
{
569+
$result = TestModel::select(order: ["number" => OrderField::DESCENDING], limit: 3);
570+
571+
$this->assertEquals(9, $result[0]->number);
572+
$this->assertEquals(8, $result[1]->number);
573+
$this->assertEquals(7, $result[2]->number);
574+
}
575+
567576
protected function tearDown(): void
568577
{
569578
TestModel::clearTestEntries();

0 commit comments

Comments
 (0)