Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions src/Driver/Test/TestTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -228,4 +224,4 @@ public function deleteEntry(TestTableEntry $entry): static
}
return $this;
}
}
}
9 changes: 9 additions & 0 deletions test/tests/TestDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading