|
6 | 6 | use DirectoryTree\ImapEngine\Connection\Loggers\LoggerInterface; |
7 | 7 | use DirectoryTree\ImapEngine\Connection\Responses\ContinuationResponse; |
8 | 8 | use DirectoryTree\ImapEngine\Connection\Responses\Data\Data; |
| 9 | +use DirectoryTree\ImapEngine\Connection\Responses\Data\ListData; |
9 | 10 | use DirectoryTree\ImapEngine\Connection\Responses\Response; |
10 | 11 | use DirectoryTree\ImapEngine\Connection\Responses\TaggedResponse; |
11 | 12 | use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse; |
| 13 | +use DirectoryTree\ImapEngine\Connection\Streams\FakeStream; |
12 | 14 | use DirectoryTree\ImapEngine\Connection\Streams\StreamInterface; |
13 | 15 | use DirectoryTree\ImapEngine\Connection\Tokens\Token; |
14 | 16 | use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier; |
@@ -49,6 +51,20 @@ public function __construct( |
49 | 51 | protected ?LoggerInterface $logger = null, |
50 | 52 | ) {} |
51 | 53 |
|
| 54 | + /** |
| 55 | + * Create a new connection with a fake stream. |
| 56 | + */ |
| 57 | + public static function fake(array $responses = []): static |
| 58 | + { |
| 59 | + $stream = new FakeStream; |
| 60 | + |
| 61 | + $stream->open(); |
| 62 | + |
| 63 | + $stream->feed($responses); |
| 64 | + |
| 65 | + return new static($stream); |
| 66 | + } |
| 67 | + |
52 | 68 | /** |
53 | 69 | * Tear down the connection. |
54 | 70 | */ |
@@ -602,13 +618,18 @@ public function fetch(array|string $items, array|int $from, mixed $to = null, Im |
602 | 618 | // << * 123 FETCH (UID 456 BODY[TEXT] {14}\nHello, World!) |
603 | 619 | // << * 123 FETCH (FLAGS (\Seen)) <-- Unsolicited response |
604 | 620 | return $this->result->responses()->untagged()->filter(function (UntaggedResponse $response) use ($items, $identifier) { |
605 | | - // The third token will always be a list of data items. |
| 621 | + // Skip over any untagged responses that are not FETCH responses. |
| 622 | + // The third token should always be the list of data items. |
| 623 | + if (! ($data = $response->tokenAt(3)) instanceof ListData) { |
| 624 | + return false; |
| 625 | + } |
| 626 | + |
606 | 627 | return match ($identifier) { |
607 | 628 | // If we're fetching UIDs, we can check if a UID token is contained in the list. |
608 | | - ImapFetchIdentifier::Uid => $response->tokenAt(3)->contains('UID'), |
| 629 | + ImapFetchIdentifier::Uid => $data->contains('UID'), |
609 | 630 |
|
610 | 631 | // If we're fetching message numbers, we can check if the requested items are all contained in the list. |
611 | | - ImapFetchIdentifier::MessageNumber => $response->tokenAt(3)->contains($items), |
| 632 | + ImapFetchIdentifier::MessageNumber => $data->contains($items), |
612 | 633 | }; |
613 | 634 | }); |
614 | 635 | } |
|
0 commit comments