-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathMessageQueryTest.php
More file actions
183 lines (143 loc) · 4.4 KB
/
MessageQueryTest.php
File metadata and controls
183 lines (143 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
use DirectoryTree\ImapEngine\Connection\ImapConnection;
use DirectoryTree\ImapEngine\Connection\ImapQueryBuilder;
use DirectoryTree\ImapEngine\Connection\Streams\FakeStream;
use DirectoryTree\ImapEngine\Folder;
use DirectoryTree\ImapEngine\Mailbox;
use DirectoryTree\ImapEngine\MessageQuery;
function query(?Mailbox $mailbox = null): MessageQuery
{
return new MessageQuery(
new Folder($mailbox ?? new Mailbox, 'test'),
new ImapQueryBuilder
);
}
test('passthru', function () {
$query = query();
expect($query->toImap())->toBe('');
expect($query->isEmpty())->toBeTrue();
});
test('where', function () {
$query = query()
->where('subject', 'hello')
->toImap();
expect($query)->toBe('SUBJECT "hello"');
});
test('destroy', function () {
$stream = new FakeStream;
$stream->open();
$stream->feed([
'* OK Welcome to IMAP',
'TAG1 OK Logged in',
'TAG2 OK UID STORE completed',
]);
$mailbox = Mailbox::make();
$mailbox->connect(new ImapConnection($stream));
query($mailbox)->destroy(1);
$stream->assertWritten('TAG2 UID STORE 1 +FLAGS.SILENT (\Deleted)');
});
test('destroy with multiple messages', function () {
$stream = new FakeStream;
$stream->open();
$stream->feed([
'* OK Welcome to IMAP',
'TAG1 OK Logged in',
'TAG2 OK UID STORE completed',
]);
$mailbox = Mailbox::make();
$mailbox->connect(new ImapConnection($stream));
query($mailbox)->destroy([1, 2, 3]);
$stream->assertWritten('TAG2 UID STORE 1,2,3 +FLAGS.SILENT (\Deleted)');
});
test('oldest sets fetch order to asc', function () {
$query = query();
$query->oldest();
expect($query->getFetchOrder())->toBe('asc');
});
test('newest sets fetch order to desc', function () {
$query = query();
$query->newest();
expect($query->getFetchOrder())->toBe('desc');
});
test('oldest and newest return query instance for chaining', function () {
$query = query();
expect($query->oldest())->toBe($query);
expect($query->newest())->toBe($query);
});
test('each breaks when callback returns false', function () {
$stream = new FakeStream;
$stream->open();
$stream->feed([
'* OK Welcome to IMAP',
'TAG1 OK Logged in',
'* SEARCH 1 2 3 4 5',
'TAG2 OK SEARCH completed',
'* 5 FETCH (UID 5 FLAGS () BODY[HEADER] {0}',
'',
' BODY[TEXT] {0}',
'',
')',
'* 4 FETCH (UID 4 FLAGS () BODY[HEADER] {0}',
'',
' BODY[TEXT] {0}',
'',
')',
'TAG3 OK FETCH completed',
]);
$mailbox = Mailbox::make();
$mailbox->connect(new ImapConnection($stream));
$processedUids = [];
query($mailbox)->each(function ($message) use (&$processedUids) {
$processedUids[] = $message->uid();
// Break after processing the first message (which will be UID 5 due to desc order)
if ($message->uid() === 5) {
return false;
}
}, 2); // Use chunk size of 2
// Should only process the first message (UID 5 due to desc order)
expect($processedUids)->toBe([5]);
});
test('chunk breaks when callback returns false', function () {
$stream = new FakeStream;
$stream->open();
$stream->feed([
'* OK Welcome to IMAP',
'TAG1 OK Logged in',
'* SEARCH 1 2 3 4 5',
'TAG2 OK SEARCH completed',
'* 5 FETCH (UID 5 FLAGS () BODY[HEADER] {0}',
'',
' BODY[TEXT] {0}',
'',
')',
'* 4 FETCH (UID 4 FLAGS () BODY[HEADER] {0}',
'',
' BODY[TEXT] {0}',
'',
')',
'TAG3 OK FETCH completed',
'* 3 FETCH (UID 3 FLAGS () BODY[HEADER] {0}',
'',
' BODY[TEXT] {0}',
'',
')',
'* 2 FETCH (UID 2 FLAGS () BODY[HEADER] {0}',
'',
' BODY[TEXT] {0}',
'',
')',
'TAG4 OK FETCH completed',
]);
$mailbox = Mailbox::make();
$mailbox->connect(new ImapConnection($stream));
$processedChunks = [];
query($mailbox)->chunk(function ($messages, $page) use (&$processedChunks) {
$processedChunks[] = $page;
// Break after processing the first chunk
if ($page === 1) {
return false;
}
}, 2); // Use chunk size of 2
// Should only process the first chunk (page 1)
expect($processedChunks)->toBe([1]);
});