Browse Source

Merge branch '4.x-assertMailContainsAttachment' into 4.x

Refs #13216
Mark Story 7 years ago
parent
commit
07763cbf0a

+ 5 - 0
psalm-baseline.xml

@@ -2501,6 +2501,11 @@
       <code>$type</code>
     </PropertyNotSetInConstructor>
   </file>
+  <file src="src/TestSuite/Constraint/Email/MailContainsAttachment.php">
+    <PropertyNotSetInConstructor occurrences="1">
+      <code>MailContainsAttachment</code>
+    </PropertyNotSetInConstructor>
+  </file>
   <file src="src/TestSuite/Constraint/Email/MailContainsHtml.php">
     <DeprecatedConstant occurrences="1">
       <code>Email::MESSAGE_HTML</code>

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

@@ -0,0 +1,77 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * 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         4.0.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): bool
+    {
+        [$expectedFilename, $expectedFileInfo] = $other;
+
+        $messages = $this->getMessages();
+        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(): string
+    {
+        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): string
+    {
+        [$expectedFilename] = $other;
+
+        return '\'' . $expectedFilename . '\' ' . $this->toString();
+    }
+}

+ 14 - 0
src/TestSuite/EmailTrait.php

@@ -17,6 +17,7 @@ declare(strict_types=1);
 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;
@@ -195,6 +196,19 @@ trait EmailTrait
     }
 
     /**
+     * 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(string $filename, array $file = [], string $message = ''): void
+    {
+        $this->assertThat([$filename, $file], new MailContainsAttachment(), $message);
+    }
+
+    /**
      * Asserts an email contains expected html contents
      *
      * @param string $contents Contents

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

@@ -88,6 +88,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');
@@ -193,6 +196,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']],
@@ -214,6 +218,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');