Browse Source

Add test for mailers not being reset on error.

Refs #10059
Refs #10070
Mark Story 9 years ago
parent
commit
0ab2d073ee
1 changed files with 28 additions and 0 deletions
  1. 28 0
      tests/TestCase/Mailer/MailerTest.php

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

@@ -13,6 +13,7 @@
 namespace Cake\Test\TestCase\Mailer;
 
 use Cake\TestSuite\TestCase;
+use RuntimeException;
 use TestApp\Mailer\TestMailer;
 
 class MailerTest extends TestCase
@@ -139,6 +140,33 @@ class MailerTest extends TestCase
     }
 
     /**
+     * Test that mailers call reset() when send fails
+     */
+    public function testSendFailsEmailIsReset()
+    {
+        $email = $this->getMockForEmail(['send', 'reset']);
+        $email->expects($this->once())
+            ->method('send')
+            ->will($this->throwException(new RuntimeException('kaboom')));
+
+        $mailer = $this->getMockBuilder('TestApp\Mailer\TestMailer')
+            ->setMethods(['welcome', 'reset'])
+            ->setConstructorArgs([$email])
+            ->getMock();
+
+        // Mailer should be reset even if sending fails.
+        $mailer->expects($this->once())
+            ->method('reset');
+
+        try {
+            $mailer->send('welcome', ['foo', 'bar']);
+            $this->fail('Exception should bubble up.');
+        } catch (RuntimeException $e) {
+            $this->assertTrue(true, 'Exception was raised');
+        }
+    }
+
+    /**
      * test that initial email instance config is restored after email is sent.
      *
      * @return [type]