OSDN Git Service

Добавлена возможность изменения количества записей, выводимых на странице предметов...
[invent/invent.git] / vendor / swiftmailer / swiftmailer / tests / unit / Swift / Plugins / BandwidthMonitorPluginTest.php
1 <?php
2
3 class Swift_Plugins_BandwidthMonitorPluginTest extends \PHPUnit\Framework\TestCase
4 {
5     private $_monitor;
6
7     private $_bytes = 0;
8
9     protected function setUp()
10     {
11         $this->monitor = new Swift_Plugins_BandwidthMonitorPlugin();
12     }
13
14     public function testBytesOutIncreasesWhenCommandsSent()
15     {
16         $evt = $this->createCommandEvent("RCPT TO:<foo@bar.com>\r\n");
17
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());
23     }
24
25     public function testBytesInIncreasesWhenResponsesReceived()
26     {
27         $evt = $this->createResponseEvent("250 Ok\r\n");
28
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());
34     }
35
36     public function testCountersCanBeReset()
37     {
38         $evt = $this->createResponseEvent("250 Ok\r\n");
39
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());
45
46         $evt = $this->createCommandEvent("RCPT TO:<foo@bar.com>\r\n");
47
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());
53
54         $this->monitor->reset();
55
56         $this->assertEquals(0, $this->monitor->getBytesOut());
57         $this->assertEquals(0, $this->monitor->getBytesIn());
58     }
59
60     public function testBytesOutIncreasesAccordingToMessageLength()
61     {
62         $message = $this->createMessageWithByteCount(6);
63         $evt = $this->createSendEvent($message);
64
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());
70     }
71
72     private function createSendEvent($message)
73     {
74         $evt = $this->getMockBuilder('Swift_Events_SendEvent')
75                     ->disableOriginalConstructor()
76                     ->getMock();
77         $evt->expects($this->any())
78             ->method('getMessage')
79             ->will($this->returnValue($message));
80
81         return $evt;
82     }
83
84     private function createCommandEvent($command)
85     {
86         $evt = $this->getMockBuilder('Swift_Events_CommandEvent')
87                     ->disableOriginalConstructor()
88                     ->getMock();
89         $evt->expects($this->any())
90             ->method('getCommand')
91             ->will($this->returnValue($command));
92
93         return $evt;
94     }
95
96     private function createResponseEvent($response)
97     {
98         $evt = $this->getMockBuilder('Swift_Events_ResponseEvent')
99                     ->disableOriginalConstructor()
100                     ->getMock();
101         $evt->expects($this->any())
102             ->method('getResponse')
103             ->will($this->returnValue($response));
104
105         return $evt;
106     }
107
108     private function createMessageWithByteCount($bytes)
109     {
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'))
117           ); */
118
119         return $msg;
120     }
121
122     public function write($is)
123     {
124         for ($i = 0; $i < $this->bytes; ++$i) {
125             $is->write('x');
126         }
127     }
128 }