Browse Source

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.

ndm2 11 years ago
parent
commit
07adfa14f6
2 changed files with 16 additions and 0 deletions
  1. 5 0
      src/Network/Email/Email.php
  2. 11 0
      tests/TestCase/Network/Email/EmailTest.php

+ 5 - 0
src/Network/Email/Email.php

@@ -937,6 +937,7 @@ class Email {
  *   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) {
@@ -947,6 +948,10 @@ class Email {
 			$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)));

+ 11 - 0
tests/TestCase/Network/Email/EmailTest.php

@@ -732,6 +732,7 @@ class EmailTest extends TestCase {
  * Test that using unknown transports fails.
  *
  * @expectedException InvalidArgumentException
+ * @expectedExceptionMessage Transport config "Invalid" is missing.
  */
 	public function testTransportInvalid() {
 		$this->CakeEmail->transport('Invalid');
@@ -747,6 +748,16 @@ class EmailTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void