Skip to content

Commit d545ff3

Browse files
authored
Merge pull request #159 from DirectoryTree/bug-157
Fix UID infinity search range returning last message
2 parents 703ef29 + 3df20af commit d545ff3

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/Connection/ImapQueryBuilder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
class ImapQueryBuilder
1313
{
14+
/**
15+
* The largest UID value allowed by IMAP.
16+
*/
17+
protected const MAX_UID = '4294967295';
18+
1419
/**
1520
* The where conditions for the query.
1621
*/
@@ -270,6 +275,10 @@ public function header(string $header, string $value): static
270275
*/
271276
public function uid(int|string|array $from, int|float|null $to = null): static
272277
{
278+
if ($to === INF) {
279+
$to = self::MAX_UID;
280+
}
281+
273282
return $this->where(ImapSearchKey::Uid, new RawQueryValue(Str::set($from, $to)));
274283
}
275284

tests/Unit/Connection/ImapQueryBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,12 @@ function (ImapQueryBuilder $q) {
245245
expect($builder->toImap())->toBe('UID 2,3,5');
246246
});
247247

248-
test('compiles UID range to infinity with from and to', function () {
248+
test('compiles UID range to infinity with from and to as a numeric upper bound', function () {
249249
$builder = new ImapQueryBuilder;
250250

251251
$builder->uid(2, INF);
252252

253-
expect($builder->toImap())->toBe('UID 2:*');
253+
expect($builder->toImap())->toBe('UID 2:4294967295');
254254
});
255255

256256
test('compiles UID range with upper bound with array', function () {

tests/Unit/MessageQueryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,26 @@ function query(?Mailbox $mailbox = null): MessageQuery
331331
$stream->assertWritten('TAG3 UID STORE 5,6,7,8 +FLAGS.SILENT (\Flagged)');
332332
});
333333

334+
test('uid range to infinity searches with numeric upper bound', function () {
335+
$stream = new FakeStream;
336+
$stream->open();
337+
338+
$stream->feed([
339+
'* OK Welcome to IMAP',
340+
'TAG1 OK Logged in',
341+
'* SEARCH',
342+
'TAG2 OK SEARCH completed',
343+
]);
344+
345+
$mailbox = Mailbox::make();
346+
$mailbox->connect(new ImapConnection($stream));
347+
348+
$messages = query($mailbox)->uid(42, INF)->get();
349+
350+
expect($messages)->toBeEmpty();
351+
$stream->assertWritten('TAG2 UID SEARCH UID 42:4294967295');
352+
});
353+
334354
test('unmarkFlagged unflags all matching messages', function () {
335355
$stream = new FakeStream;
336356
$stream->open();

0 commit comments

Comments
 (0)