diff --git a/src/Fetch/Server.php b/src/Fetch/Server.php index df4870d..82b1c70 100644 --- a/src/Fetch/Server.php +++ b/src/Fetch/Server.php @@ -322,11 +322,22 @@ class Server /** * This returns the number of messages that the current mailbox contains. * + * @param string $mailbox * @return int */ - public function numMessages() + public function numMessages($mailbox='') { - return imap_num_msg($this->getImapStream()); + $cnt = 0; + if ($mailbox==='') { + $cnt = imap_num_msg($this->getImapStream()); + } elseif ($this->hasMailbox($mailbox) && $mailbox !== '') { + $oldMailbox = $this->getMailBox(); + $this->setMailbox($mailbox); + $cnt = $this->numMessages(); + $this->setMailbox($oldMailbox); + } + + return ((int) $cnt); } /** diff --git a/tests/Fetch/Test/MessageTest.php b/tests/Fetch/Test/MessageTest.php index 5553785..0cb9a76 100644 --- a/tests/Fetch/Test/MessageTest.php +++ b/tests/Fetch/Test/MessageTest.php @@ -212,32 +212,29 @@ class MessageTest extends \PHPUnit_Framework_TestCase // Testing by moving message from "Test Folder" to "Sent" // Count Test Folder - $server->setMailBox('Test Folder'); - $testFolderNumStart = $server->numMessages(); + $testFolderNumStart = $server->numMessages('Test Folder'); + $server->setMailbox('Test Folder'); + $this->assertEquals($testFolderNumStart, $server->numMessages(), 'Server presents consistent information between numMessages when mailbox set and directly queried for number of messages'); // Get message from Test Folder - $message = $server->getMessageByUid(1); + $message = current($server->getMessages(1)); $this->assertInstanceOf('\Fetch\Message', $message, 'Server returned Message.'); // Switch to Sent folder, count messages - $server->setMailBox('Sent'); - $sentFolderNumStart = $server->numMessages(); + $sentFolderNumStart = $server->numMessages('Sent'); + $server->setMailbox('Sent'); + $this->assertEquals($sentFolderNumStart, $server->numMessages(), 'Server presents consistent information between numMessages when mailbox set and directly queried for number of messages'); // Switch to "Flagged" folder in order to test that function properly returns to it $this->assertTrue($server->setMailBox('Flagged Email')); - // Move the message! $this->assertTrue($message->moveToMailBox('Sent')); - // Make sure we're still in the same folder $this->assertEquals('Flagged Email', $server->getMailBox(), 'Returned Server back to right mailbox.'); - $this->assertAttributeEquals('Sent', 'mailbox', $message, 'Message mailbox changed to new location.'); - // Make sure Test Folder lost a message $this->assertTrue($server->setMailBox('Test Folder')); $this->assertEquals($testFolderNumStart - 1, $server->numMessages(), 'Message moved out of Test Folder.'); - // Make sure Sent folder gains one $this->assertTrue($server->setMailBox('Sent')); $this->assertEquals($sentFolderNumStart + 1, $server->numMessages(), 'Message moved into Sent Folder.'); diff --git a/tests/Fetch/Test/ServerTest.php b/tests/Fetch/Test/ServerTest.php index 553f002..a90ee7f 100644 --- a/tests/Fetch/Test/ServerTest.php +++ b/tests/Fetch/Test/ServerTest.php @@ -100,6 +100,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase $server = static::getServer(); $numMessages = $server->numMessages(); $this->assertEquals(self::$num_messages_inbox, $numMessages); + $this->assertEquals(0, $server->numMessages( 'DOESNOTEXIST'.time() ) ); } public function testGetMessages()