Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 14 additions & 6 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
66 changes: 66 additions & 0 deletions tests/issues/PR621Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests;

use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Webklex\PHPIMAP\Client;
use Webklex\PHPIMAP\Config;
use Webklex\PHPIMAP\Message;
use Webklex\PHPIMAP\Connection\Protocols\ImapProtocol;

/**
* @see https://github.com/Webklex/php-imap/pull/621
*/
class PR619Test extends TestCase
{
private function makeClientWithNullActiveFolder(): Client
{
$config = Config::make([
'accounts' => [
'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());
}
}