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

Increased code-coverage #211

Merged
merged 1 commit into from
Oct 2, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ class Message extends Message\Part
*/
public function __construct($stream, int $messageNumber)
{
$this->stream = $stream;
$this->messageNumber = $messageNumber;

$this->loadStructure();
$structure = self::loadStructure($stream, $messageNumber);
parent::__construct($stream, $messageNumber, null, $structure);
}

/**
Expand Down Expand Up @@ -432,20 +430,22 @@ public function getRawMessage(bool $keepUnseen = false): string

/**
* Load message structure
*
* @param mixed $stream
*/
private function loadStructure()
private static function loadStructure($stream, int $messageNumber): \stdClass
{
set_error_handler(function ($nr, $error) {
set_error_handler(function ($nr, $error) use ($messageNumber) {
throw new MessageDoesNotExistException(sprintf(
'Message %s does not exist: %s',
$this->messageNumber,
$messageNumber,
$error
), $nr);
});

$structure = imap_fetchstructure(
$this->stream,
$this->messageNumber,
$stream,
$messageNumber,
\FT_UID
);

Expand All @@ -455,7 +455,7 @@ private function loadStructure()
throw new MessageStructureException('imap_fetchstructure() returned empty message');
}

$this->parseStructure($structure);
return $structure;
}

/**
Expand Down
17 changes: 2 additions & 15 deletions src/Message/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function getLines()
return $this->lines;
}

public function getParameters()
public function getParameters(): Parameters
{
return $this->parameters;
}
Expand Down Expand Up @@ -185,24 +185,11 @@ public function getDecodedContent(bool $keepUnseen = false): string
return $this->decodedContent;
}

public function getStructure()
public function getStructure(): \stdClass
{
return $this->structure;
}

protected function fetchStructure(int $partNumber = null)
{
if (null === $this->structure) {
$this->loadStructure();
}

if ($partNumber) {
return $this->structure->parts[$partNumber];
}

return $this->structure;
}

final protected function parseStructure(\stdClass $structure)
{
$this->type = $this->typesMap[$structure->type] ?? self::TYPE_UNKNOWN;
Expand Down
12 changes: 11 additions & 1 deletion tests/MailboxSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,23 @@ public function testSpacesAndDoubleQuoteEscape()
$this->assertCount(0, $messages);
}

public function testOrCondition()
public function testOrConditionFunctionality()
{
$orCondition = new Search\LogicalOperator\OrConditions([
new Search\Text\Body(uniqid()),
new Search\Text\Subject(uniqid()),
]);

$this->assertContains('(', $orCondition->toString());

return $orCondition;
}

/**
* @depends testOrConditionFunctionality
*/
public function testOrConditionUsage()
{
$this->markTestIncomplete('Unable to get a server working with OR condition');

$messages = $this->mailbox->getMessages($orCondition);
Expand Down
32 changes: 32 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Ddeboer\Imap\Exception\InvalidDateHeaderException;
use Ddeboer\Imap\Exception\UnsupportedCharsetException;
use Ddeboer\Imap\Message\EmailAddress;
use Ddeboer\Imap\Parameters;
use Zend\Mime\Mime;

/**
Expand Down Expand Up @@ -185,6 +186,15 @@ public function testEmailAddress()
$this->mailbox->addMessage($this->getFixture('email_address'));
$message = $this->mailbox->getMessage(1);

$this->assertSame('<[email protected]>', $message->getId());
$this->assertGreaterThan(0, $message->getNumber());
$this->assertGreaterThan(0, $message->getSize());
$this->assertGreaterThan(0, $message->getBytes());
$this->assertInstanceOf(Parameters::class, $message->getParameters());
$this->assertNull($message->getLines());
$this->assertNull($message->getDisposition());
$this->assertNotEmpty($message->getStructure());

$from = $message->getFrom();
$this->assertInstanceOf(EmailAddress::class, $from);
$this->assertEquals('no_host', $from->getMailbox());
Expand All @@ -199,6 +209,8 @@ public function testEmailAddress()

$this->assertInstanceOf(EmailAddress::class, $cc[1]);
$this->assertEquals('No-address', $cc[1]->getMailbox());

$this->assertCount(0, $message->getReturnPath());
}

public function testBcc()
Expand Down Expand Up @@ -255,12 +267,15 @@ public function testGetAttachments(string $fixture)
);

$message = $this->mailbox->getMessage(1);
$this->assertTrue($message->hasAttachments());
$this->assertCount(1, $message->getAttachments());
$attachment = $message->getAttachments()[0];

$this->assertEquals(
'Prostřeno_2014_poslední volné termíny.xls',
$attachment->getFilename()
);
$this->assertNull($attachment->getSize());
}

public function getAttachmentFixture(): array
Expand Down Expand Up @@ -424,6 +439,23 @@ public function testHtmlOnlyMessage()
$this->assertNull($message->getBodyText());
}

public function testSimpleMultipart()
{
$this->mailbox->addMessage($this->getFixture('simple_multipart'));

$message = $this->mailbox->getMessage(1);

$this->assertSame('MyPlain', rtrim($message->getBodyText()));
$this->assertSame('MyHtml', rtrim($message->getBodyHtml()));

$parts = [];
foreach ($message as $key => $part) {
$parts[$key] = $part;
}

$this->assertCount(2, $parts);
}

public function testGetRawMessage()
{
$fixture = $this->getFixture('structured_with_attachment');
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/email_address.eml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Message-ID: <[email protected]>
From: no_host
Cc: "This one: is \"right\"" <[email protected]>, No-address
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi
How are you?
24 changes: 24 additions & 0 deletions tests/fixtures/simple_multipart.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
From: [email protected]
To: [email protected]
Date: Wed, 27 Sep 2017 12:48:51 +0200
Subject: test
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_0081_01D32C91.033E7020"

This is a multipart message in MIME format.

------=_NextPart_000_0081_01D32C91.033E7020
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

MyPlain

------=_NextPart_000_0081_01D32C91.033E7020
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

MyHtml
------=_NextPart_000_0081_01D32C91.033E7020--