Navigation Menu

Skip to content

Commit

Permalink
Throw exception on invalid types passed to transport().
Browse files Browse the repository at this point in the history
This also avoids notices and warnings because of the `$transport`
variable not being defined in case of an unhandled type.
  • Loading branch information
ndm2 committed Oct 15, 2014
1 parent 8397573 commit 07adfa1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Network/Email/Email.php
Expand Up @@ -937,6 +937,7 @@ public function emailFormat($format = null) {
* transport, or a transport instance.
* @return \Cake\Network\Email\AbstractTransport|$this
* @throws \LogicException When the chosen transport lacks a send method.
* @throws \InvalidArgumentException When $name is neither a string nor an object.
*/
public function transport($name = null) {
if ($name === null) {
Expand All @@ -947,6 +948,10 @@ public function transport($name = null) {
$transport = $this->_constructTransport($name);
} elseif (is_object($name)) {
$transport = $name;
} else {
throw new InvalidArgumentException(
sprintf('The value passed for the "$name" argument must be either a string, or an object, %s given.', gettype($name))
);
}
if (!method_exists($transport, 'send')) {
throw new LogicException(sprintf('The "%s" do not have send method.', get_class($transport)));
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase/Network/Email/EmailTest.php
Expand Up @@ -732,6 +732,7 @@ public function testTransport() {
* Test that using unknown transports fails.
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Transport config "Invalid" is missing.
*/
public function testTransportInvalid() {
$this->CakeEmail->transport('Invalid');
Expand All @@ -746,6 +747,16 @@ public function testTransportInstanceInvalid() {
$this->CakeEmail->transport(new \StdClass());
}

/**
* Test that using unknown transports fails.
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The value passed for the "$name" argument must be either a string, or an object, integer given.
*/
public function testTransportTypeInvalid() {
$this->CakeEmail->transport(123);
}

/**
* Test configuring a transport.
*
Expand Down

0 comments on commit 07adfa1

Please sign in to comment.