Browse Source

deprecate Mailer::setMessage

Kevin Pfeifer 1 year ago
parent
commit
33f29ef172
2 changed files with 12 additions and 2 deletions
  1. 6 0
      src/Mailer/Mailer.php
  2. 6 2
      tests/TestCase/Mailer/MailerTest.php

+ 6 - 0
src/Mailer/Mailer.php

@@ -22,6 +22,7 @@ use Cake\Mailer\Exception\MissingActionException;
 use Cake\ORM\Locator\LocatorAwareTrait;
 use Cake\View\ViewBuilder;
 use InvalidArgumentException;
+use function Cake\Core\deprecationWarning;
 
 /**
  * Mailer base class.
@@ -259,9 +260,14 @@ class Mailer implements EventListenerInterface
      *
      * @param \Cake\Mailer\Message $message Message instance.
      * @return $this
+     * @deprecated 5.1.0 Configure the mailer according to the documentation instead of manually setting the Message instance.
      */
     public function setMessage(Message $message)
     {
+        deprecationWarning(
+            '5.1.0',
+            'Setting the message instance is deprecated. Configure the mailer according to the documentation instead.'
+        );
         $this->message = $message;
 
         return $this;

+ 6 - 2
tests/TestCase/Mailer/MailerTest.php

@@ -27,6 +27,7 @@ use Cake\TestSuite\TestCase;
 use Cake\View\Exception\MissingTemplateException;
 use DateTime;
 use InvalidArgumentException;
+use PHPUnit\Framework\Attributes\WithoutErrorHandler;
 use TestApp\Mailer\TestMailer;
 use function Cake\Core\env;
 
@@ -107,13 +108,16 @@ class MailerTest extends TestCase
     /**
      * testMessage function
      */
-    public function testMessage(): void
+    #[WithoutErrorHandler]
+    public function testSetMessage(): void
     {
         $message = $this->mailer->getMessage();
         $this->assertInstanceOf(Message::class, $message);
 
         $newMessage = new Message();
-        $this->mailer->setMessage($newMessage);
+        $this->deprecated(function () use ($newMessage): void {
+            $this->mailer->setMessage($newMessage);
+        });
         $this->assertSame($newMessage, $this->mailer->getMessage());
         $this->assertNotSame($message, $newMessage);
     }