Skip to content
Permalink
Browse files

Simplify tests, fix CS, remove `autoDisconnect` option.

  • Loading branch information...
ndm2
ndm2 committed Jul 17, 2014
1 parent 1879c3f commit 66b1ba8a3282aa59d6b64e742a3d4a69036dff8c
Showing with 12 additions and 59 deletions.
  1. +5 −8 src/Network/Email/SmtpTransport.php
  2. +7 −51 tests/TestCase/Network/Email/SmtpTransportTest.php
@@ -35,7 +35,6 @@ class SmtpTransport extends AbstractTransport {
'password' => null,
'client' => null,
'tls' => false,
'autoDisconnect' => true,
'keepAlive' => false
];
@@ -70,15 +69,13 @@ class SmtpTransport extends AbstractTransport {
/**
* Destructor
*
* Tries to disconnect in case `autoDisconnect` is enabled.
* Tries to disconnect to ensure that the connection is being
* terminated properly before the socket gets closed.
*/
public function __destruct() {
try {
if ($this->_config['autoDisconnect']) {
$this->disconnect();
}
}
catch (\Exception $e) { // avoid fatal error on script termination
$this->disconnect();
} catch (\Exception $e) { // avoid fatal error on script termination
}
}
@@ -427,4 +424,4 @@ protected function _smtpSend($data, $checkCode = '250') {
}
}
}
}
@@ -464,7 +464,6 @@ public function testBufferResponseLines() {
* @return void
*/
public function testAutoDisconnect() {
$this->SmtpTransport->config(array('autoDisconnect' => true));
$this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
$this->socket->connected = true;
unset($this->SmtpTransport);
@@ -476,34 +475,12 @@ public function testAutoDisconnect() {
* @return void
*/
public function testKeepAlive() {
$this->SmtpTransport->config(array('keepAlive' => true, 'autoDisconnect' => false));
$this->SmtpTransport->config(array('keepAlive' => true));
$email = $this->getMock('Cake\Network\Email\Email', array('message'));
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->to('cake@cakephp.org', 'CakePHP');
$email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
$email->subject('Testing SMTP');
$date = date(DATE_RFC2822);
$email->setHeaders(array('X-Mailer' => Email::EMAIL_CLIENT, 'Date' => $date));
$email->expects($this->exactly(2))
->method('message')
->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', '')));
$data = "From: CakePHP Test <noreply@cakephp.org>\r\n";
$data .= "To: CakePHP <cake@cakephp.org>\r\n";
$data .= "X-Mailer: CakePHP Email\r\n";
$data .= "Date: " . $date . "\r\n";
$data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n";
$data .= "Subject: Testing SMTP\r\n";
$data .= "MIME-Version: 1.0\r\n";
$data .= "Content-Type: text/plain; charset=UTF-8\r\n";
$data .= "Content-Transfer-Encoding: 8bit\r\n";
$data .= "\r\n";
$data .= "First Line\r\n";
$data .= "Second Line\r\n";
$data .= "..Third Line\r\n"; // RFC5321 4.5.2.Transparency
$data .= "\r\n";
$data .= "\r\n\r\n.\r\n";
$email->expects($this->exactly(2))->method('message')->will($this->returnValue(array('First Line')));
$this->socket->expects($this->never())->method('disconnect');
@@ -524,7 +501,7 @@ public function testKeepAlive() {
$this->socket->expects($this->at(12))->method('write')->with("DATA\r\n");
$this->socket->expects($this->at(13))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(14))->method('read')->will($this->returnValue("354 OK\r\n"));
$this->socket->expects($this->at(15))->method('write')->with($data);
$this->socket->expects($this->at(15))->method('write')->with($this->stringContains('First Line'));
$this->socket->expects($this->at(16))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(17))->method('read')->will($this->returnValue("250 OK\r\n"));
@@ -542,7 +519,7 @@ public function testKeepAlive() {
$this->socket->expects($this->at(27))->method('write')->with("DATA\r\n");
$this->socket->expects($this->at(28))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(29))->method('read')->will($this->returnValue("354 OK\r\n"));
$this->socket->expects($this->at(30))->method('write')->with($data);
$this->socket->expects($this->at(15))->method('write')->with($this->stringContains('First Line'));
$this->socket->expects($this->at(31))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(32))->method('read')->will($this->returnValue("250 OK\r\n"));
@@ -560,29 +537,7 @@ public function testSendDefaults() {
$email = $this->getMock('Cake\Network\Email\Email', array('message'));
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->to('cake@cakephp.org', 'CakePHP');
$email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
$email->subject('Testing SMTP');
$date = date(DATE_RFC2822);
$email->setHeaders(array('X-Mailer' => Email::EMAIL_CLIENT, 'Date' => $date));
$email->expects($this->once())
->method('message')
->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', '')));
$data = "From: CakePHP Test <noreply@cakephp.org>\r\n";
$data .= "To: CakePHP <cake@cakephp.org>\r\n";
$data .= "X-Mailer: CakePHP Email\r\n";
$data .= "Date: " . $date . "\r\n";
$data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n";
$data .= "Subject: Testing SMTP\r\n";
$data .= "MIME-Version: 1.0\r\n";
$data .= "Content-Type: text/plain; charset=UTF-8\r\n";
$data .= "Content-Transfer-Encoding: 8bit\r\n";
$data .= "\r\n";
$data .= "First Line\r\n";
$data .= "Second Line\r\n";
$data .= "..Third Line\r\n"; // RFC5321 4.5.2.Transparency
$data .= "\r\n";
$data .= "\r\n\r\n.\r\n";
$email->expects($this->once())->method('message')->will($this->returnValue(array('First Line')));
$this->socket->expects($this->at(0))->method('connect')->will($this->returnValue(true));
@@ -602,12 +557,13 @@ public function testSendDefaults() {
$this->socket->expects($this->at(12))->method('write')->with("DATA\r\n");
$this->socket->expects($this->at(13))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(14))->method('read')->will($this->returnValue("354 OK\r\n"));
$this->socket->expects($this->at(15))->method('write')->with($data);
$this->socket->expects($this->at(15))->method('write')->with($this->stringContains('First Line'));
$this->socket->expects($this->at(16))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(17))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(18))->method('disconnect');
$this->SmtpTransport->send($email);
}
}

0 comments on commit 66b1ba8

Please sign in to comment.
You can’t perform that action at this time.