Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/tests/ClientTest.php

raw symbols SCIP
1 <?php
2 /*
3 * File: ClientTest.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 28.12.22 18:11
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Tests;
14
15 use PHPUnit\Framework\MockObject\MockObject;
16 use PHPUnit\Framework\TestCase;
17 use Webklex\PHPIMAP\Client;
18 use Webklex\PHPIMAP\Config;
19 use Webklex\PHPIMAP\Connection\Protocols\ImapProtocol;
20 use Webklex\PHPIMAP\Connection\Protocols\Response;
21 use Webklex\PHPIMAP\Exceptions\AuthFailedException;
22 use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
23 use Webklex\PHPIMAP\Exceptions\ImapBadRequestException;
24 use Webklex\PHPIMAP\Exceptions\ImapServerErrorException;
25 use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
26 use Webklex\PHPIMAP\Exceptions\RuntimeException;
27 use Webklex\PHPIMAP\Folder;
28 use Webklex\PHPIMAP\Support\Masks\AttachmentMask;
29 use Webklex\PHPIMAP\Support\Masks\MessageMask;
30
31 class ClientTest extends TestCase {
32
33 /** @var Client $client */
34 protected Client $client;
35
36 /** @var MockObject ImapProtocol mockup */
37 protected MockObject $protocol;
38
39 /**
40 * Setup the test environment.
41 *
42 * @return void
43 * @throws MaskNotFoundException
44 */
45 public function setUp(): void {
46 $config = Config::make([
47 "accounts" => [
48 "default" => [
49 'protocol' => 'imap',
50 'encryption' => 'ssl',
51 'username' => '[email protected]',
52 'password' => 'bar',
53 'proxy' => [
54 'socket' => null,
55 'request_fulluri' => false,
56 'username' => null,
57 'password' => null,
58 ],
59 ]]
60 ]);
61 $this->client = new Client($config);
62 }
63
64 /**
65 * Client test
66 *
67 * @return void
68 * @throws AuthFailedException
69 * @throws ConnectionFailedException
70 * @throws ImapBadRequestException
71 * @throws ImapServerErrorException
72 * @throws RuntimeException
73 */
74 public function testClient(): void {
75 $this->createNewProtocolMockup();
76
77 self::assertInstanceOf(ImapProtocol::class, $this->client->getConnection());
78 self::assertSame(true, $this->client->isConnected());
79 self::assertSame(false, $this->client->checkConnection());
80 self::assertSame(30, $this->client->getTimeout());
81 self::assertSame(MessageMask::class, $this->client->getDefaultMessageMask());
82 self::assertSame(AttachmentMask::class, $this->client->getDefaultAttachmentMask());
83 self::assertArrayHasKey("new", $this->client->getDefaultEvents("message"));
84 }
85
86 /**
87 * @throws MaskNotFoundException
88 */
89 public function testClientClone(): void {
90 $config = Config::make([
91 "accounts" => [
92 "default" => [
93 'host' => 'example.com',
94 'port' => 993,
95 'protocol' => 'imap', //might also use imap, [pop3 or nntp (untested)]
96 'encryption' => 'ssl', // Supported: false, 'ssl', 'tls'
97 'validate_cert' => true,
98 'username' => '[email protected]',
99 'password' => 'foo',
100 'authentication' => null,
101 'rfc' => 'RFC822', // If you are using iCloud, you might want to set this to 'BODY'
102 'proxy' => [
103 'socket' => null,
104 'request_fulluri' => false,
105 'username' => null,
106 'password' => null,
107 ],
108 "timeout" => 30,
109 "extensions" => []
110 ]]
111 ]);
112 $client = new Client($config);
113 $clone = $client->clone();
114 self::assertInstanceOf(Client::class, $clone);
115 self::assertSame($client->getConfig(), $clone->getConfig());
116 self::assertSame($client->getAccountConfig(), $clone->getAccountConfig());
117 self::assertSame($client->host, $clone->host);
118 }
119
120 public function testClientLogout(): void {
121 $this->createNewProtocolMockup();
122
123 $this->protocol->expects($this->any())->method('logout')->willReturn(Response::empty()->setResponse([
124 0 => "BYE Logging out\r\n",
125 1 => "OK Logout completed (0.001 + 0.000 secs).\r\n",
126 ]));
127 self::assertInstanceOf(Client::class, $this->client->disconnect());
128
129 }
130
131 public function testClientExpunge(): void {
132 $this->createNewProtocolMockup();
133 $this->protocol->expects($this->any())->method('expunge')->willReturn(Response::empty()->setResponse([
134 0 => "OK",
135 1 => "Expunge",
136 2 => "completed",
137 3 => [
138 0 => "0.001",
139 1 => "+",
140 2 => "0.000",
141 3 => "secs).",
142 ],
143 ]));
144 self::assertNotEmpty($this->client->expunge());
145
146 }
147
148 public function testClientFolders(): void {
149 $this->createNewProtocolMockup();
150 $this->protocol->expects($this->any())->method('expunge')->willReturn(Response::empty()->setResponse([
151 0 => "OK",
152 1 => "Expunge",
153 2 => "completed",
154 3 => [
155 0 => "0.001",
156 1 => "+",
157 2 => "0.000",
158 3 => "secs).",
159 ],
160 ]));
161
162 $this->protocol->expects($this->any())->method('selectFolder')->willReturn(Response::empty()->setResponse([
163 "flags" => [
164 0 => [
165 0 => "\Answered",
166 1 => "\Flagged",
167 2 => "\Deleted",
168 3 => "\Seen",
169 4 => "\Draft",
170 5 => "NonJunk",
171 6 => "unknown-1",
172 ],
173 ],
174 "exists" => 139,
175 "recent" => 0,
176 "unseen" => 94,
177 "uidvalidity" => 1488899637,
178 "uidnext" => 278,
179 ]));
180 self::assertNotEmpty($this->client->openFolder("INBOX"));
181 self::assertSame("INBOX", $this->client->getFolderPath());
182
183 $this->protocol->expects($this->any())->method('examineFolder')->willReturn(Response::empty()->setResponse([
184 "flags" => [
185 0 => [
186 0 => "\Answered",
187 1 => "\Flagged",
188 2 => "\Deleted",
189 3 => "\Seen",
190 4 => "\Draft",
191 5 => "NonJunk",
192 6 => "unknown-1",
193 ],
194 ],
195 "exists" => 139,
196 "recent" => 0,
197 "unseen" => 94,
198 "uidvalidity" => 1488899637,
199 "uidnext" => 278,
200 ]));
201 self::assertNotEmpty($this->client->checkFolder("INBOX"));
202
203 $this->protocol->expects($this->any())->method('folders')->with($this->identicalTo(""), $this->identicalTo("*"))->willReturn(Response::empty()->setResponse([
204 "INBOX" => [
205 "delimiter" => ".",
206 "flags" => [
207 0 => "\HasChildren",
208 ],
209 ],
210 "INBOX.new" => [
211 "delimiter" => ".",
212 "flags" => [
213 0 => "\HasNoChildren",
214 ],
215 ],
216 "INBOX.9AL56dEMTTgUKOAz" => [
217 "delimiter" => ".",
218 "flags" => [
219 0 => "\HasNoChildren",
220 ],
221 ],
222 "INBOX.U9PsHCvXxAffYvie" => [
223 "delimiter" => ".",
224 "flags" => [
225 0 => "\HasNoChildren",
226 ],
227 ],
228 "INBOX.Trash" => [
229 "delimiter" => ".",
230 "flags" => [
231 0 => "\HasNoChildren",
232 1 => "\Trash",
233 ],
234 ],
235 "INBOX.processing" => [
236 "delimiter" => ".",
237 "flags" => [
238 0 => "\HasNoChildren",
239 ],
240 ],
241 "INBOX.Sent" => [
242 "delimiter" => ".",
243 "flags" => [
244 0 => "\HasNoChildren",
245 1 => "\Sent",
246 ],
247 ],
248 "INBOX.OzDWCXKV3t241koc" => [
249 "delimiter" => ".",
250 "flags" => [
251 0 => "\HasNoChildren",
252 ],
253 ],
254 "INBOX.5F3bIVTtBcJEqIVe" => [
255 "delimiter" => ".",
256 "flags" => [
257 0 => "\HasNoChildren",
258 ],
259 ],
260 "INBOX.8J3rll6eOBWnTxIU" => [
261 "delimiter" => ".",
262 "flags" => [
263 0 => "\HasNoChildren",
264 ],
265 ],
266 "INBOX.Junk" => [
267 "delimiter" => ".",
268 "flags" => [
269 0 => "\HasNoChildren",
270 1 => "\Junk",
271 ],
272 ],
273 "INBOX.Drafts" => [
274 "delimiter" => ".",
275 "flags" => [
276 0 => "\HasNoChildren",
277 1 => "\Drafts",
278 ],
279 ],
280 "INBOX.test" => [
281 "delimiter" => ".",
282 "flags" => [
283 0 => "\HasNoChildren",
284 ],
285 ],
286 ]));
287
288 $this->protocol->expects($this->any())->method('createFolder')->willReturn(Response::empty()->setResponse([
289 0 => "OK Create completed (0.004 + 0.000 + 0.003 secs).\r\n",
290 ]));
291 self::assertNotEmpty($this->client->createFolder("INBOX.new"));
292
293 $this->protocol->expects($this->any())->method('deleteFolder')->willReturn(Response::empty()->setResponse([
294 0 => "OK Delete completed (0.007 + 0.000 + 0.006 secs).\r\n",
295 ]));
296 self::assertNotEmpty($this->client->deleteFolder("INBOX.new"));
297
298 self::assertInstanceOf(Folder::class, $this->client->getFolderByPath("INBOX.new"));
299 self::assertInstanceOf(Folder::class, $this->client->getFolderByName("new"));
300 self::assertInstanceOf(Folder::class, $this->client->getFolder("INBOX.new", "."));
301 self::assertInstanceOf(Folder::class, $this->client->getFolder("new"));
302 }
303
304 public function testClientId(): void {
305 $this->createNewProtocolMockup();
306 $this->protocol->expects($this->any())->method('ID')->willReturn(Response::empty()->setResponse([
307 0 => "ID (\"name\" \"Dovecot\")\r\n",
308 1 => "OK ID completed (0.001 + 0.000 secs).\r\n"
309
310 ]));
311 self::assertSame("ID (\"name\" \"Dovecot\")\r\n", $this->client->Id()[0]);
312
313 }
314
315 public function testClientConfig(): void {
316 $config = $this->client->getConfig()->get("accounts.".$this->client->getConfig()->getDefaultAccount());
317 self::assertSame("[email protected]", $config["username"]);
318 self::assertSame("bar", $config["password"]);
319 self::assertSame("localhost", $config["host"]);
320 self::assertSame(true, $config["validate_cert"]);
321 self::assertSame(993, $config["port"]);
322
323 $this->client->getConfig()->set("accounts.".$this->client->getConfig()->getDefaultAccount(), [
324 "host" => "domain.tld",
325 'password' => 'bar',
326 ]);
327 $config = $this->client->getConfig()->get("accounts.".$this->client->getConfig()->getDefaultAccount());
328
329 self::assertSame("bar", $config["password"]);
330 self::assertSame("domain.tld", $config["host"]);
331 self::assertSame(true, $config["validate_cert"]);
332 }
333
334 protected function createNewProtocolMockup() {
335 $this->protocol = $this->createMock(ImapProtocol::class);
336
337 $this->protocol->expects($this->any())->method('connected')->willReturn(true);
338 $this->protocol->expects($this->any())->method('getConnectionTimeout')->willReturn(30);
339
340 $this->protocol
341 ->expects($this->any())
342 ->method('createStream')
343 //->will($this->onConsecutiveCalls(true));
344 ->willReturn(true);
345
346 $this->client->connection = $this->protocol;
347 }
348 }