Browse Source

Added assertions for text and html contents of emails

Jeremy Harris 8 years ago
parent
commit
f2ceb41c30

+ 1 - 1
src/TestSuite/Constraint/Email/MailConstraintBase.php

@@ -40,7 +40,7 @@ abstract class MailConstraintBase extends Constraint
     /**
      * Gets the email or emails to check
      *
-     * @return \Cake\Mailer\Email|array
+     * @return \Cake\Mailer\Email[]
      */
     public function getEmails()
     {

+ 11 - 2
src/TestSuite/Constraint/Email/MailContains.php

@@ -21,6 +21,13 @@ class MailContains extends MailConstraintBase
 {
 
     /**
+     * Mail type to check contents of
+     *
+     * @var string
+     */
+    protected $type;
+
+    /**
      * Checks constraint
      *
      * @param mixed $other Constraint check
@@ -30,9 +37,11 @@ class MailContains extends MailConstraintBase
     {
         $emails = $this->getEmails();
         foreach ($emails as $email) {
-            $message = implode("\r\n", (array)$email->message());
+            $message = implode("\r\n", (array)$email->message($this->type));
 
-            return preg_match("/$other/", $message) !== false;
+            if (preg_match("/$other/", $message) > 0) {
+                return true;
+            }
         }
 
         return false;

+ 39 - 0
src/TestSuite/Constraint/Email/MailContainsHtml.php

@@ -0,0 +1,39 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.7.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\TestSuite\Constraint\Email;
+
+use Cake\Mailer\Email;
+
+/**
+ * MailContainsHtml
+ */
+class MailContainsHtml extends MailContains
+{
+    protected $type = Email::MESSAGE_HTML;
+
+    /**
+     * Assertion message string
+     *
+     * @return string
+     */
+    public function toString()
+    {
+        if ($this->at) {
+            return sprintf('is in the html message of email #%d', $this->at);
+        }
+
+        return 'is in the html message of an email';
+    }
+}

+ 39 - 0
src/TestSuite/Constraint/Email/MailContainsText.php

@@ -0,0 +1,39 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.7.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\TestSuite\Constraint\Email;
+
+use Cake\Mailer\Email;
+
+/**
+ * MailContainsText
+ */
+class MailContainsText extends MailContains
+{
+    protected $type = Email::MESSAGE_TEXT;
+
+    /**
+     * Assertion message string
+     *
+     * @return string
+     */
+    public function toString()
+    {
+        if ($this->at) {
+            return sprintf('is in the text message of email #%d', $this->at);
+        }
+
+        return 'is in the text message of an email';
+    }
+}

+ 52 - 0
src/TestSuite/EmailTrait.php

@@ -15,6 +15,8 @@
 namespace Cake\TestSuite;
 
 use Cake\TestSuite\Constraint\Email\MailContains;
+use Cake\TestSuite\Constraint\Email\MailContainsHtml;
+use Cake\TestSuite\Constraint\Email\MailContainsText;
 use Cake\TestSuite\Constraint\Email\MailCount;
 use Cake\TestSuite\Constraint\Email\MailSentFrom;
 use Cake\TestSuite\Constraint\Email\MailSentTo;
@@ -98,6 +100,32 @@ trait EmailTrait
     }
 
     /**
+     * Asserts an email at a specific index contains expected html contents
+     *
+     * @param int $at Email index
+     * @param int $contents Contents
+     * @param string $message Message
+     * @return void
+     */
+    public function assertMailContainsHtmlAt($at, $contents, $message = null)
+    {
+        $this->assertThat($contents, new MailContainsHtml($at), $message);
+    }
+
+    /**
+     * Asserts an email at a specific index contains expected text contents
+     *
+     * @param int $at Email index
+     * @param int $contents Contents
+     * @param string $message Message
+     * @return void
+     */
+    public function assertMailContainsTextAt($at, $contents, $message = null)
+    {
+        $this->assertThat($contents, new MailContainsText($at), $message);
+    }
+
+    /**
      * Asserts an email at a specific index contains the expected value within an Email getter
      *
      * @param int $at Email index
@@ -148,6 +176,30 @@ trait EmailTrait
     }
 
     /**
+     * Asserts an email contains expected html contents
+     *
+     * @param int $contents Contents
+     * @param string $message Message
+     * @return void
+     */
+    public function assertMailContainsHtml($contents, $message = null)
+    {
+        $this->assertThat($contents, new MailContainsHtml(), $message);
+    }
+
+    /**
+     * Asserts an email contains expected text contents
+     *
+     * @param int $contents Contents
+     * @param string $message Message
+     * @return void
+     */
+    public function assertMailContainsText($contents, $message = null)
+    {
+        $this->assertThat($contents, new MailContainsText(), $message);
+    }
+
+    /**
      * Asserts an email contains the expected value within an Email getter
      *
      * @param int $expected Contents

+ 1 - 1
src/TestSuite/TestEmailTransport.php

@@ -61,7 +61,7 @@ class TestEmailTransport extends AbstractTransport
     /**
      * Gets emails sent
      *
-     * @return array
+     * @return \Cake\Mailer\Email[]
      */
     public static function getEmails()
     {

+ 40 - 5
tests/TestCase/TestSuite/EmailTraitTest.php

@@ -79,7 +79,8 @@ class EmailTraitTest extends TestCase
         $this->assertMailSentTo('to@example.com');
         $this->assertMailSentTo('to2@example.com');
 
-        $this->assertMailContains('message');
+        $this->assertMailContains('text');
+        $this->assertMailContains('html');
 
         $this->assertMailSentWith('Hello world', 'subject');
         $this->assertMailSentWith('cc@example.com', 'cc');
@@ -106,8 +107,8 @@ class EmailTraitTest extends TestCase
         $this->assertMailSentToAt(0, 'to@example.com');
         $this->assertMailSentToAt(1, 'to2@example.com');
 
-        $this->assertMailContainsAt(0, 'message');
-        $this->assertMailContainsAt(1, 'message 2');
+        $this->assertMailContainsAt(0, 'text');
+        $this->assertMailContainsAt(1, 'html');
 
         $this->assertMailSentWithAt(0, 'Hello world', 'subject');
     }
@@ -127,6 +128,34 @@ class EmailTraitTest extends TestCase
     }
 
     /**
+     * tests assertMailContainsHtml fails appropriately
+     *
+     * @return void
+     */
+    public function testAssertContainsHtmlFailure()
+    {
+        $this->expectException(AssertionFailedError::class);
+
+        $this->sendEmails();
+
+        $this->assertMailContainsHtmlAt(0, 'text');
+    }
+
+    /**
+     * tests assertMailContainsText fails appropriately
+     *
+     * @return void
+     */
+    public function testAssertContainsTextFailure()
+    {
+        $this->expectException(AssertionFailedError::class);
+
+        $this->sendEmails();
+
+        $this->assertMailContainsTextAt(1, 'html');
+    }
+
+    /**
      * tests constraint failure messages
      *
      * @param string $assertion Assertion method
@@ -158,7 +187,11 @@ class EmailTraitTest extends TestCase
             'assertMailSentWith' => ['assertMailSentWith', 'Failed asserting that \'Missing\' is in an email `subject`.', ['Missing', 'subject']],
             'assertMailSentWithAt' => ['assertMailSentWithAt', 'Failed asserting that \'Missing\' is in email #1 `subject`.', [1, 'Missing', 'subject']],
             'assertMailContains' => ['assertMailContains', 'Failed asserting that \'Missing\' is in an email.', ['Missing']],
+            'assertMailContainsHtml' => ['assertMailContainsHtml', 'Failed asserting that \'Missing\' is in the html message of an email.', ['Missing']],
+            'assertMailContainsText' => ['assertMailContainsText', 'Failed asserting that \'Missing\' is in the text message of an email.', ['Missing']],
             'assertMailContainsAt' => ['assertMailContainsAt', 'Failed asserting that \'Missing\' is in email #1.', [1, 'Missing']],
+            'assertMailContainsHtmlAt' => ['assertMailContainsHtmlAt', 'Failed asserting that \'Missing\' is in the html message of email #1.', [1, 'Missing']],
+            'assertMailContainsTextAt' => ['assertMailContainsTextAt', 'Failed asserting that \'Missing\' is in the text message of email #1.', [1, 'Missing']],
         ];
     }
 
@@ -174,11 +207,13 @@ class EmailTraitTest extends TestCase
             ->setCc('cc@example.com')
             ->setBcc(['bcc@example.com' => 'Baz Qux'])
             ->setSubject('Hello world')
-            ->send('message');
+            ->setEmailFormat(Email::MESSAGE_TEXT)
+            ->send('text');
 
         (new Email('alternate'))
             ->setTo('to2@example.com')
             ->setCc('cc2@example.com')
-            ->send('message 2');
+            ->setEmailFormat(Email::MESSAGE_HTML)
+            ->send('html');
     }
 }