BandwidthMonitorPluginTest.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Plugins/BandwidthMonitorPlugin.php';
  4. require_once 'Swift/Events/SendEvent.php';
  5. require_once 'Swift/Events/CommandEvent.php';
  6. require_once 'Swift/Events/ResponseEvent.php';
  7. require_once 'Swift/Mime/Message.php';
  8. class Swift_Plugins_BandwidthMonitorPluginTest
  9. extends Swift_Tests_SwiftUnitTestCase
  10. {
  11. public function setUp()
  12. {
  13. $this->_monitor = new Swift_Plugins_BandwidthMonitorPlugin();
  14. }
  15. public function testBytesOutIncreasesAccordingToMessageLength()
  16. {
  17. $message = $this->_createMessageWithByteCount(6);
  18. $evt = $this->_createSendEvent($message);
  19. $this->assertEqual(0, $this->_monitor->getBytesOut());
  20. $this->_monitor->sendPerformed($evt);
  21. $this->assertEqual(6, $this->_monitor->getBytesOut());
  22. $this->_monitor->sendPerformed($evt);
  23. $this->assertEqual(12, $this->_monitor->getBytesOut());
  24. }
  25. public function testBytesOutIncreasesWhenCommandsSent()
  26. {
  27. $evt = $this->_createCommandEvent("RCPT TO: <foo@bar.com>\r\n");
  28. $this->assertEqual(0, $this->_monitor->getBytesOut());
  29. $this->_monitor->commandSent($evt);
  30. $this->assertEqual(24, $this->_monitor->getBytesOut());
  31. $this->_monitor->commandSent($evt);
  32. $this->assertEqual(48, $this->_monitor->getBytesOut());
  33. }
  34. public function testBytesInIncreasesWhenResponsesReceived()
  35. {
  36. $evt = $this->_createResponseEvent("250 Ok\r\n");
  37. $this->assertEqual(0, $this->_monitor->getBytesIn());
  38. $this->_monitor->responseReceived($evt);
  39. $this->assertEqual(8, $this->_monitor->getBytesIn());
  40. $this->_monitor->responseReceived($evt);
  41. $this->assertEqual(16, $this->_monitor->getBytesIn());
  42. }
  43. public function testCountersCanBeReset()
  44. {
  45. $evt = $this->_createResponseEvent("250 Ok\r\n");
  46. $this->assertEqual(0, $this->_monitor->getBytesIn());
  47. $this->_monitor->responseReceived($evt);
  48. $this->assertEqual(8, $this->_monitor->getBytesIn());
  49. $this->_monitor->responseReceived($evt);
  50. $this->assertEqual(16, $this->_monitor->getBytesIn());
  51. $evt = $this->_createCommandEvent("RCPT TO: <foo@bar.com>\r\n");
  52. $this->assertEqual(0, $this->_monitor->getBytesOut());
  53. $this->_monitor->commandSent($evt);
  54. $this->assertEqual(24, $this->_monitor->getBytesOut());
  55. $this->_monitor->commandSent($evt);
  56. $this->assertEqual(48, $this->_monitor->getBytesOut());
  57. $this->_monitor->reset();
  58. $this->assertEqual(0, $this->_monitor->getBytesOut());
  59. $this->assertEqual(0, $this->_monitor->getBytesIn());
  60. }
  61. // -- Creation Methods
  62. private function _createSendEvent($message)
  63. {
  64. $evt = $this->_mock('Swift_Events_SendEvent');
  65. $this->_checking(Expectations::create()
  66. -> ignoring($evt)->getMessage() -> returns($message)
  67. );
  68. return $evt;
  69. }
  70. private function _createCommandEvent($command)
  71. {
  72. $evt = $this->_mock('Swift_Events_CommandEvent');
  73. $this->_checking(Expectations::create()
  74. -> ignoring($evt)->getCommand() -> returns($command)
  75. );
  76. return $evt;
  77. }
  78. private function _createResponseEvent($response)
  79. {
  80. $evt = $this->_mock('Swift_Events_ResponseEvent');
  81. $this->_checking(Expectations::create()
  82. -> ignoring($evt)->getResponse() -> returns($response)
  83. );
  84. return $evt;
  85. }
  86. private function _createMessageWithByteCount($bytes)
  87. {
  88. $this->_bytes = $bytes;
  89. $msg = $this->_mock('Swift_Mime_Message');
  90. $this->_checking(Expectations::create()
  91. -> ignoring($msg)->toByteStream(any()) -> calls(array($this, '_write'))
  92. );
  93. return $msg;
  94. }
  95. private $_bytes = 0;
  96. public function _write($invocation)
  97. {
  98. $args = $invocation->getArguments();
  99. $is = $args[0];
  100. for ($i = 0; $i < $this->_bytes; ++$i)
  101. {
  102. $is->write('x');
  103. }
  104. }
  105. }