-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathImapQueryBuilderTest.php
More file actions
322 lines (221 loc) · 8.75 KB
/
ImapQueryBuilderTest.php
File metadata and controls
322 lines (221 loc) · 8.75 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
use Carbon\Carbon;
use DirectoryTree\ImapEngine\Connection\ImapQueryBuilder;
use DirectoryTree\ImapEngine\Connection\RawQueryValue;
use DirectoryTree\ImapEngine\Enums\ImapSearchKey;
test('returns an empty string if no conditions are provided', function () {
$builder = new ImapQueryBuilder;
expect($builder->toImap())->toBe('');
});
test('compiles a single basic where condition', function () {
$builder = new ImapQueryBuilder;
$builder->where('subject', 'hello');
expect($builder->toImap())->toBe('SUBJECT "hello"');
});
test('compiles a single criteria', function () {
$builder = new ImapQueryBuilder;
$builder->where(ImapSearchKey::All);
expect($builder->toImap())->toBe('ALL');
});
test('compiles a single not criteria', function () {
$builder = new ImapQueryBuilder;
$builder->whereNot(ImapSearchKey::All);
expect($builder->toImap())->toBe('NOT ALL');
});
test('compiles multiple AND conditions', function () {
$builder = new ImapQueryBuilder;
$builder
->where('subject', 'hello')
->where('from', 'me');
expect($builder->toImap())->toBe('SUBJECT "hello" FROM "me"');
});
test('compiles an OR condition', function () {
$builder = new ImapQueryBuilder;
$builder
->where('subject', 'hello')
->orWhere('subject', 'world');
expect($builder->toImap())->toBe('OR (SUBJECT "hello") (SUBJECT "world")');
});
test('compiles a NOT condition', function () {
$builder = new ImapQueryBuilder;
$builder->whereNot('subject', 'junk');
expect($builder->toImap())->toBe('NOT SUBJECT "junk"');
});
test('compiles nested conditions with AND by default', function () {
$builder = new ImapQueryBuilder;
$builder->where('subject', 'test')->where(
function (ImapQueryBuilder $q) {
$q
->where('from', 'someone')
->orWhere('from', 'somebody');
}
);
expect($builder->toImap())->toBe('SUBJECT "test" OR (FROM "someone") (FROM "somebody")');
});
test('compiles complex nested conditions', function () {
$builder = new ImapQueryBuilder;
$builder->where(function (ImapQueryBuilder $q) {
$q
->where('subject', 'foo')
->orWhere('subject', 'bar');
})->orWhere(function (ImapQueryBuilder $q) {
$q
->where('from', 'someone')
->whereNot('subject', 'junk');
});
expect($builder->toImap())->toBe(
'OR (OR (SUBJECT "foo") (SUBJECT "bar")) (FROM "someone" NOT SUBJECT "junk")'
);
});
test('compiles an empty string value', function () {
$builder = new ImapQueryBuilder;
$builder->where('subject', ''); // empty string
expect($builder->toImap())->toBe('SUBJECT');
});
test('compiles a null value', function () {
$builder = new ImapQueryBuilder;
$builder->where('subject', null); // null value
expect($builder->toImap())->toBe('SUBJECT');
});
test('compiles a NOT condition with null value', function () {
$builder = new ImapQueryBuilder;
$builder->whereNot('subject', null); // null value
expect($builder->toImap())->toBe('NOT SUBJECT');
});
test('compiles nested closure that has no conditions', function () {
$builder = new ImapQueryBuilder;
$builder->where('subject', 'test')
->where(function (ImapQueryBuilder $q) {
// no conditions
});
expect($builder->toImap())->toBe('SUBJECT "test"');
});
test('compiles deeply nested closures', function () {
$builder = new ImapQueryBuilder;
$builder->where('subject', 'level1')
->orWhere(function (ImapQueryBuilder $q) {
$q->where('subject', 'level2')
->orWhere(function (ImapQueryBuilder $q2) {
$q2->where('subject', 'level3');
});
});
expect($builder->toImap())->toBe(
'OR (SUBJECT "level1") (OR (SUBJECT "level2") (SUBJECT "level3"))'
);
});
test('compiles multiple orWhere calls', function () {
$builder = new ImapQueryBuilder;
$builder->where('subject', 'first')
->orWhere('subject', 'second')
->orWhere('subject', 'third');
expect($builder->toImap())->toBe(
'OR (OR (SUBJECT "first") (SUBJECT "second")) (SUBJECT "third")'
);
});
test('compiles multiple conditions on the same column', function () {
$builder = new ImapQueryBuilder;
$builder->where('subject', 'foo')
->where('subject', 'bar')
->orWhere('subject', 'baz');
expect($builder->toImap())->toBe(
'OR (SUBJECT "foo" SUBJECT "bar") (SUBJECT "baz")'
);
});
test('escapes double quotes in search value', function () {
$builder = new ImapQueryBuilder;
$builder->where('subject', 'He said "Hello"');
expect($builder->toImap())->toBe('SUBJECT "He said \\"Hello\\""');
});
test('removes newlines and carriage returns in search value', function () {
$builder = new ImapQueryBuilder;
$builder->where('body', "Line one\nLine two\rLine three");
expect($builder->toImap())->toBe('BODY "Line oneLine twoLine three"');
});
test('escapes multiple special characters', function () {
$builder = new ImapQueryBuilder;
// Special characters: double quotes, newlines, backslash
$value = "Foo \"Bar\"\nBaz\\Zot";
$builder->where('subject', $value);
expect($builder->toImap())->toBe('SUBJECT "Foo \\"Bar\\"Baz\\\\Zot"');
});
test('compiles a SINCE condition with unquoted date', function () {
$builder = new ImapQueryBuilder;
$builder->since(Carbon::create(2024, 4, 4));
expect($builder->toImap())->toBe('SINCE 04-Apr-2024');
});
test('compiles a SINCE condition with quoted date', function () {
$builder = new ImapQueryBuilder;
$builder->where('since', Carbon::create(2024, 4, 4));
expect($builder->toImap())->toBe('SINCE "04-Apr-2024"');
});
test('compiles raw value', function () {
$builder = new ImapQueryBuilder;
$builder->where('foo', new RawQueryValue('bar'));
expect($builder->toImap())->toBe('FOO bar');
});
test('converts values from utf-8 to utf-7', function () {
$builder = new ImapQueryBuilder;
$builder->where('foo', 'Joué');
expect($builder->toImap())->toBe('FOO "Jou&AOk-"');
});
test('compiles UID condition without quotes', function () {
$builder = new ImapQueryBuilder;
$builder->uid(2);
expect($builder->toImap())->toBe('UID 2');
});
test('compiles multiple UID values without quotes', function () {
$builder = new ImapQueryBuilder;
$builder->uid([2, 3, 5]);
expect($builder->toImap())->toBe('UID 2,3,5');
});
test('compiles UID range to infinity with from and to', function () {
$builder = new ImapQueryBuilder;
$builder->uid(2, INF);
expect($builder->toImap())->toBe('UID 2:*');
});
test('compiles UID range with upper bound with array', function () {
$builder = new ImapQueryBuilder;
$builder->uid([2, 5]);
expect($builder->toImap())->toBe('UID 2,5');
});
test('compiles UID range with upper bound with from and to', function () {
$builder = new ImapQueryBuilder;
$builder->uid(2, 5);
expect($builder->toImap())->toBe('UID 2:5');
});
test('compiles UID range with single value', function () {
$builder = new ImapQueryBuilder;
$builder->uid(2);
expect($builder->toImap())->toBe('UID 2');
});
test('compiles UID range with single value array', function () {
$builder = new ImapQueryBuilder;
$builder->uid([2]);
expect($builder->toImap())->toBe('UID 2');
});
test('compiles LARGER condition without quotes', function () {
expect((new ImapQueryBuilder)->larger(5242880)->toImap())->toBe('LARGER 5242880');
});
test('compiles SMALLER condition without quotes', function () {
expect((new ImapQueryBuilder)->smaller(10240)->toImap())->toBe('SMALLER 10240');
});
test('compiles LARGER and SMALLER conditions together', function () {
$builder = new ImapQueryBuilder;
$builder->larger(1024)->smaller(1048576);
expect($builder->toImap())->toBe('LARGER 1024 SMALLER 1048576');
});
test('compiles KEYWORD condition', function () {
expect((new ImapQueryBuilder)->keyword('important')->toImap())->toBe('KEYWORD "important"');
});
test('compiles UNKEYWORD condition', function () {
expect((new ImapQueryBuilder)->unkeyword('important')->toImap())->toBe('UNKEYWORD "important"');
});
test('compiles a SENTON condition with unquoted date', function () {
expect((new ImapQueryBuilder)->sentOn(Carbon::create(2024, 4, 4))->toImap())->toBe('SENTON 04-Apr-2024');
});
test('compiles a SENTSINCE condition with unquoted date', function () {
expect((new ImapQueryBuilder)->sentSince(Carbon::create(2024, 4, 4))->toImap())->toBe('SENTSINCE 04-Apr-2024');
});
test('compiles a SENTBEFORE condition with unquoted date', function () {
expect((new ImapQueryBuilder)->sentBefore(Carbon::create(2024, 4, 4))->toImap())->toBe('SENTBEFORE 04-Apr-2024');
});