Browse Source

Backport 4.next assertMailContainsAttachment() to 3.next

mscherer 6 years ago
parent
commit
d92679e6d6

+ 3 - 0
src/TestSuite/Constraint/Email/MailConstraintBase.php

@@ -24,6 +24,9 @@ use PHPUnit\Framework\Constraint\Constraint;
  */
 abstract class MailConstraintBase extends Constraint
 {
+    /**
+     * @var int|null
+     */
     protected $at;
 
     /**

+ 76 - 0
src/TestSuite/Constraint/Email/MailContainsAttachment.php

@@ -0,0 +1,76 @@
+<?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.9.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\TestSuite\Constraint\Email;
+
+/**
+ * MailContainsAttachment
+ *
+ * @internal
+ */
+class MailContainsAttachment extends MailContains
+{
+    /**
+     * Checks constraint
+     *
+     * @param mixed $other Constraint check
+     * @return bool
+     */
+    public function matches($other)
+    {
+        list($expectedFilename, $expectedFileInfo) = $other;
+
+        $messages = $this->getEmails();
+        foreach ($messages as $message) {
+            foreach ($message->getAttachments() as $filename => $fileInfo) {
+                if ($filename === $expectedFilename && empty($expectedFileInfo)) {
+                    return true;
+                }
+                if (!empty($expectedFileInfo) && array_intersect($expectedFileInfo, $fileInfo) === $expectedFileInfo) {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
+    /**
+     * Assertion message string
+     *
+     * @return string
+     */
+    public function toString()
+    {
+        if ($this->at) {
+            return sprintf('is an attachment of email #%d', $this->at);
+        }
+
+        return 'is an attachment of an email';
+    }
+
+    /**
+     * Overwrites the descriptions so we can remove the automatic "expected" message
+     *
+     * @param mixed $other Value
+     * @return string
+     */
+    protected function failureDescription($other)
+    {
+        list($expectedFilename) = $other;
+
+        return '\'' . $expectedFilename . '\' ' . $this->toString();
+    }
+}

+ 28 - 14
src/TestSuite/EmailTrait.php

@@ -15,6 +15,7 @@
 namespace Cake\TestSuite;
 
 use Cake\TestSuite\Constraint\Email\MailContains;
+use Cake\TestSuite\Constraint\Email\MailContainsAttachment;
 use Cake\TestSuite\Constraint\Email\MailContainsHtml;
 use Cake\TestSuite\Constraint\Email\MailContainsText;
 use Cake\TestSuite\Constraint\Email\MailCount;
@@ -61,7 +62,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailCount($count, $message = null)
+    public function assertMailCount($count, $message = '')
     {
         $this->assertThat($count, new MailCount(), $message);
     }
@@ -73,7 +74,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertNoMailSent($message = null)
+    public function assertNoMailSent($message = '')
     {
         $this->assertThat(null, new NoMailSent(), $message);
     }
@@ -86,7 +87,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailSentToAt($at, $address, $message = null)
+    public function assertMailSentToAt($at, $address, $message = '')
     {
         $this->assertThat($address, new MailSentTo($at), $message);
     }
@@ -99,7 +100,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailSentFromAt($at, $address, $message = null)
+    public function assertMailSentFromAt($at, $address, $message = '')
     {
         $this->assertThat($address, new MailSentFrom($at), $message);
     }
@@ -112,7 +113,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailContainsAt($at, $contents, $message = null)
+    public function assertMailContainsAt($at, $contents, $message = '')
     {
         $this->assertThat($contents, new MailContains($at), $message);
     }
@@ -125,7 +126,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailContainsHtmlAt($at, $contents, $message = null)
+    public function assertMailContainsHtmlAt($at, $contents, $message = '')
     {
         $this->assertThat($contents, new MailContainsHtml($at), $message);
     }
@@ -138,7 +139,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailContainsTextAt($at, $contents, $message = null)
+    public function assertMailContainsTextAt($at, $contents, $message = '')
     {
         $this->assertThat($contents, new MailContainsText($at), $message);
     }
@@ -152,7 +153,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailSentWithAt($at, $expected, $parameter, $message = null)
+    public function assertMailSentWithAt($at, $expected, $parameter, $message = '')
     {
         $this->assertThat($expected, new MailSentWith($at, $parameter), $message);
     }
@@ -164,7 +165,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailSentTo($address, $message = null)
+    public function assertMailSentTo($address, $message = '')
     {
         $this->assertThat($address, new MailSentTo(), $message);
     }
@@ -176,7 +177,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailSentFrom($address, $message = null)
+    public function assertMailSentFrom($address, $message = '')
     {
         $this->assertThat($address, new MailSentFrom(), $message);
     }
@@ -188,19 +189,32 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailContains($contents, $message = null)
+    public function assertMailContains($contents, $message = '')
     {
         $this->assertThat($contents, new MailContains(), $message);
     }
 
     /**
+     * Asserts an email contains expected attachment
+     *
+     * @param string $filename Filename
+     * @param array $file Additional file properties
+     * @param string $message Message
+     * @return void
+     */
+    public function assertMailContainsAttachment($filename, array $file = [], $message = '')
+    {
+        $this->assertThat([$filename, $file], new MailContainsAttachment(), $message);
+    }
+
+    /**
      * Asserts an email contains expected html contents
      *
      * @param string $contents Contents
      * @param string $message Message
      * @return void
      */
-    public function assertMailContainsHtml($contents, $message = null)
+    public function assertMailContainsHtml($contents, $message = '')
     {
         $this->assertThat($contents, new MailContainsHtml(), $message);
     }
@@ -212,7 +226,7 @@ trait EmailTrait
      * @param string $message Message to display if assertion fails.
      * @return void
      */
-    public function assertMailContainsText($expectedText, $message = null)
+    public function assertMailContainsText($expectedText, $message = '')
     {
         $this->assertThat($expectedText, new MailContainsText(), $message);
     }
@@ -225,7 +239,7 @@ trait EmailTrait
      * @param string $message Message
      * @return void
      */
-    public function assertMailSentWith($expected, $parameter, $message = null)
+    public function assertMailSentWith($expected, $parameter, $message = '')
     {
         $this->assertThat($expected, new MailSentWith(null, $parameter), $message);
     }

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

@@ -86,6 +86,9 @@ class EmailTraitTest extends TestCase
         $this->assertMailContains('text');
         $this->assertMailContains('html');
 
+        $this->assertMailContainsAttachment('custom_name.php');
+        $this->assertMailContainsAttachment('custom_name.php', ['file' => CAKE . 'basics.php']);
+
         $this->assertMailSentWith('Hello world', 'subject');
         $this->assertMailSentWith('cc@example.com', 'cc');
         $this->assertMailSentWith('bcc@example.com', 'bcc');
@@ -210,6 +213,7 @@ 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']],
+            'assertMailContainsAttachment' => ['assertMailContainsAttachment', 'Failed asserting that \'no_existing_file.php\' is an attachment of an email.', ['no_existing_file.php']],
             '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']],
@@ -231,6 +235,7 @@ class EmailTraitTest extends TestCase
             ->setCc('cc@example.com')
             ->setBcc(['bcc@example.com' => 'Baz Qux'])
             ->setSubject('Hello world')
+            ->setAttachments(['custom_name.php' => CAKE . 'basics.php'])
             ->setEmailFormat(Email::MESSAGE_TEXT)
             ->send('text');