From 85c883e98c45e6ec80b05e3347be4f862d16fc66 Mon Sep 17 00:00:00 2001 From: Jakub Szymanowski Date: Tue, 13 Oct 2020 10:19:33 +0200 Subject: [PATCH] Add when() and unless() methods to MailMessage --- .../Notifications/Messages/MailMessage.php | 38 ++++++ .../NotificationMailMessageTest.php | 118 ++++++++++++++++++ 2 files changed, 156 insertions(+) diff --git a/src/Illuminate/Notifications/Messages/MailMessage.php b/src/Illuminate/Notifications/Messages/MailMessage.php index 08ee2f1f7433..181451217876 100644 --- a/src/Illuminate/Notifications/Messages/MailMessage.php +++ b/src/Illuminate/Notifications/Messages/MailMessage.php @@ -332,4 +332,42 @@ public function withSwiftMessage($callback) return $this; } + + /** + * Apply the callback's message changes if the given "value" is true. + * + * @param mixed $value + * @param callable $callback + * @param callable|null $default + * @return mixed|$this + */ + public function when($value, $callback, $default = null) + { + if ($value) { + return $callback($this, $value) ?: $this; + } elseif ($default) { + return $default($this, $value) ?: $this; + } + + return $this; + } + + /** + * Apply the callback's message changes if the given "value" is false. + * + * @param mixed $value + * @param callable $callback + * @param callable|null $default + * @return mixed|$this + */ + public function unless($value, $callback, $default = null) + { + if (! $value) { + return $callback($this, $value) ?: $this; + } elseif ($default) { + return $default($this, $value) ?: $this; + } + + return $this; + } } diff --git a/tests/Notifications/NotificationMailMessageTest.php b/tests/Notifications/NotificationMailMessageTest.php index 87c5e3f46558..f86949a5a369 100644 --- a/tests/Notifications/NotificationMailMessageTest.php +++ b/tests/Notifications/NotificationMailMessageTest.php @@ -132,4 +132,122 @@ public function testCallbackIsSetCorrectly() $this->assertSame([$callback], $message->callbacks); } + + public function testWhenCallback() + { + $callback = function (MailMessage $mailMessage, $condition) { + $this->assertTrue($condition); + + $mailMessage->cc('cc@example.com'); + }; + + $message = new MailMessage(); + $message->when(true, $callback); + $this->assertSame([['cc@example.com', null]], $message->cc); + + $message = new MailMessage(); + $message->when(false, $callback); + $this->assertSame([], $message->cc); + } + + public function testWhenCallbackWithReturn() + { + $callback = function (MailMessage $mailMessage, $condition) { + $this->assertTrue($condition); + + return $mailMessage->cc('cc@example.com'); + }; + + $message = new MailMessage(); + $message->when(true, $callback)->bcc('bcc@example.com'); + $this->assertSame([['cc@example.com', null]], $message->cc); + $this->assertSame([['bcc@example.com', null]], $message->bcc); + + $message = new MailMessage(); + $message->when(false, $callback)->bcc('bcc@example.com'); + $this->assertSame([], $message->cc); + $this->assertSame([['bcc@example.com', null]], $message->bcc); + } + + public function testWhenCallbackWithDefault() + { + $callback = function (MailMessage $mailMessage, $condition) { + $this->assertEquals('truthy', $condition); + + $mailMessage->cc('truthy@example.com'); + }; + + $default = function (MailMessage $mailMessage, $condition) { + $this->assertEquals(0, $condition); + + $mailMessage->cc('zero@example.com'); + }; + + $message = new MailMessage(); + $message->when('truthy', $callback, $default); + $this->assertSame([['truthy@example.com', null]], $message->cc); + + $message = new MailMessage(); + $message->when(0, $callback, $default); + $this->assertSame([['zero@example.com', null]], $message->cc); + } + + public function testUnlessCallback() + { + $callback = function (MailMessage $mailMessage, $condition) { + $this->assertFalse($condition); + + $mailMessage->cc('test@example.com'); + }; + + $message = new MailMessage(); + $message->unless(false, $callback); + $this->assertSame([['test@example.com', null]], $message->cc); + + $message = new MailMessage(); + $message->unless(true, $callback); + $this->assertSame([], $message->cc); + } + + public function testUnlessCallbackWithReturn() + { + $callback = function (MailMessage $mailMessage, $condition) { + $this->assertFalse($condition); + + return $mailMessage->cc('cc@example.com'); + }; + + $message = new MailMessage(); + $message->unless(false, $callback)->bcc('bcc@example.com'); + $this->assertSame([['cc@example.com', null]], $message->cc); + $this->assertSame([['bcc@example.com', null]], $message->bcc); + + $message = new MailMessage(); + $message->unless(true, $callback)->bcc('bcc@example.com'); + $this->assertSame([], $message->cc); + $this->assertSame([['bcc@example.com', null]], $message->bcc); + } + + public function testUnlessCallbackWithDefault() + { + $callback = function (MailMessage $mailMessage, $condition) { + $this->assertEquals(0, $condition); + + $mailMessage->cc('zero@example.com'); + }; + + $default = function (MailMessage $mailMessage, $condition) { + $this->assertEquals('truthy', $condition); + + $mailMessage->cc('truthy@example.com'); + }; + + $message = new MailMessage(); + $message->unless(0, $callback, $default); + $this->assertSame([['zero@example.com', null]], $message->cc); + + $message = new MailMessage(); + $message->unless('truthy', $callback, $default); + $this->assertSame([['truthy@example.com', null]], $message->cc); + } }