Преглед изворни кода

ZF-9011 Zend_Mail_Transport_Sendmail now accepts arrays and instances of zend_config as constructor. ::_sendMail() now throws exception on invalid params.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20780 44c647ce-9c0f-0410-b52a-842ac1e357ba
freak пре 16 година
родитељ
комит
98668a049e
2 измењених фајлова са 69 додато и 1 уклоњено
  1. 24 1
      library/Zend/Mail/Transport/Sendmail.php
  2. 45 0
      tests/Zend/Mail/MailTest.php

+ 24 - 1
library/Zend/Mail/Transport/Sendmail.php

@@ -69,11 +69,19 @@ class Zend_Mail_Transport_Sendmail extends Zend_Mail_Transport_Abstract
     /**
      * Constructor.
      *
-     * @param  string $parameters OPTIONAL (Default: null)
+     * @param  string|array|Zend_Config $parameters OPTIONAL (Default: null)
      * @return void
      */
     public function __construct($parameters = null)
     {
+		if ($parameters instanceof Zend_Config) { 
+			$parameters = $parameters->toArray(); 
+		}
+
+		if (is_array($parameters)) { 
+			$parameters = implode(' ', $parameters);
+		}
+		
         $this->parameters = $parameters;
     }
 
@@ -83,6 +91,8 @@ class Zend_Mail_Transport_Sendmail extends Zend_Mail_Transport_Abstract
      *
      * @access public
      * @return void
+     * @throws Zend_Mail_Transport_Exception if parameters is set
+     *         but not a string
      * @throws Zend_Mail_Transport_Exception on mail() failure
      */
     public function _sendMail()
@@ -95,6 +105,19 @@ class Zend_Mail_Transport_Sendmail extends Zend_Mail_Transport_Abstract
                 $this->body,
                 $this->header);
         } else {
+        	if(!is_string($this->parameters)) {
+	            /**
+	             * @see Zend_Mail_Transport_Exception
+	             * 
+	             * Exception is thrown here because
+	             * $parameters is a public property
+	             */
+                require_once 'Zend/Mail/Transport/Exception.php';
+                throw new Zend_Mail_Transport_Exception(
+                    'Parameters were set but are not a string'
+                );
+        	}
+
             $result = mail(
                 $this->recipients,
                 $this->_mail->getSubject(),

+ 45 - 0
tests/Zend/Mail/MailTest.php

@@ -51,6 +51,11 @@ require_once 'Zend/Mail/Transport/Smtp.php';
 require_once 'Zend/Date.php';
 
 /**
+ * Zend_Config
+ */
+require_once 'Zend/Config.php';
+
+/**
  * Mock mail transport class for testing purposes
  *
  * @category   Zend
@@ -897,6 +902,46 @@ class Zend_Mail_MailTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($headers['From'][0], 'John Doe <john@example.com>');
         $this->assertEquals($headers['Reply-To'][0], 'Foo Bar <foo@example.com>');
     }
+    
+    /**
+     * @group ZF-9011
+     */
+    public function testSendmailTransportShouldAcceptConfigAndArrayAsConstructor()
+    {
+        $mail = new Zend_Mail("UTF-8");
+        $mail->setBodyText('My Nice Test Text');
+        $mail->addTo('foobar@example.com');
+        $mail->setSubject('hello world!');
+
+        $params = array('envelope'=> '-tjohn@example.com', 'foo' => '-fbar');
+        $expected = '-tjohn@example.com -fbar';
+        
+        $transportMock = new Zend_Mail_Transport_Sendmail_Mock($params);
+        $this->assertEquals($expected, $transportMock->parameters);
+
+        $transportMock = new Zend_Mail_Transport_Sendmail_Mock(new Zend_Config($params));
+        $this->assertEquals($expected, $transportMock->parameters);
+    }
+    
+    /**
+     * @group ZF-9011
+     */
+    public function testSendmailTransportThrowsExceptionWithInvalidParams()
+    {
+        $mail = new Zend_Mail("UTF-8");
+        $mail->setBodyText('My Nice Test Text');
+        $mail->addTo('foobar@example.com');
+        $mail->setSubject('hello world!');
+
+        $transport = new Zend_Mail_Transport_Sendmail();
+        $transport->parameters = true;
+        try {
+            $mail->send();
+            $this->fail('Exception should have been thrown, but wasn\'t');    	
+        } catch(Zend_Mail_Transport_Exception $e) {
+        	// do nothing
+        }
+    }
 
     public static function dataSubjects()
     {