AntiFloodPluginTest.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Plugins/AntiFloodPlugin.php';
  4. require_once 'Swift/Events/SendEvent.php';
  5. require_once 'Swift/Transport.php';
  6. require_once 'Swift/Plugins/Sleeper.php';
  7. class Swift_Plugins_AntiFloodPluginTest extends Swift_Tests_SwiftUnitTestCase
  8. {
  9. public function testThresholdCanBeSetAndFetched()
  10. {
  11. $plugin = new Swift_Plugins_AntiFloodPlugin(10);
  12. $this->assertEqual(10, $plugin->getThreshold());
  13. $plugin->setThreshold(100);
  14. $this->assertEqual(100, $plugin->getThreshold());
  15. }
  16. public function testSleepTimeCanBeSetAndFetched()
  17. {
  18. $plugin = new Swift_Plugins_AntiFloodPlugin(10, 5);
  19. $this->assertEqual(5, $plugin->getSleepTime());
  20. $plugin->setSleepTime(1);
  21. $this->assertEqual(1, $plugin->getSleepTime());
  22. }
  23. public function testPluginStopsConnectionAfterThreshold()
  24. {
  25. $transport = $this->_createTransport();
  26. $evt = $this->_createSendEvent($transport);
  27. $this->_checking(Expectations::create()
  28. -> one($transport)->start()
  29. -> one($transport)->stop()
  30. -> ignoring($transport)
  31. );
  32. $plugin = new Swift_Plugins_AntiFloodPlugin(10);
  33. for ($i = 0; $i < 12; $i++)
  34. {
  35. $plugin->sendPerformed($evt);
  36. }
  37. }
  38. public function testPluginCanStopAndStartMultipleTimes()
  39. {
  40. $transport = $this->_createTransport();
  41. $evt = $this->_createSendEvent($transport);
  42. $this->_checking(Expectations::create()
  43. -> exactly(5)->of($transport)->start()
  44. -> exactly(5)->of($transport)->stop()
  45. -> ignoring($transport)
  46. );
  47. $plugin = new Swift_Plugins_AntiFloodPlugin(2);
  48. for ($i = 0; $i < 11; $i++)
  49. {
  50. $plugin->sendPerformed($evt);
  51. }
  52. }
  53. public function testPluginCanSleepDuringRestart()
  54. {
  55. $sleeper = $this->_createSleeper();
  56. $transport = $this->_createTransport();
  57. $evt = $this->_createSendEvent($transport);
  58. $this->_checking(Expectations::create()
  59. -> one($sleeper)->sleep(10)
  60. -> one($transport)->start()
  61. -> one($transport)->stop()
  62. -> ignoring($transport)
  63. );
  64. $plugin = new Swift_Plugins_AntiFloodPlugin(99, 10, $sleeper);
  65. for ($i = 0; $i < 101; $i++)
  66. {
  67. $plugin->sendPerformed($evt);
  68. }
  69. }
  70. // -- Creation Methods
  71. private function _createTransport()
  72. {
  73. return $this->_mock('Swift_Transport');
  74. }
  75. private function _createSendEvent($transport)
  76. {
  77. $evt = $this->_mock('Swift_Events_SendEvent');
  78. $this->_checking(Expectations::create()
  79. -> ignoring($evt)->getSource() -> returns($transport)
  80. -> ignoring($evt)->getTransport() -> returns($transport)
  81. );
  82. return $evt;
  83. }
  84. private function _createSleeper()
  85. {
  86. return $this->_mock('Swift_Plugins_Sleeper');
  87. }
  88. }