Skip to content

Commit 07adfa1

Browse files
author
ndm2
committed
Throw exception on invalid types passed to transport().
This also avoids notices and warnings because of the `$transport` variable not being defined in case of an unhandled type.
1 parent 8397573 commit 07adfa1

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Network/Email/Email.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ public function emailFormat($format = null) {
937937
* transport, or a transport instance.
938938
* @return \Cake\Network\Email\AbstractTransport|$this
939939
* @throws \LogicException When the chosen transport lacks a send method.
940+
* @throws \InvalidArgumentException When $name is neither a string nor an object.
940941
*/
941942
public function transport($name = null) {
942943
if ($name === null) {
@@ -947,6 +948,10 @@ public function transport($name = null) {
947948
$transport = $this->_constructTransport($name);
948949
} elseif (is_object($name)) {
949950
$transport = $name;
951+
} else {
952+
throw new InvalidArgumentException(
953+
sprintf('The value passed for the "$name" argument must be either a string, or an object, %s given.', gettype($name))
954+
);
950955
}
951956
if (!method_exists($transport, 'send')) {
952957
throw new LogicException(sprintf('The "%s" do not have send method.', get_class($transport)));

tests/TestCase/Network/Email/EmailTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ public function testTransport() {
732732
* Test that using unknown transports fails.
733733
*
734734
* @expectedException InvalidArgumentException
735+
* @expectedExceptionMessage Transport config "Invalid" is missing.
735736
*/
736737
public function testTransportInvalid() {
737738
$this->CakeEmail->transport('Invalid');
@@ -746,6 +747,16 @@ public function testTransportInstanceInvalid() {
746747
$this->CakeEmail->transport(new \StdClass());
747748
}
748749

750+
/**
751+
* Test that using unknown transports fails.
752+
*
753+
* @expectedException InvalidArgumentException
754+
* @expectedExceptionMessage The value passed for the "$name" argument must be either a string, or an object, integer given.
755+
*/
756+
public function testTransportTypeInvalid() {
757+
$this->CakeEmail->transport(123);
758+
}
759+
749760
/**
750761
* Test configuring a transport.
751762
*

0 commit comments

Comments
 (0)