Browse Source

Merge pull request #6885 from cakephp/jadb-patch-mailer

Auto-set template name based on action
Mark Story 10 years ago
parent
commit
293d983a0f
2 changed files with 30 additions and 0 deletions
  1. 12 0
      src/Mailer/Mailer.php
  2. 18 0
      tests/TestCase/Mailer/MailerTest.php

+ 12 - 0
src/Mailer/Mailer.php

@@ -100,6 +100,14 @@ abstract class Mailer implements ArrayAccess, EventListenerInterface
     public $layout;
 
     /**
+     * Email view template to render, defaults to the triggered mailer
+     * action's name.
+     *
+     * @var string
+     */
+    public $template;
+
+    /**
      * Email instance.
      *
      * @var \Cake\Mailer\Email
@@ -223,6 +231,10 @@ abstract class Mailer implements ArrayAccess, EventListenerInterface
 
         call_user_func_array([$this, $action], $args);
 
+        if ($this->template === null) {
+            $this->template = $action;
+        }
+
         $result = $this->_email
             ->profile((array)$this)
             ->send();

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

@@ -110,7 +110,25 @@ class MailerTest extends TestCase
             ->method('test')
             ->with('foo', 'bar');
 
+        $mailer->template = 'foobar';
         $mailer->send('test', ['foo', 'bar']);
+        $this->assertEquals($mailer->template, 'foobar');
+    }
+
+    public function testSendWithUnsetTemplateDefaultsToActionName()
+    {
+        $email = $this->getMockForEmail('send');
+        $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->send('test', ['foo', 'bar']);
+        $this->assertEquals($mailer->template, 'test');
     }
 
     /**