diff --git a/src/Client.php b/src/Client.php index 5eb26ff2..4936e876 100755 --- a/src/Client.php +++ b/src/Client.php @@ -400,13 +400,20 @@ public function isConnected(): bool { * @throws ResponseException */ public function checkConnection(): bool { + $active_folder = $this->active_folder; try { if (!$this->isConnected()) { $this->connect(); + if ($active_folder !== null) { + $this->openFolder($active_folder, true); + } return true; } } catch (\Throwable) { $this->connect(); + if ($active_folder !== null) { + $this->openFolder($active_folder, true); + } } return false; } diff --git a/src/Message.php b/src/Message.php index 9d15a7b8..a6b249b8 100755 --- a/src/Message.php +++ b/src/Message.php @@ -130,10 +130,8 @@ class Message { /** * The message folder path - * - * @var string $folder_path */ - protected string $folder_path; + protected string $folder_path = ''; /** * Fetch body options @@ -297,11 +295,11 @@ public static function make(int $uid, ?int $msglist, Client $client, string $raw "message" => $client->getDefaultEvents("message"), "flag" => $client->getDefaultEvents("flag"), ]); + $instance->setClient($client); $instance->setFolderPath($client->getFolderPath()); $instance->setSequence($sequence); $instance->setFetchOption($fetch_options); - $instance->setClient($client); $instance->setSequenceId($uid, $msglist); $instance->parseRawHeader($raw_header); @@ -1465,11 +1463,21 @@ public function getFolderPath(): string { /** * Set the message path aka folder path - * @param $folder_path + * @param ?string $folder_path * * @return Message */ - public function setFolderPath($folder_path): Message { + public function setFolderPath(?string $folder_path): Message { + if ($folder_path === null || $folder_path === '') { + $folder_path = $this->client?->getFolderPath(); + } + if ($folder_path === null || $folder_path === '') { + $folder_path = (string)$this->config->get('options.common_folders.inbox', 'INBOX'); + } + if ($folder_path === '') { + $folder_path = 'INBOX'; + } + $this->folder_path = $folder_path; return $this; diff --git a/tests/issues/PR621Test.php b/tests/issues/PR621Test.php new file mode 100644 index 00000000..4c2d965a --- /dev/null +++ b/tests/issues/PR621Test.php @@ -0,0 +1,66 @@ + [ + 'default' => [ + 'host' => 'localhost', + 'protocol' => 'imap', + 'encryption' => 'ssl', + 'username' => 'foo@example.com', + 'password' => 'secret', + ], + ], + ]); + + $client = new Client($config); + + $protocol = $this->createStub(ImapProtocol::class); + $protocol->method('connected')->willReturn(true); + $client->connection = $protocol; + + // Simulate the state after an implicit reconnect: + // Client::connect() always calls disconnect() first, + // and disconnect() resets active_folder to null. + $ref = new ReflectionClass($client); + $prop = $ref->getProperty('active_folder'); + $prop->setAccessible(true); + $prop->setValue($client, null); + + return $client; + } + + public function testMakeThrowsTypeErrorWhenActiveFolderIsNull(): void + { + $this->expectException(\TypeError::class); + + $client = $this->makeClientWithNullActiveFolder(); + Message::make(1, 0, $client, "Subject: Test\r\n\r\n", '', []); + } + + public function testMakeSucceedsAfterFix(): void + { + $client = $this->makeClientWithNullActiveFolder(); + + $message = Message::make(1, 0, $client, "Subject: Test\r\n\r\n", '', []); + + $this->assertInstanceOf(Message::class, $message); + $this->assertIsString($message->getFolderPath()); + $this->assertNotEmpty($message->getFolderPath()); + } +}