-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBodyStructureTest.php
More file actions
200 lines (152 loc) · 8.05 KB
/
BodyStructureTest.php
File metadata and controls
200 lines (152 loc) · 8.05 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
<?php
use DirectoryTree\ImapEngine\BodyStructureCollection;
use DirectoryTree\ImapEngine\BodyStructurePart;
use DirectoryTree\ImapEngine\Connection\ImapParser;
use DirectoryTree\ImapEngine\Connection\ImapTokenizer;
use DirectoryTree\ImapEngine\Connection\Responses\Data\ListData;
use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse;
use DirectoryTree\ImapEngine\Connection\Streams\FakeStream;
function parseBodyStructureResponse(string $response): ListData
{
$stream = new FakeStream;
$stream->open();
$stream->feed([$response]);
$tokenizer = new ImapTokenizer($stream);
$parser = new ImapParser($tokenizer);
$parsed = $parser->next();
expect($parsed)->toBeInstanceOf(UntaggedResponse::class);
$data = $parsed->tokenAt(3);
expect($data)->toBeInstanceOf(ListData::class);
return $data->lookup('BODYSTRUCTURE');
}
test('it parses a simple text/plain message as BodyStructurePart', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE ("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 100 5 NIL NIL NIL) UID 1)'
);
$part = BodyStructurePart::fromListData($listData);
expect($part)->toBeInstanceOf(BodyStructurePart::class);
expect($part->contentType())->toBe('text/plain');
expect($part->charset())->toBe('utf-8');
expect($part->encoding())->toBe('7bit');
expect($part->size())->toBe(100);
expect($part->lines())->toBe(5);
expect($part->partNumber())->toBe('1');
});
test('it parses a multipart/alternative message as BodyStructureCollection', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "utf-8") NIL NIL "quoted-printable" 11 1 NIL NIL NIL) ("text" "html" ("charset" "utf-8") NIL NIL "quoted-printable" 18 1 NIL NIL NIL) "alternative" ("boundary" "Aq14h3UL") NIL NIL) UID 1)'
);
$collection = BodyStructureCollection::fromListData($listData);
expect($collection)->toBeInstanceOf(BodyStructureCollection::class);
expect($collection->contentType())->toBe('multipart/alternative');
expect($collection->parts())->toHaveCount(2);
$textPart = $collection->text();
expect($textPart)->not->toBeNull();
expect($textPart->contentType())->toBe('text/plain');
expect($textPart->partNumber())->toBe('1');
$htmlPart = $collection->html();
expect($htmlPart)->not->toBeNull();
expect($htmlPart->contentType())->toBe('text/html');
expect($htmlPart->partNumber())->toBe('2');
});
test('it flattens all parts in a collection', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 100 5 NIL NIL NIL) ("text" "html" ("charset" "utf-8") NIL NIL "7bit" 200 10 NIL NIL NIL) "alternative" ("boundary" "abc") NIL NIL) UID 1)'
);
$collection = BodyStructureCollection::fromListData($listData);
$parts = $collection->flatten();
expect($parts)->toHaveCount(2);
expect($parts[0]->isText())->toBeTrue();
expect($parts[1]->isHtml())->toBeTrue();
});
test('it finds part by part number in a collection', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 100 5 NIL NIL NIL) ("text" "html" ("charset" "utf-8") NIL NIL "7bit" 200 10 NIL NIL NIL) "alternative" ("boundary" "abc") NIL NIL) UID 1)'
);
$collection = BodyStructureCollection::fromListData($listData);
$part = $collection->find('2');
expect($part)->not->toBeNull();
expect($part->isHtml())->toBeTrue();
$notFound = $collection->find('99');
expect($notFound)->toBeNull();
});
test('it detects attachments in a collection', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 100 5 NIL NIL NIL) ("application" "pdf" ("name" "document.pdf") NIL NIL "base64" 5000 NIL ("attachment" ("filename" "document.pdf")) NIL NIL) "mixed" ("boundary" "abc") NIL NIL) UID 1)'
);
$collection = BodyStructureCollection::fromListData($listData);
expect($collection->hasAttachments())->toBeTrue();
expect($collection->attachmentCount())->toBe(1);
$attachments = $collection->attachments();
expect($attachments[0]->filename())->toBe('document.pdf');
expect($attachments[0]->contentType())->toBe('application/pdf');
});
test('it converts BodyStructurePart to array', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE ("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 100 5 NIL NIL NIL) UID 1)'
);
$part = BodyStructurePart::fromListData($listData);
$array = $part->toArray();
expect($array)->toBeArray();
expect($array['type'])->toBe('text');
expect($array['subtype'])->toBe('plain');
expect($array['content_type'])->toBe('text/plain');
});
test('it converts BodyStructureCollection to array', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 100 5 NIL NIL NIL) ("text" "html" ("charset" "utf-8") NIL NIL "7bit" 200 10 NIL NIL NIL) "alternative" ("boundary" "abc") NIL NIL) UID 1)'
);
$collection = BodyStructureCollection::fromListData($listData);
$array = $collection->toArray();
expect($array)->toBeArray();
expect($array['content_type'])->toBe('multipart/alternative');
expect($array['subtype'])->toBe('alternative');
expect($array['parts'])->toHaveCount(2);
});
test('it identifies inline disposition in body structure part', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE (("text" "html" ("charset" "utf-8") NIL NIL "7bit" 100 5 NIL NIL NIL) ("image" "png" ("name" "logo.png") "<cid123>" NIL "base64" 1000 NIL ("inline" ("filename" "logo.png")) NIL NIL) "related" ("boundary" "abc") NIL NIL) UID 1)'
);
$collection = BodyStructureCollection::fromListData($listData);
$parts = $collection->flatten();
$imagePart = $parts[1];
expect($imagePart->isInline())->toBeTrue();
expect($imagePart->isAttachment())->toBeFalse();
expect($imagePart->id())->toBe('<cid123>');
});
test('it does not emit warnings when non-scalar tokens appear in message/rfc822 structure fields', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE ("MESSAGE" "RFC822" NIL NIL NIL "7BIT" 3456 (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) ("TEXT" "PLAIN" ("charset" "utf-8") NIL NIL "7BIT" 100 10 NIL NIL NIL) 42) UID 1)'
);
set_error_handler(static function (int $severity, string $message, string $file, int $line): never {
throw new ErrorException($message, 0, $severity, $file, $line);
});
try {
$part = BodyStructurePart::fromListData($listData);
} finally {
restore_error_handler();
}
expect($part->contentType())->toBe('message/rfc822');
expect($part->size())->toBe(3456);
expect($part->lines())->toBeNull();
});
test('it makes BodyStructureCollection countable', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 100 5 NIL NIL NIL) ("text" "html" ("charset" "utf-8") NIL NIL "7bit" 200 10 NIL NIL NIL) "alternative" ("boundary" "abc") NIL NIL) UID 1)'
);
$collection = BodyStructureCollection::fromListData($listData);
expect(count($collection))->toBe(2);
});
test('it makes BodyStructureCollection iterable', function () {
$listData = parseBodyStructureResponse(
'* 1 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 100 5 NIL NIL NIL) ("text" "html" ("charset" "utf-8") NIL NIL "7bit" 200 10 NIL NIL NIL) "alternative" ("boundary" "abc") NIL NIL) UID 1)'
);
$collection = BodyStructureCollection::fromListData($listData);
$parts = [];
foreach ($collection as $part) {
$parts[] = $part;
}
expect($parts)->toHaveCount(2);
expect($parts[0])->toBeInstanceOf(BodyStructurePart::class);
expect($parts[1])->toBeInstanceOf(BodyStructurePart::class);
});