Skip to content

Commit

Permalink
Implement imap_thread (#249)
Browse files Browse the repository at this point in the history
Close #113
  • Loading branch information
Slamdunk authored Oct 17, 2017
1 parent 58b1c7b commit 1e298f3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Mailbox.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Message-ID: <[email protected]>
From: [email protected]
To: [email protected]
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Message-ID: <[email protected]>
In-Reply-To: <[email protected]>
From: [email protected]
To: [email protected]
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Message-ID: <[email protected]>
From: [email protected]
To: [email protected]
Subject: Wut
Date: Wed, 13 Sep 2017 13:05:45 +0200
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi

0 comments on commit 1e298f3

Please sign in to comment.