1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
<?php
class Swift_Plugins_BandwidthMonitorPluginTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->_monitor = new Swift_Plugins_BandwidthMonitorPlugin();
}
public function testBytesOutIncreasesWhenCommandsSent()
{
$evt = $this->_createCommandEvent("RCPT TO: <foo@bar.com>\r\n");
$this->assertEquals(0, $this->_monitor->getBytesOut());
$this->_monitor->commandSent($evt);
$this->assertEquals(24, $this->_monitor->getBytesOut());
$this->_monitor->commandSent($evt);
$this->assertEquals(48, $this->_monitor->getBytesOut());
}
public function testBytesInIncreasesWhenResponsesReceived()
{
$evt = $this->_createResponseEvent("250 Ok\r\n");
$this->assertEquals(0, $this->_monitor->getBytesIn());
$this->_monitor->responseReceived($evt);
$this->assertEquals(8, $this->_monitor->getBytesIn());
$this->_monitor->responseReceived($evt);
$this->assertEquals(16, $this->_monitor->getBytesIn());
}
public function testCountersCanBeReset()
{
$evt = $this->_createResponseEvent("250 Ok\r\n");
$this->assertEquals(0, $this->_monitor->getBytesIn());
$this->_monitor->responseReceived($evt);
$this->assertEquals(8, $this->_monitor->getBytesIn());
$this->_monitor->responseReceived($evt);
$this->assertEquals(16, $this->_monitor->getBytesIn());
$evt = $this->_createCommandEvent("RCPT TO: <foo@bar.com>\r\n");
$this->assertEquals(0, $this->_monitor->getBytesOut());
$this->_monitor->commandSent($evt);
$this->assertEquals(24, $this->_monitor->getBytesOut());
$this->_monitor->commandSent($evt);
$this->assertEquals(48, $this->_monitor->getBytesOut());
$this->_monitor->reset();
$this->assertEquals(0, $this->_monitor->getBytesOut());
$this->assertEquals(0, $this->_monitor->getBytesIn());
}
public function testBytesOutIncreasesAccordingToMessageLength()
{
$message = $this->_createMessageWithByteCount(6);
$evt = $this->_createSendEvent($message);
$this->assertEquals(0, $this->_monitor->getBytesOut());
$this->_monitor->sendPerformed($evt);
$this->assertEquals(6, $this->_monitor->getBytesOut());
$this->_monitor->sendPerformed($evt);
$this->assertEquals(12, $this->_monitor->getBytesOut());
}
// -- Creation Methods
private function _createSendEvent($message)
{
$evt = $this->getMockBuilder('Swift_Events_SendEvent')
->disableOriginalConstructor()
->getMock();
$evt->expects($this->any())
->method('getMessage')
->will($this->returnValue($message));
return $evt;
}
private function _createCommandEvent($command)
{
$evt = $this->getMockBuilder('Swift_Events_CommandEvent')
->disableOriginalConstructor()
->getMock();
$evt->expects($this->any())
->method('getCommand')
->will($this->returnValue($command));
return $evt;
}
private function _createResponseEvent($response)
{
$evt = $this->getMockBuilder('Swift_Events_ResponseEvent')
->disableOriginalConstructor()
->getMock();
$evt->expects($this->any())
->method('getResponse')
->will($this->returnValue($response));
return $evt;
}
private function _createMessageWithByteCount($bytes)
{
$this->_bytes = $bytes;
$msg = $this->getMock('Swift_Mime_Message');
$msg->expects($this->any())
->method('toByteStream')
->will($this->returnCallback(array($this, '_write')));
/* $this->_checking(Expectations::create()
-> ignoring($msg)->toByteStream(any()) -> calls(array($this, '_write'))
); */
return $msg;
}
private $_bytes = 0;
public function _write($is)
{
for ($i = 0; $i < $this->_bytes; ++$i) {
$is->write('x');
}
}
}
|