Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 /*
3 * File: LiveMailboxTestCase.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 04.03.23 03:43
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Tests\live;
14
15 use PHPUnit\Framework\TestCase;
16 use Webklex\PHPIMAP\Client;
17 use Webklex\PHPIMAP\ClientManager;
18 use Webklex\PHPIMAP\Exceptions\AuthFailedException;
19 use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
20 use Webklex\PHPIMAP\Exceptions\EventNotFoundException;
21 use Webklex\PHPIMAP\Exceptions\FolderFetchingException;
22 use Webklex\PHPIMAP\Exceptions\ImapBadRequestException;
23 use Webklex\PHPIMAP\Exceptions\ImapServerErrorException;
24 use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
25 use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
26 use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException;
27 use Webklex\PHPIMAP\Exceptions\MessageFlagException;
28 use Webklex\PHPIMAP\Exceptions\MessageHeaderFetchingException;
29 use Webklex\PHPIMAP\Exceptions\ResponseException;
30 use Webklex\PHPIMAP\Exceptions\RuntimeException;
31 use Webklex\PHPIMAP\Folder;
32 use Webklex\PHPIMAP\Message;
33
34 /**
35 * Class LiveMailboxTestCase
36 *
37 * @package Tests
38 */
39 abstract class LiveMailboxTestCase extends TestCase {
40
41 /**
42 * Special chars
43 */
44 const SPECIAL_CHARS = 'A_\\|!"£$%&()=?àèìòùÀÈÌÒÙ<>-@#[]_ß_б_π_€_✔_你_يد_Z_';
45
46 /**
47 * Client manager
48 * @var ClientManager $manager
49 */
50 protected static ClientManager $manager;
51
52 /**
53 * Get the client manager
54 *
55 * @return ClientManager
56 */
57 final protected function getManager(): ClientManager {
58 if (!isset(self::$manager)) {
59 self::$manager = new ClientManager([
60 'options' => [
61 "debug" => $_ENV["LIVE_MAILBOX_DEBUG"] ?? false,
62 ],
63 'accounts' => [
64 'default' => [
65 'host' => getenv("LIVE_MAILBOX_HOST"),
66 'port' => getenv("LIVE_MAILBOX_PORT"),
67 'encryption' => getenv("LIVE_MAILBOX_ENCRYPTION"),
68 'validate_cert' => getenv("LIVE_MAILBOX_VALIDATE_CERT"),
69 'username' => getenv("LIVE_MAILBOX_USERNAME"),
70 'password' => getenv("LIVE_MAILBOX_PASSWORD"),
71 'protocol' => 'imap', //might also use imap, [pop3 or nntp (untested)]
72 ],
73 ],
74 ]);
75 }
76 return self::$manager;
77 }
78
79 /**
80 * Get the client
81 *
82 * @return Client
83 * @throws MaskNotFoundException
84 */
85 final protected function getClient(): Client {
86 if (!getenv("LIVE_MAILBOX") ?? false) {
87 $this->markTestSkipped("This test requires a live mailbox. Please set the LIVE_MAILBOX environment variable to run this test.");
88 }
89 return $this->getManager()->account('default');
90 }
91
92 /**
93 * Get special chars
94 *
95 * @return string
96 */
97 final protected function getSpecialChars(): string {
98 return self::SPECIAL_CHARS;
99 }
100
101 /**
102 * Get a folder
103 * @param string $folder_path
104 *
105 * @return Folder
106 * @throws AuthFailedException
107 * @throws ConnectionFailedException
108 * @throws ImapBadRequestException
109 * @throws ImapServerErrorException
110 * @throws MaskNotFoundException
111 * @throws ResponseException
112 * @throws RuntimeException
113 * @throws FolderFetchingException
114 */
115 final protected function getFolder(string $folder_path = "INDEX"): Folder {
116 $client = $this->getClient();
117 self::assertInstanceOf(Client::class, $client->connect());
118
119 $folder = $client->getFolderByPath($folder_path);
120 self::assertInstanceOf(Folder::class, $folder);
121
122 return $folder;
123 }
124
125 /**
126 * Append a message to a folder
127 * @param Folder $folder
128 * @param string $message
129 *
130 * @return Message
131 * @throws AuthFailedException
132 * @throws ConnectionFailedException
133 * @throws EventNotFoundException
134 * @throws ImapBadRequestException
135 * @throws ImapServerErrorException
136 * @throws InvalidMessageDateException
137 * @throws MessageContentFetchingException
138 * @throws MessageFlagException
139 * @throws MessageHeaderFetchingException
140 * @throws ResponseException
141 * @throws RuntimeException
142 */
143 final protected function appendMessage(Folder $folder, string $message): Message {
144 $status = $folder->select();
145 if (!isset($status['uidnext'])) {
146 $this->fail("No UIDNEXT returned");
147 }
148
149 $response = $folder->appendMessage($message);
150 $valid_response = false;
151 foreach ($response as $line) {
152 if (str_starts_with($line, 'OK')) {
153 $valid_response = true;
154 break;
155 }
156 }
157 if (!$valid_response) {
158 $this->fail("Failed to append message: ".implode("\n", $response));
159 }
160
161 $message = $folder->messages()->getMessageByUid($status['uidnext']);
162 self::assertInstanceOf(Message::class, $message);
163
164 return $message;
165 }
166
167 /**
168 * Append a message template to a folder
169 * @param Folder $folder
170 * @param string $template
171 *
172 * @return Message
173 * @throws AuthFailedException
174 * @throws ConnectionFailedException
175 * @throws EventNotFoundException
176 * @throws ImapBadRequestException
177 * @throws ImapServerErrorException
178 * @throws InvalidMessageDateException
179 * @throws MessageContentFetchingException
180 * @throws MessageFlagException
181 * @throws MessageHeaderFetchingException
182 * @throws ResponseException
183 * @throws RuntimeException
184 */
185 final protected function appendMessageTemplate(Folder $folder, string $template): Message {
186 $content = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, "..", "messages", $template]));
187 return $this->appendMessage($folder, $content);
188 }
189
190 /**
191 * Delete a folder if it is given
192 * @param Folder|null $folder
193 *
194 * @return bool
195 * @throws AuthFailedException
196 * @throws ConnectionFailedException
197 * @throws EventNotFoundException
198 * @throws ImapBadRequestException
199 * @throws ImapServerErrorException
200 * @throws ResponseException
201 * @throws RuntimeException
202 */
203 final protected function deleteFolder(?Folder $folder = null): bool {
204 $response = $folder?->delete(false);
205 if (is_array($response)) {
206 $valid_response = false;
207 foreach ($response as $line) {
208 if (str_starts_with($line, 'OK')) {
209 $valid_response = true;
210 break;
211 }
212 }
213 if (!$valid_response) {
214 $this->fail("Failed to delete mailbox: ".implode("\n", $response));
215 }
216 return $valid_response;
217 }
218 return false;
219 }
220 }