Browse Source

Restore initial email instance config after email is sent.

This preserves the "default" config that is loaded when Email instance
is created.
ADmad 10 years ago
parent
commit
b48cdfd5d9
2 changed files with 33 additions and 0 deletions
  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;
 
     /**
+     * Serialized Email instance config.
+     *
+     * @var string
+     */
+    protected $_emailConfig;
+
+    /**
      * Constructor.
      *
      * @param \Cake\Mailer\Email|null $email Email instance.
@@ -110,6 +117,7 @@ abstract class Mailer implements EventListenerInterface
         }
 
         $this->_email = $email;
+        $this->_emailConfig = $this->_email->serialize();
     }
 
     /**
@@ -201,6 +209,8 @@ abstract class Mailer implements EventListenerInterface
         $result = $this->_email->send();
 
         $this->reset();
+        $this->_email->unserialize($this->_emailConfig);
+
         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
      * @expectedExceptionMessage Mail TestMailer::test() could not be found, or is not accessible.
      */