-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathImapQueryBuilderTest.php
More file actions
230 lines (160 loc) · 6.11 KB
/
ImapQueryBuilderTest.php
File metadata and controls
230 lines (160 loc) · 6.11 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
<?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-"');
});