diff --git a/src/Connection/ImapQueryBuilder.php b/src/Connection/ImapQueryBuilder.php index 71fcdaf..4fdc0e0 100644 --- a/src/Connection/ImapQueryBuilder.php +++ b/src/Connection/ImapQueryBuilder.php @@ -11,6 +11,11 @@ class ImapQueryBuilder { + /** + * The largest UID value allowed by IMAP. + */ + protected const MAX_UID = '4294967295'; + /** * The where conditions for the query. */ @@ -270,6 +275,10 @@ public function header(string $header, string $value): static */ public function uid(int|string|array $from, int|float|null $to = null): static { + if ($to === INF) { + $to = self::MAX_UID; + } + return $this->where(ImapSearchKey::Uid, new RawQueryValue(Str::set($from, $to))); } diff --git a/tests/Unit/Connection/ImapQueryBuilderTest.php b/tests/Unit/Connection/ImapQueryBuilderTest.php index ad4b1b5..29edf1f 100644 --- a/tests/Unit/Connection/ImapQueryBuilderTest.php +++ b/tests/Unit/Connection/ImapQueryBuilderTest.php @@ -245,12 +245,12 @@ function (ImapQueryBuilder $q) { expect($builder->toImap())->toBe('UID 2,3,5'); }); -test('compiles UID range to infinity with from and to', function () { +test('compiles UID range to infinity with from and to as a numeric upper bound', function () { $builder = new ImapQueryBuilder; $builder->uid(2, INF); - expect($builder->toImap())->toBe('UID 2:*'); + expect($builder->toImap())->toBe('UID 2:4294967295'); }); test('compiles UID range with upper bound with array', function () { diff --git a/tests/Unit/MessageQueryTest.php b/tests/Unit/MessageQueryTest.php index 88ba964..9f37bb4 100644 --- a/tests/Unit/MessageQueryTest.php +++ b/tests/Unit/MessageQueryTest.php @@ -331,6 +331,26 @@ function query(?Mailbox $mailbox = null): MessageQuery $stream->assertWritten('TAG3 UID STORE 5,6,7,8 +FLAGS.SILENT (\Flagged)'); }); +test('uid range to infinity searches with numeric upper bound', function () { + $stream = new FakeStream; + $stream->open(); + + $stream->feed([ + '* OK Welcome to IMAP', + 'TAG1 OK Logged in', + '* SEARCH', + 'TAG2 OK SEARCH completed', + ]); + + $mailbox = Mailbox::make(); + $mailbox->connect(new ImapConnection($stream)); + + $messages = query($mailbox)->uid(42, INF)->get(); + + expect($messages)->toBeEmpty(); + $stream->assertWritten('TAG2 UID SEARCH UID 42:4294967295'); +}); + test('unmarkFlagged unflags all matching messages', function () { $stream = new FakeStream; $stream->open();