Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 /*
3 * File: ClientTest.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 04.03.23 03:52
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Tests\live;
14
15 use Webklex\PHPIMAP\Client;
16 use Webklex\PHPIMAP\Connection\Protocols\ProtocolInterface;
17 use Webklex\PHPIMAP\EncodingAliases;
18 use Webklex\PHPIMAP\Exceptions\AuthFailedException;
19 use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
20 use Webklex\PHPIMAP\Exceptions\FolderFetchingException;
21 use Webklex\PHPIMAP\Exceptions\ImapBadRequestException;
22 use Webklex\PHPIMAP\Exceptions\ImapServerErrorException;
23 use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
24 use Webklex\PHPIMAP\Exceptions\ResponseException;
25 use Webklex\PHPIMAP\Exceptions\RuntimeException;
26 use Webklex\PHPIMAP\Folder;
27 use Webklex\PHPIMAP\Support\Masks\AttachmentMask;
28 use Webklex\PHPIMAP\Support\Masks\MessageMask;
29
30 /**
31 * Class ClientTest
32 *
33 * @package Tests
34 */
35 class ClientTest extends LiveMailboxTestCase {
36
37 /**
38 * Test if the connection is working
39 *
40 * @return void
41 * @throws AuthFailedException
42 * @throws ConnectionFailedException
43 * @throws ImapBadRequestException
44 * @throws ImapServerErrorException
45 * @throws ResponseException
46 * @throws RuntimeException
47 * @throws MaskNotFoundException
48 */
49 public function testConnect(): void {
50 self::assertNotNull($this->getClient()->connect());
51 }
52
53 /**
54 * Test if the connection is working
55 *
56 * @return void
57 * @throws AuthFailedException
58 * @throws ConnectionFailedException
59 * @throws ImapBadRequestException
60 * @throws ImapServerErrorException
61 * @throws MaskNotFoundException
62 * @throws ResponseException
63 * @throws RuntimeException
64 */
65 public function testIsConnected(): void {
66 $client = $this->getClient()->connect();
67
68 self::assertTrue($client->isConnected());
69 }
70
71 /**
72 * Test if the connection state can be determined
73 *
74 * @return void
75 * @throws AuthFailedException
76 * @throws ConnectionFailedException
77 * @throws ImapBadRequestException
78 * @throws ImapServerErrorException
79 * @throws MaskNotFoundException
80 * @throws ResponseException
81 * @throws RuntimeException
82 */
83 public function testDisconnect(): void {
84 $client = $this->getClient()->connect();
85
86 self::assertFalse($client->disconnect()->isConnected());
87 }
88
89 /**
90 * Test to get the default inbox folder
91 *
92 * @return void
93 * @throws AuthFailedException
94 * @throws ConnectionFailedException
95 * @throws ImapBadRequestException
96 * @throws ImapServerErrorException
97 * @throws MaskNotFoundException
98 * @throws ResponseException
99 * @throws RuntimeException
100 * @throws FolderFetchingException
101 */
102 public function testGetFolder(): void {
103 $client = $this->getClient()->connect();
104
105 $folder = $client->getFolder('INBOX');
106 self::assertInstanceOf(Folder::class, $folder);
107 }
108
109 /**
110 * Test to get the default inbox folder by name
111 *
112 * @return void
113 * @throws AuthFailedException
114 * @throws ConnectionFailedException
115 * @throws FolderFetchingException
116 * @throws ImapBadRequestException
117 * @throws ImapServerErrorException
118 * @throws MaskNotFoundException
119 * @throws ResponseException
120 * @throws RuntimeException
121 */
122 public function testGetFolderByName(): void {
123 $client = $this->getClient()->connect();
124
125 $folder = $client->getFolderByName('INBOX');
126 self::assertInstanceOf(Folder::class, $folder);
127 }
128
129 /**
130 * Test to get the default inbox folder by path
131 *
132 * @return void
133 * @throws AuthFailedException
134 * @throws ConnectionFailedException
135 * @throws FolderFetchingException
136 * @throws ImapBadRequestException
137 * @throws ImapServerErrorException
138 * @throws MaskNotFoundException
139 * @throws ResponseException
140 * @throws RuntimeException
141 */
142 public function testGetFolderByPath(): void {
143 $client = $this->getClient()->connect();
144
145 $folder = $client->getFolderByPath('INBOX');
146 self::assertInstanceOf(Folder::class, $folder);
147 }
148
149 /**
150 * Test to get all folders
151 *
152 * @return void
153 * @throws AuthFailedException
154 * @throws ConnectionFailedException
155 * @throws FolderFetchingException
156 * @throws ImapBadRequestException
157 * @throws ImapServerErrorException
158 * @throws MaskNotFoundException
159 * @throws ResponseException
160 * @throws RuntimeException
161 */
162 public function testGetFolders(): void {
163 $client = $this->getClient()->connect();
164
165 $folders = $client->getFolders(false);
166 self::assertTrue($folders->count() > 0);
167 }
168
169 public function testGetFoldersWithStatus(): void {
170 $client = $this->getClient()->connect();
171
172 $folders = $client->getFoldersWithStatus(false);
173 self::assertTrue($folders->count() > 0);
174 }
175
176 public function testOpenFolder(): void {
177 $client = $this->getClient()->connect();
178
179 $status = $client->openFolder("INBOX");
180 self::assertTrue(isset($status["flags"]) && count($status["flags"]) > 0);
181 self::assertTrue(($status["uidnext"] ?? 0) > 0);
182 self::assertTrue(($status["uidvalidity"] ?? 0) > 0);
183 self::assertTrue(($status["recent"] ?? -1) >= 0);
184 self::assertTrue(($status["exists"] ?? -1) >= 0);
185 }
186
187 public function testCreateFolder(): void {
188 $client = $this->getClient()->connect();
189
190 $delimiter = $this->getManager()->getConfig()->get("options.delimiter");
191 $folder_path = implode($delimiter, ['INBOX', $this->getSpecialChars()]);
192
193 $folder = $client->getFolder($folder_path);
194
195 $this->deleteFolder($folder);
196
197 $folder = $client->createFolder($folder_path, false);
198 self::assertInstanceOf(Folder::class, $folder);
199
200 $folder = $this->getFolder($folder_path);
201 self::assertInstanceOf(Folder::class, $folder);
202
203 $this->assertEquals($this->getSpecialChars(), $folder->name);
204 $this->assertEquals($folder_path, $folder->full_name);
205
206 $folder_path = implode($delimiter, ['INBOX', EncodingAliases::convert($this->getSpecialChars(), "utf-8", "utf7-imap")]);
207 $this->assertEquals($folder_path, $folder->path);
208
209 // Clean up
210 if ($this->deleteFolder($folder) === false) {
211 $this->fail("Could not delete folder: " . $folder->path);
212 }
213 }
214
215 public function testCheckFolder(): void {
216 $client = $this->getClient()->connect();
217
218 $status = $client->checkFolder("INBOX");
219 self::assertTrue(isset($status["flags"]) && count($status["flags"]) > 0);
220 self::assertTrue(($status["uidnext"] ?? 0) > 0);
221 self::assertTrue(($status["uidvalidity"] ?? 0) > 0);
222 self::assertTrue(($status["recent"] ?? -1) >= 0);
223 self::assertTrue(($status["exists"] ?? -1) >= 0);
224 }
225
226 public function testGetFolderPath(): void {
227 $client = $this->getClient()->connect();
228
229 self::assertIsArray($client->openFolder("INBOX"));
230 self::assertEquals("INBOX", $client->getFolderPath());
231 }
232
233 public function testId(): void {
234 $client = $this->getClient()->connect();
235
236 $info = $client->Id();
237 self::assertIsArray($info);
238 $valid = false;
239 foreach ($info as $value) {
240 if (str_starts_with($value, "OK")) {
241 $valid = true;
242 break;
243 }
244 }
245 self::assertTrue($valid);
246 }
247
248 public function testGetQuotaRoot(): void {
249 if (!getenv("LIVE_MAILBOX_QUOTA_SUPPORT")) {
250 $this->markTestSkipped("Quota support is not enabled");
251 }
252
253 $client = $this->getClient()->connect();
254
255 $quota = $client->getQuotaRoot("INBOX");
256 self::assertIsArray($quota);
257 self::assertTrue(count($quota) > 1);
258 self::assertIsArray($quota[0]);
259 self::assertEquals("INBOX", $quota[0][1]);
260 self::assertIsArray($quota[1]);
261 self::assertIsArray($quota[1][2]);
262 self::assertTrue($quota[1][2][2] > 0);
263 }
264
265 public function testSetTimeout(): void {
266 $client = $this->getClient()->connect();
267
268 self::assertInstanceOf(ProtocolInterface::class, $client->setTimeout(57));
269 self::assertEquals(57, $client->getTimeout());
270 }
271
272 public function testExpunge(): void {
273 $client = $this->getClient()->connect();
274
275 $client->openFolder("INBOX");
276 $status = $client->expunge();
277
278 self::assertIsArray($status);
279 self::assertIsArray($status[0]);
280 self::assertEquals("OK", $status[0][0]);
281 }
282
283 public function testGetDefaultMessageMask(): void {
284 $client = $this->getClient();
285
286 self::assertEquals(MessageMask::class, $client->getDefaultMessageMask());
287 }
288
289 public function testGetDefaultEvents(): void {
290 $client = $this->getClient();
291
292 self::assertIsArray($client->getDefaultEvents("message"));
293 }
294
295 public function testSetDefaultMessageMask(): void {
296 $client = $this->getClient();
297
298 self::assertInstanceOf(Client::class, $client->setDefaultMessageMask(AttachmentMask::class));
299 self::assertEquals(AttachmentMask::class, $client->getDefaultMessageMask());
300
301 $client->setDefaultMessageMask(MessageMask::class);
302 }
303
304 public function testGetDefaultAttachmentMask(): void {
305 $client = $this->getClient();
306
307 self::assertEquals(AttachmentMask::class, $client->getDefaultAttachmentMask());
308 }
309
310 public function testSetDefaultAttachmentMask(): void {
311 $client = $this->getClient();
312
313 self::assertInstanceOf(Client::class, $client->setDefaultAttachmentMask(MessageMask::class));
314 self::assertEquals(MessageMask::class, $client->getDefaultAttachmentMask());
315
316 $client->setDefaultAttachmentMask(AttachmentMask::class);
317 }
318 }