diff --git a/src/MessageQuery.php b/src/MessageQuery.php index e6711fa..b4e3790 100644 --- a/src/MessageQuery.php +++ b/src/MessageQuery.php @@ -258,23 +258,29 @@ protected function populate(Collection $uids): MessageCollection */ protected function fetch(Collection $messages): array { - if ($this->fetchOrder === 'desc') { - $messages = $messages->reverse(); - } + $messages = match ($this->fetchOrder) { + 'asc' => $messages->sort(SORT_NUMERIC), + 'desc' => $messages->sortDesc(SORT_NUMERIC), + }; - $uids = $messages->forPage($this->page, $this->limit)->toArray(); + $uids = $messages->forPage($this->page, $this->limit) + ->values() + ->all(); $flags = $this->fetchFlags ? $this->connection() ->flags($uids) - ->mapWithKeys(MessageResponseParser::getFlags(...))->all() : []; + ->mapWithKeys(MessageResponseParser::getFlags(...)) + ->all() : []; $headers = $this->fetchHeaders ? $this->connection() ->bodyHeader($uids, $this->fetchAsUnread) - ->mapWithKeys(MessageResponseParser::getBodyHeader(...))->all() : []; + ->mapWithKeys(MessageResponseParser::getBodyHeader(...)) + ->all() : []; $contents = $this->fetchBody ? $this->connection() ->bodyText($uids, $this->fetchAsUnread) - ->mapWithKeys(MessageResponseParser::getBodyText(...))->all() : []; + ->mapWithKeys(MessageResponseParser::getBodyText(...)) + ->all() : []; return [ 'uids' => $uids, diff --git a/tests/Integration/MailboxTest.php b/tests/Integration/MailboxTest.php index a6cc9b2..84ee47d 100644 --- a/tests/Integration/MailboxTest.php +++ b/tests/Integration/MailboxTest.php @@ -17,7 +17,7 @@ test('capabilities', function () { $mailbox = mailbox(); - expect($mailbox->capabilities())->toBe([ + expect(array_flip($mailbox->capabilities()))->toHaveKeys([ 'IMAP4rev1', 'LITERAL+', 'UIDPLUS',