浏览代码

Restore initial email instance config after email is sent.

This preserves the "default" config that is loaded when Email instance
is created.
ADmad 10 年之前
父节点
当前提交
b48cdfd5d9
共有 2 个文件被更改,包括 33 次插入0 次删除
  1. 10 0
      src/Mailer/Mailer.php
  2. 23 0
      tests/TestCase/Mailer/MailerTest.php

+ 10 - 0
src/Mailer/Mailer.php

@@ -99,6 +99,13 @@ abstract class Mailer implements EventListenerInterface
     protected $_email;
     protected $_email;
 
 
     /**
     /**
+     * Serialized Email instance config.
+     *
+     * @var string
+     */
+    protected $_emailConfig;
+
+    /**
      * Constructor.
      * Constructor.
      *
      *
      * @param \Cake\Mailer\Email|null $email Email instance.
      * @param \Cake\Mailer\Email|null $email Email instance.
@@ -110,6 +117,7 @@ abstract class Mailer implements EventListenerInterface
         }
         }
 
 
         $this->_email = $email;
         $this->_email = $email;
+        $this->_emailConfig = $this->_email->serialize();
     }
     }
 
 
     /**
     /**
@@ -201,6 +209,8 @@ abstract class Mailer implements EventListenerInterface
         $result = $this->_email->send();
         $result = $this->_email->send();
 
 
         $this->reset();
         $this->reset();
+        $this->_email->unserialize($this->_emailConfig);
+
         return $result;
         return $result;
     }
     }
 
 

+ 23 - 0
tests/TestCase/Mailer/MailerTest.php

@@ -132,6 +132,29 @@ class MailerTest extends TestCase
     }
     }
 
 
     /**
     /**
+     * test that initial email instance config is restored after email is sent.
+     *
+     * @return [type]
+     */
+    public function testDefaultProfileRestoration()
+    {
+        $email = $this->getMockForEmail('send', [['template' => 'cakephp']]);
+        $email->expects($this->any())
+            ->method('send')
+            ->will($this->returnValue([]));
+
+        $mailer = $this->getMock('TestApp\Mailer\TestMailer', ['test'], [$email]);
+        $mailer->expects($this->once())
+            ->method('test')
+            ->with('foo', 'bar');
+
+        $mailer->template('test');
+        $mailer->send('test', ['foo', 'bar']);
+        $this->assertEquals($mailer->template, 'test');
+        $this->assertEquals('cakephp', $mailer->viewBuilder()->template());
+    }
+
+    /**
      * @expectedException Cake\Mailer\Exception\MissingActionException
      * @expectedException Cake\Mailer\Exception\MissingActionException
      * @expectedExceptionMessage Mail TestMailer::test() could not be found, or is not accessible.
      * @expectedExceptionMessage Mail TestMailer::test() could not be found, or is not accessible.
      */
      */