/** * Get a mock connection for testing. * The mock will be set up to send to X addresses (sccuessfully). * The mock can also be configured to reconnect after X emails have been sent. * @param int The number emails you expect to send * @param Swift_Connection A mocked object which has not been setup yet, optional * @param int The number of emails to send before reconnecting, optional * @param int The maximum number of times the connection will re-connect, optional * @param boolean True if the same email is copied to all recipients * @return FullMockConnection */ protected function getWorkingMockConnection($send = 1, $conn = null, $reconnect_at = 0, $max_reconnect = 0, $duplicate = false) { $count = 0; if (!$conn) { $conn = new FullMockConnection(); } $conn->setReturnValueAt($count++, "read", "220 xxx ESMTP"); $conn->setReturnValueAt($count++, "read", "250-Hello xxx\r\n250-AUTH PLAIN\r\n250 HELP"); $cycle = 0; $reconnected = 0; for ($i = 0; $i < $send; $i++) { $conn->setReturnValueAt($count++, "read", "250 Ok"); if (!$duplicate || $i == $send - 1) { $cycle++; $conn->setReturnValueAt($count++, "read", "250 Ok"); $conn->setReturnValueAt($count++, "read", "354 Go ahead"); $conn->setReturnValueAt($count++, "read", "250 Ok"); } if ($reconnect_at && $reconnect_at == $cycle) { if (!$max_reconnect || $max_reconnect > $reconnected) { $conn->setReturnValueAt($count++, "read", "220 xxx ESMTP"); $conn->setReturnValueAt($count++, "read", "250-Hello xxx\r\n250 HELP"); $cycle = 0; $reconnected++; } } } $conn->setReturnValue("read", "250 ok"); $conn->setReturnValue("isAlive", true); return $conn; }
/** * The counters should be settable through setBytesIn() and setBytesOut(). */ public function testBytesCanBeReset() { $conn = new FullMockConnection(); //7 chars + 2 for EOL $conn->setReturnValueAt(0, "read", "250 foo"); //15 chars + 2 for EOL $conn->setReturnValueAt(1, "read", "221 bye for now"); $swift = new Swift($conn, null, Swift::NO_START); $plugin = new Swift_Plugin_BandwidthMonitor(); $swift->attachPlugin($plugin, "bwmon"); //20 chars + 2 for EOL $swift->command("abcdefghijklm 123456"); $this->assertEqual(22, $plugin->getBytesOut()); $this->assertEqual(9, $plugin->getBytesIn()); $plugin->setBytesOut(0); $this->assertEqual(0, $plugin->getBytesOut()); //3 chars + 2 for EOL $swift->command("bar"); $this->assertEqual(5, $plugin->getBytesOut()); $this->assertEqual(26, $plugin->getBytesIn()); $plugin->setBytesIn(0); $this->assertEqual(0, $plugin->getBytesIn()); }
public function testBytesPerMinuteThrottling() { $conn = new FullMockConnection(); for ($i = 0; $i < 10; $i++) { $conn->setReturnValueAt($i, "read", "250 xx"); } $swift = new Swift($conn, null, Swift::NO_START); set_time_limit(90); //60 secs expected + standard 30 secs $plugin = new Swift_Plugin_Throttler(); //Outgoing bytes $plugin->setBytesPerMinute(60); $swift->attachPlugin($plugin, "throttler"); $start = time(); for ($i = 0; $i < 10; $i++) { //4 + 2 = 6 bytes each (and 6 x 10 = 60) $swift->command("1234"); } $end = time(); $duration = $end - $start; $this->assertTrue($duration >= 60); $this->dump("Sending 60 bytes at 60 bytes per minute took " . $duration . " secs"); // $conn = new FullMockConnection(); for ($i = 0; $i < 10; $i++) { $conn->setReturnValueAt($i, "read", "250 xx"); } $swift = new Swift($conn, null, Swift::NO_START); set_time_limit(50); //20 secs expected + standard 30 secs $plugin = new Swift_Plugin_Throttler(); //Outgoing bytes $plugin->setBytesPerMinute(180); $swift->attachPlugin($plugin, "throttler"); $start = time(); for ($i = 0; $i < 10; $i++) { //4 + 2 = 6 bytes each (and 6 x 10 = 60) $swift->command("ab c"); } $end = time(); $duration = $end - $start; $this->assertTrue($duration >= 20); $this->dump("Sending 60 bytes at 180 bytes per minute took " . $duration . " secs"); }
public function testDisconnectListenerRunsUponDisconnect() { $conn = new FullMockConnection(); $conn->setReturnValueAt(0, "read", "220 Hello xx"); $conn->setReturnValueAt(1, "read", "250 Hello xxx"); $conn->setReturnValueAt(2, "read", "221 Bye"); $disconnect_listener = new MockDisconnectListener(); $disconnect_listener->expectCallCount("disconnectPerformed", 1); $swift = new Swift($conn); $swift->attachPlugin($disconnect_listener, "myplugin"); $swift->disconnect(); $conn = new FullMockConnection(); $conn->setReturnValueAt(0, "read", "220 Hello xx"); $conn->setReturnValueAt(1, "read", "250 Hello xxx"); $conn->setReturnValueAt(2, "read", "221 Bye"); $conn->setReturnValueAt(3, "read", "220 Hello xx"); $conn->setReturnValueAt(4, "read", "250 Hello xxx"); $conn->setReturnValueAt(5, "read", "221 Bye"); $conn->setReturnValueAt(6, "read", "220 Hello xx"); $conn->setReturnValueAt(7, "read", "250 Hello xxx"); $conn->setReturnValueAt(8, "read", "221 Bye"); $disconnect_listener = new MockDisconnectListener(); $disconnect_listener->expectCallCount("disconnectPerformed", 3); $swift = new Swift($conn); $swift->attachPlugin($disconnect_listener, "myplugin"); $swift->disconnect(); $swift->connect(); $swift->disconnect(); $swift->connect(); $swift->disconnect(); }
public function testAllConnectionsAreClosed() { $mock1 = new FullMockConnection(); $mock1->setReturnValue("isAlive", true); $mock1->setReturnValueAt(0, "isAlive", false); $mock1->setReturnValueAt(1, "isAlive", true); $mock1->expectOnce("stop"); $mock2 = new FullMockConnection(); $mock2->setReturnValue("isAlive", true); $mock2->setReturnValueAt(0, "isAlive", false); $mock2->setReturnValueAt(1, "isAlive", true); $mock2->expectOnce("stop"); $mock3 = new FullMockConnection(); $mock3->setReturnValue("isAlive", true); $mock3->setReturnValueAt(0, "isAlive", false); $mock3->setReturnValueAt(1, "isAlive", true); $mock3->expectOnce("stop"); $multi = new Swift_Connection_Rotator(); $multi->addConnection($mock1); $multi->addConnection($mock2); $multi->addConnection($mock3); $multi->start(); for ($i = 0; $i < 10; $i++) { $multi->nextConnection(); } $multi->stop(); }
public function testLoggerIsInvokedIfSetActive() { $conn = new FullMockConnection(); $conn->setReturnValueAt(0, "read", "220 xxx ESMTP"); $conn->setReturnValueAt(1, "read", "250-Hello xxx\r\n250 HELP"); $conn->setReturnValueAt(2, "read", "250 Ok"); $conn->setReturnValueAt(3, "read", "250 ok"); $conn->setReturnValueAt(4, "read", "354 Go ahead"); $conn->setReturnValueAt(5, "read", "250 ok"); $logger = new MockLogger(); $logger->setLogLevel(Swift_Log::LOG_EVERYTHING); $logger->expectMinimumCallCount("add", 8); Swift_LogContainer::setLog($logger); $swift = new Swift($conn, "abc"); $message = new Swift_Message("My Subject", "my body"); $swift->send($message, new Swift_Address("*****@*****.**"), new Swift_Address("*****@*****.**", "Foo Bar")); }