Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement imap_thread #249

Merged
merged 2 commits into from Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Mailbox.php
Expand Up @@ -216,4 +216,20 @@ public function addMessage(string $message): bool
{
return \imap_append($this->resource->getStream(), $this->getFullEncodedName(), $message);
}

/**
* Returns a tree of threaded message for the current Mailbox.
*
* @return array
*/
public function getThread(): array
{
\set_error_handler(function () {});

$tree = \imap_thread($this->resource->getStream());

\restore_error_handler();

return false !== $tree ? $tree : [];
}
}
7 changes: 7 additions & 0 deletions src/MailboxInterface.php
Expand Up @@ -108,4 +108,11 @@ public function getIterator(): MessageIteratorInterface;
* @return bool
*/
public function addMessage(string $message): bool;

/**
* Returns a tree of threaded message for the current Mailbox.
*
* @return array
*/
public function getThread(): array;
}
26 changes: 26 additions & 0 deletions tests/MailboxTest.php
Expand Up @@ -179,4 +179,30 @@ public function testBulkSetFlagsNumbersParameter()
$this->assertFalse($message->isSeen());
}
}

public function testThread()
{
$mailboxOne = $this->createMailbox();
$mailboxOne->addMessage($this->getFixture('thread/my_topic'));
$mailboxOne->addMessage($this->getFixture('thread/unrelated'));
$mailboxOne->addMessage($this->getFixture('thread/re_my_topic'));

$expected = [
'0.num' => 1,
'0.next' => 1,
'1.num' => 3,
'1.next' => 0,
'1.branch' => 0,
'0.branch' => 2,
'2.num' => 2,
'2.next' => 0,
'2.branch' => 0,
];

$this->assertSame($expected, $mailboxOne->getThread());

$emptyMailbox = $this->createMailbox();

$this->assertEmpty($emptyMailbox->getThread());
}
}
10 changes: 10 additions & 0 deletions tests/fixtures/thread/my_topic.eml
@@ -0,0 +1,10 @@
Message-ID: <123@example.com>
From: from@there.com
To: to@here.com
Subject: Nuu
Date: Wed, 13 Sep 2017 13:05:45 +0200
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi
11 changes: 11 additions & 0 deletions tests/fixtures/thread/re_my_topic.eml
@@ -0,0 +1,11 @@
Message-ID: <456@example.com>
In-Reply-To: <123@example.com>
From: from@there.com
To: to@here.com
Subject: Re: Nuu
Date: Wed, 13 Sep 2017 13:05:45 +0200
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi
10 changes: 10 additions & 0 deletions tests/fixtures/thread/unrelated.eml
@@ -0,0 +1,10 @@
Message-ID: <999@example.com>
From: from@there.com
To: to@here.com
Subject: Wut
Date: Wed, 13 Sep 2017 13:05:45 +0200
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi