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
|
<?php
class Swift_Plugins_BandwidthMonitorPluginTest extends \PHPUnit\Framework\TestCase
{
private $monitor;
private $bytes = 0;
protected function setUp(): void
{
$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(23, $this->monitor->getBytesOut());
$this->monitor->commandSent($evt);
$this->assertEquals(46, $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(23, $this->monitor->getBytesOut());
$this->monitor->commandSent($evt);
$this->assertEquals(46, $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());
}
private function createSendEvent($message)
{
$evt = $this->createMock('Swift_Events_SendEvent');
$evt->expects($this->any())
->method('getMessage')
->willReturn($message);
return $evt;
}
private function createCommandEvent($command)
{
$evt = $this->createMock('Swift_Events_CommandEvent');
$evt->expects($this->any())
->method('getCommand')
->willReturn($command);
return $evt;
}
private function createResponseEvent($response)
{
$evt = $this->createMock('Swift_Events_ResponseEvent');
$evt->expects($this->any())
->method('getResponse')
->willReturn($response);
return $evt;
}
private function createMessageWithByteCount($bytes)
{
$this->bytes = $bytes;
$msg = $this->createMock('Swift_Mime_SimpleMessage');
$msg->expects($this->any())
->method('toByteStream')
->willReturnCallback([$this, 'write']);
/* $this->checking(Expectations::create()
-> ignoring($msg)->toByteStream(any()) -> calls(array($this, 'write'))
); */
return $msg;
}
public function write($is)
{
for ($i = 0; $i < $this->bytes; ++$i) {
$is->write('x');
}
}
}
|