Skip to content

Commit

Permalink
Make connect() invoke authentication, make tests more explicit, add…
Browse files Browse the repository at this point in the history
… more tests.
  • Loading branch information
ndm2 committed Jul 17, 2014
1 parent 66b1ba8 commit c2020da
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Network/Email/SmtpTransport.php
Expand Up @@ -90,6 +90,7 @@ public function __destruct() {
public function connect() {
if (!$this->connected()) {
$this->_connect();
$this->_auth();
}
}

Expand Down
61 changes: 58 additions & 3 deletions tests/TestCase/Network/Email/SmtpTransportTest.php
Expand Up @@ -82,7 +82,7 @@ public function setUp() {
parent::setUp();
$this->socket = $this->getMock(
'Cake\Network\Socket',
array('read', 'write', 'connect', 'enableCrypto')
array('read', 'write', 'connect', 'disconnect', 'enableCrypto')
);

$this->SmtpTransport = new SmtpTestTransport();
Expand Down Expand Up @@ -166,7 +166,6 @@ public function testConnectEhloNoTlsOnRequiredTlsServer() {
$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized authentication type\r\n"));
$this->SmtpTransport->connect();
$this->SmtpTransport->auth();
}

/**
Expand Down Expand Up @@ -458,17 +457,68 @@ public function testBufferResponseLines() {
$this->assertEquals($expected, $result);
}

/**
* testExplicitConnectAlreadyConnected method
*
* @return void
*/
public function testExplicitConnectAlreadyConnected() {
$this->socket->expects($this->never())->method('connect');
$this->socket->connected = true;
$this->SmtpTransport->connect();
}

/**
* testConnected method
*
* @return void
*/
public function testConnected() {
$this->socket->connected = true;
$this->assertTrue($this->SmtpTransport->connected());

$this->socket->connected = false;
$this->assertFalse($this->SmtpTransport->connected());
}

/**
* testAutoDisconnect method
*
* @return void
*/
public function testAutoDisconnect() {
$this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
$this->socket->expects($this->at(1))->method('disconnect');
$this->socket->connected = true;
unset($this->SmtpTransport);
}

/**
* testExplicitDisconnect method
*
* @return void
*/
public function testExplicitDisconnect() {
$this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
$this->socket->expects($this->at(1))->method('disconnect');
$this->socket->connected = true;
$this->SmtpTransport->disconnect();
}

/**
* testExplicitDisconnectNotConnected method
*
* @return void
*/
public function testExplicitDisconnectNotConnected() {
$callback = function($arg) {
$this->assertNotEquals("QUIT\r\n", $arg);
};
$this->socket->expects($this->any())->method('write')->will($this->returnCallback($callback));
$this->socket->expects($this->never())->method('disconnect');
$this->SmtpTransport->disconnect();
}

/**
* testKeepAlive method
*
Expand All @@ -482,6 +532,10 @@ public function testKeepAlive() {
$email->to('cake@cakephp.org', 'CakePHP');
$email->expects($this->exactly(2))->method('message')->will($this->returnValue(array('First Line')));

$callback = function($arg) {
$this->assertNotEquals("QUIT\r\n", $arg);
};
$this->socket->expects($this->any())->method('write')->will($this->returnCallback($callback));
$this->socket->expects($this->never())->method('disconnect');

$this->socket->expects($this->at(0))->method('connect')->will($this->returnValue(true));
Expand Down Expand Up @@ -561,7 +615,8 @@ public function testSendDefaults() {
$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->socket->expects($this->at(18))->method('write')->with("QUIT\r\n");
$this->socket->expects($this->at(19))->method('disconnect');

$this->SmtpTransport->send($email);
}
Expand Down

0 comments on commit c2020da

Please sign in to comment.