/** * 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; }
public function testExceptionIsThrownIf200ResponseIsNotReceivedAtStart() { $conn = new FullMockConnection(); $conn->setReturnValue("read", "000 abc ESMTP xx"); try { $swift = new Swift($conn, "mydomain"); $this->fail("No 220 response was given so this should have failed"); } catch (Swift_ConnectionException $e) { //Pass } $conn = new FullMockConnection(); $conn->setReturnValue("read", "120 abc ESMTP xx"); try { $swift = new Swift($conn, "mydomain"); $this->fail("No 220 response was given so this should have failed"); } catch (Swift_ConnectionException $e) { //Pass } $conn = new FullMockConnection(); $conn->setReturnValue("read", "x220 abc ESMTP xx"); try { $swift = new Swift($conn, "mydomain"); $this->fail("No 220 response was given so this should have failed"); } catch (Swift_ConnectionException $e) { //Pass } }
public function testLastErrorCanBeFetched() { $mockConnection = new FullMockConnection(); $mockConnection->setReturnValue("read", "500 Bad"); $swift = new EasySwift($mockConnection); $this->assertTrue($swift->hasFailed()); $this->assertFalse($swift->isConnected()); }
/** * When the expected code is set to -1 were sending a literal chunk of data (not a full command). * Therefore EOL \r\n won't be included. */ public function testBytesOutDoesNotAddEOLOverheadIfCodeIsMinusOne() { $conn = new FullMockConnection(); $conn->setReturnValue("read", "250 foo"); $swift = new Swift($conn, null, Swift::NO_START); $plugin = new Swift_Plugin_BandwidthMonitor(); $swift->attachPlugin($plugin, "bwmon"); //Just 3 chars, no EOL $swift->command("foo", -1); $this->assertEqual(3, $plugin->getBytesOut()); }
/** Get a mock connection for testing * @param int The number emails you expect to send * @param boolean If the email will be duplicated to all recipients * @return FullMockConnection */ protected function getWorkingMockConnection($send = 1, $duplicate = false, $conn = null) { $count = 0; if ($conn === null) { $conn = new FullMockConnection(); } $conn->setReturnValue("isAlive", true); $conn->setReturnValueAt($count++, "read", "220 xxx ESMTP"); $conn->setReturnValueAt($count++, "read", "250-Hello xxx\r\n250-AUTH PLAIN\r\n250 HELP"); for ($i = 0; $i < $send; $i++) { $conn->setReturnValueAt($count++, "read", "250 Ok"); if (!$duplicate || $i == $send - 1) { $conn->setReturnValueAt($count++, "read", "250 Ok"); $conn->setReturnValueAt($count++, "read", "354 Go ahead"); $conn->setReturnValueAt($count++, "read", "250 Ok"); } } $conn->setReturnValueAt($count++, "read", "250 Bye"); return $conn; }
public function testIsAliveReturnsFalseIfTheConnectionIsClosed() { $mock1 = new FullMockConnection(); $mock1->setReturnValue("isAlive", false); $mock1->expectOnce("start"); $mock1->expectNever("stop"); $mock2 = new FullMockConnection(); $mock2->setReturnValue("isAlive", true); $mock2->expectOnce("start"); $mock2->expectOnce("stop"); $multi = new Swift_Connection_Multi(); $multi->addConnection($mock1, "mock1"); $multi->addConnection($mock2, "mock2"); $multi->start(); $this->assertTrue($multi->isAlive()); $multi->stop(); $this->assertFalse($multi->isAlive()); }
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(); }