Merge pull request #131 from Lewiscowles1986/patch-2

can now get number of messages in a sub-folder
This commit is contained in:
Robert Hafner 2015-08-01 20:18:59 -07:00
commit a17ae8abbf
3 changed files with 21 additions and 12 deletions

View File

@ -322,11 +322,22 @@ class Server
/** /**
* This returns the number of messages that the current mailbox contains. * This returns the number of messages that the current mailbox contains.
* *
* @param string $mailbox
* @return int * @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);
} }
/** /**

View File

@ -212,32 +212,29 @@ class MessageTest extends \PHPUnit_Framework_TestCase
// Testing by moving message from "Test Folder" to "Sent" // Testing by moving message from "Test Folder" to "Sent"
// Count Test Folder // Count Test Folder
$server->setMailBox('Test Folder'); $testFolderNumStart = $server->numMessages('Test Folder');
$testFolderNumStart = $server->numMessages(); $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 // Get message from Test Folder
$message = $server->getMessageByUid(1); $message = current($server->getMessages(1));
$this->assertInstanceOf('\Fetch\Message', $message, 'Server returned Message.'); $this->assertInstanceOf('\Fetch\Message', $message, 'Server returned Message.');
// Switch to Sent folder, count messages // Switch to Sent folder, count messages
$server->setMailBox('Sent'); $sentFolderNumStart = $server->numMessages('Sent');
$sentFolderNumStart = $server->numMessages(); $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 // Switch to "Flagged" folder in order to test that function properly returns to it
$this->assertTrue($server->setMailBox('Flagged Email')); $this->assertTrue($server->setMailBox('Flagged Email'));
// Move the message! // Move the message!
$this->assertTrue($message->moveToMailBox('Sent')); $this->assertTrue($message->moveToMailBox('Sent'));
// Make sure we're still in the same folder // Make sure we're still in the same folder
$this->assertEquals('Flagged Email', $server->getMailBox(), 'Returned Server back to right mailbox.'); $this->assertEquals('Flagged Email', $server->getMailBox(), 'Returned Server back to right mailbox.');
$this->assertAttributeEquals('Sent', 'mailbox', $message, 'Message mailbox changed to new location.'); $this->assertAttributeEquals('Sent', 'mailbox', $message, 'Message mailbox changed to new location.');
// Make sure Test Folder lost a message // Make sure Test Folder lost a message
$this->assertTrue($server->setMailBox('Test Folder')); $this->assertTrue($server->setMailBox('Test Folder'));
$this->assertEquals($testFolderNumStart - 1, $server->numMessages(), 'Message moved out of Test Folder.'); $this->assertEquals($testFolderNumStart - 1, $server->numMessages(), 'Message moved out of Test Folder.');
// Make sure Sent folder gains one // Make sure Sent folder gains one
$this->assertTrue($server->setMailBox('Sent')); $this->assertTrue($server->setMailBox('Sent'));
$this->assertEquals($sentFolderNumStart + 1, $server->numMessages(), 'Message moved into Sent Folder.'); $this->assertEquals($sentFolderNumStart + 1, $server->numMessages(), 'Message moved into Sent Folder.');

View File

@ -100,6 +100,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase
$server = static::getServer(); $server = static::getServer();
$numMessages = $server->numMessages(); $numMessages = $server->numMessages();
$this->assertEquals(self::$num_messages_inbox, $numMessages); $this->assertEquals(self::$num_messages_inbox, $numMessages);
$this->assertEquals(0, $server->numMessages( 'DOESNOTEXIST'.time() ) );
} }
public function testGetMessages() public function testGetMessages()