| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
abstract class LiveMailboxTestCase extends TestCase { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
const SPECIAL_CHARS = 'A_\\|!"£$%&()=?àèìòùÀÈÌÒÙ<>-@#[]_ß_б_π_€_✔_你_يد_Z_'; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
protected static ClientManager $manager; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 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', |
| 72 |
], |
| 73 |
], |
| 74 |
]); |
| 75 |
} |
| 76 |
return self::$manager; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
final protected function getSpecialChars(): string { |
| 98 |
return self::SPECIAL_CHARS; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 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 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 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 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 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 |
} |