Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/mail' of https://github.com/ezimuel/zf2 into fe…
Browse files Browse the repository at this point in the history
…ature/mail-smtp-connection
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
43 changes: 39 additions & 4 deletions src/Transport/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class Smtp implements TransportInterface, Pluggable
* @var Protocol\Smtp
*/
protected $connection;

/**
* @var boolean
*/
protected $autoDisconnect = true;

/**
* @var Protocol\SmtpBroker
Expand Down Expand Up @@ -122,7 +127,26 @@ public function getBroker()
}
return $this->broker;
}

/**
* Set the automatic disconnection when destruct
*
* @param boolean $flag
* @return Smtp
*/
public function setAutoDisconnect($flag)
{
$this->autoDisconnect = (bool) $flag;
return $this;
}
/**
* Get the automatic disconnection value
*
* @return boolean
*/
public function getAutoDisconnect()
{
return $this->autoDisconnect;
}
/**
* Return an SMTP connection
*
Expand All @@ -146,11 +170,12 @@ public function __destruct()
} catch (ProtocolException\ExceptionInterface $e) {
// ignore
}
$this->connection->disconnect();
if ($this->autoDisconnect) {
$this->connection->disconnect();
}
}
}


/**
* Sets the connection protocol instance
*
Expand All @@ -171,7 +196,17 @@ public function getConnection()
{
return $this->connection;
}

/**
* Disconnect the connection protocol instance
*
* @return void
*/
public function disconnect()
{
if (!empty($this->connection) && ($this->connection instanceof Protocol\Smtp)) {
$this->connection->disconnect();
}
}
/**
* Send an email via the SMTP connection protocol
*
Expand Down
2 changes: 1 addition & 1 deletion test/TestAsset/SmtpProtocolSpy.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function disconnect()
$this->connect = false;
$this->rset();
}

/**
* "Reset" connection
*
Expand Down
34 changes: 34 additions & 0 deletions test/Transport/SmtpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,38 @@ public function testCanUseAuthenticationExtensionsViaPluginBroker()
$this->assertEquals('matthew', $connection->getUsername());
$this->assertEquals('password', $connection->getPassword());
}

public function testSetAutoDisconnect()
{
$this->transport->setAutoDisconnect(false);
$this->assertFalse($this->transport->getAutoDisconnect());
}

public function testGetDefaultAutoDisconnectValue()
{
$this->assertTrue($this->transport->getAutoDisconnect());
}

public function testAutoDisconnectTrue()
{
$this->connection->connect();
unset($this->transport);
$this->assertFalse($this->connection->isConnected());
}

public function testAutoDisconnectFalse()
{
$this->connection->connect();
$this->transport->setAutoDisconnect(false);
unset($this->transport);
$this->assertTrue($this->connection->isConnected());
}

public function testDisconnect()
{
$this->connection->connect();
$this->assertTrue($this->connection->isConnected());
$this->transport->disconnect();
$this->assertFalse($this->connection->isConnected());
}
}

0 comments on commit 2f2851a

Please sign in to comment.