3 class Swift_Plugins_BandwidthMonitorPluginTest extends \PHPUnit\Framework\TestCase
9 protected function setUp()
11 $this->monitor = new Swift_Plugins_BandwidthMonitorPlugin();
14 public function testBytesOutIncreasesWhenCommandsSent()
16 $evt = $this->createCommandEvent("RCPT TO:<foo@bar.com>\r\n");
18 $this->assertEquals(0, $this->monitor->getBytesOut());
19 $this->monitor->commandSent($evt);
20 $this->assertEquals(23, $this->monitor->getBytesOut());
21 $this->monitor->commandSent($evt);
22 $this->assertEquals(46, $this->monitor->getBytesOut());
25 public function testBytesInIncreasesWhenResponsesReceived()
27 $evt = $this->createResponseEvent("250 Ok\r\n");
29 $this->assertEquals(0, $this->monitor->getBytesIn());
30 $this->monitor->responseReceived($evt);
31 $this->assertEquals(8, $this->monitor->getBytesIn());
32 $this->monitor->responseReceived($evt);
33 $this->assertEquals(16, $this->monitor->getBytesIn());
36 public function testCountersCanBeReset()
38 $evt = $this->createResponseEvent("250 Ok\r\n");
40 $this->assertEquals(0, $this->monitor->getBytesIn());
41 $this->monitor->responseReceived($evt);
42 $this->assertEquals(8, $this->monitor->getBytesIn());
43 $this->monitor->responseReceived($evt);
44 $this->assertEquals(16, $this->monitor->getBytesIn());
46 $evt = $this->createCommandEvent("RCPT TO:<foo@bar.com>\r\n");
48 $this->assertEquals(0, $this->monitor->getBytesOut());
49 $this->monitor->commandSent($evt);
50 $this->assertEquals(23, $this->monitor->getBytesOut());
51 $this->monitor->commandSent($evt);
52 $this->assertEquals(46, $this->monitor->getBytesOut());
54 $this->monitor->reset();
56 $this->assertEquals(0, $this->monitor->getBytesOut());
57 $this->assertEquals(0, $this->monitor->getBytesIn());
60 public function testBytesOutIncreasesAccordingToMessageLength()
62 $message = $this->createMessageWithByteCount(6);
63 $evt = $this->createSendEvent($message);
65 $this->assertEquals(0, $this->monitor->getBytesOut());
66 $this->monitor->sendPerformed($evt);
67 $this->assertEquals(6, $this->monitor->getBytesOut());
68 $this->monitor->sendPerformed($evt);
69 $this->assertEquals(12, $this->monitor->getBytesOut());
72 private function createSendEvent($message)
74 $evt = $this->getMockBuilder('Swift_Events_SendEvent')
75 ->disableOriginalConstructor()
77 $evt->expects($this->any())
78 ->method('getMessage')
79 ->will($this->returnValue($message));
84 private function createCommandEvent($command)
86 $evt = $this->getMockBuilder('Swift_Events_CommandEvent')
87 ->disableOriginalConstructor()
89 $evt->expects($this->any())
90 ->method('getCommand')
91 ->will($this->returnValue($command));
96 private function createResponseEvent($response)
98 $evt = $this->getMockBuilder('Swift_Events_ResponseEvent')
99 ->disableOriginalConstructor()
101 $evt->expects($this->any())
102 ->method('getResponse')
103 ->will($this->returnValue($response));
108 private function createMessageWithByteCount($bytes)
110 $this->bytes = $bytes;
111 $msg = $this->getMockBuilder('Swift_Mime_SimpleMessage')->disableOriginalConstructor()->getMock();
112 $msg->expects($this->any())
113 ->method('toByteStream')
114 ->will($this->returnCallback([$this, 'write']));
115 /* $this->checking(Expectations::create()
116 -> ignoring($msg)->toByteStream(any()) -> calls(array($this, 'write'))
122 public function write($is)
124 for ($i = 0; $i < $this->bytes; ++$i) {