Skip to content

Commit

Permalink
Throw exception if message does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeboer committed Feb 1, 2015
1 parent c0fd4be commit 30c2493
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Exception/MessageDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Ddeboer\Imap\Exception;

class MessageDoesNotExistException extends Exception
{
public function __construct($number, $error)
{
parent::__construct(
sprintf(
'Message %s does not exist: %s',
$number,
$error
)
);
}
}
19 changes: 18 additions & 1 deletion src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ddeboer\Imap;

use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Ddeboer\Imap\Message\EmailAddress;
use Ddeboer\Imap\Exception\MessageDeleteException;
use Ddeboer\Imap\Exception\MessageMoveException;
Expand Down Expand Up @@ -307,7 +308,23 @@ public function keepUnseen($bool = true)
*/
private function loadStructure()
{
$structure = imap_fetchstructure($this->stream, $this->messageNumber, \FT_UID);
set_error_handler(
function ($nr, $error) {
throw new MessageDoesNotExistException(
$this->messageNumber,
$error
);
}
);

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

restore_error_handler();

$this->parseStructure($structure);
}
}
9 changes: 9 additions & 0 deletions tests/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public function testGetMessages()
$this->assertEquals(3, $i);
}

/**
* @expectedException \Ddeboer\Imap\Exception\MessageDoesNotExistException
* @expectedExceptionMessageRegExp /Message 666 does not exist.*Bad message number/
*/
public function testGetMessageThrowsException()
{
$this->mailbox->getMessage(666);
}

public function testCount()
{
$this->assertEquals(3, $this->mailbox->count());
Expand Down

0 comments on commit 30c2493

Please sign in to comment.