diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 8a47053f7a05..436598cf6e9f 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -340,6 +340,18 @@ public function to($address, $name = null) return $this->setAddress($address, $name, 'to'); } + /** + * Determine if the given recipient is set on the mailable. + * + * @param object|array|string $address + * @param string|null $name + * @return bool + */ + public function hasTo($address, $name = null) + { + return $this->hasRecipient($address, $name, 'to'); + } + /** * Set the recipients of the message. * @@ -352,6 +364,18 @@ public function cc($address, $name = null) return $this->setAddress($address, $name, 'cc'); } + /** + * Determine if the given recipient is set on the mailable. + * + * @param object|array|string $address + * @param string|null $name + * @return bool + */ + public function hasCc($address, $name = null) + { + return $this->hasRecipient($address, $name, 'cc'); + } + /** * Set the recipients of the message. * @@ -364,6 +388,18 @@ public function bcc($address, $name = null) return $this->setAddress($address, $name, 'bcc'); } + /** + * Determine if the given recipient is set on the mailable. + * + * @param object|array|string $address + * @param string|null $name + * @return bool + */ + public function hasBcc($address, $name = null) + { + return $this->hasRecipient($address, $name, 'bcc'); + } + /** * Set the "reply to" address of the message. * @@ -433,6 +469,34 @@ protected function normalizeRecipient($recipient) return $recipient; } + /** + * Determine if the given recipient is set on the mailable. + * + * @param object|array|string $address + * @param string|null $name + * @param string $property + * @return bool + */ + protected function hasRecipient($address, $name = null, $property = 'to') + { + $expected = $this->normalizeRecipient( + $this->addressesToArray($address, $name)[0] + ); + + $expected = [ + 'name' => isset($expected->name) ? $expected->name : null, + 'address' => $expected->email, + ]; + + return collect($this->{$property})->contains(function ($actual) use ($expected) { + if (! isset($expected['name'])) { + return $actual['address'] == $expected['address']; + } else { + return $actual == $expected; + } + }); + } + /** * Set the subject of the message. * diff --git a/tests/Mail/MailMailableTest.php b/tests/Mail/MailMailableTest.php index b4ed6a2b3a2e..6bf6b1f44deb 100644 --- a/tests/Mail/MailMailableTest.php +++ b/tests/Mail/MailMailableTest.php @@ -9,26 +9,37 @@ public function testMailableSetsRecipientsCorrectly() $mailable = new WelcomeMailableStub; $mailable->to('taylor@laravel.com'); $this->assertEquals([['name' => null, 'address' => 'taylor@laravel.com']], $mailable->to); + $this->assertTrue($mailable->hasTo('taylor@laravel.com')); $mailable = new WelcomeMailableStub; $mailable->to('taylor@laravel.com', 'Taylor Otwell'); $this->assertEquals([['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com']], $mailable->to); + $this->assertTrue($mailable->hasTo('taylor@laravel.com', 'Taylor Otwell')); + $this->assertTrue($mailable->hasTo('taylor@laravel.com')); $mailable = new WelcomeMailableStub; $mailable->to(['taylor@laravel.com']); $this->assertEquals([['name' => null, 'address' => 'taylor@laravel.com']], $mailable->to); + $this->assertTrue($mailable->hasTo('taylor@laravel.com')); + $this->assertFalse($mailable->hasTo('taylor@laravel.com', 'Taylor Otwell')); $mailable = new WelcomeMailableStub; $mailable->to([['name' => 'Taylor Otwell', 'email' => 'taylor@laravel.com']]); $this->assertEquals([['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com']], $mailable->to); + $this->assertTrue($mailable->hasTo('taylor@laravel.com', 'Taylor Otwell')); + $this->assertTrue($mailable->hasTo('taylor@laravel.com')); $mailable = new WelcomeMailableStub; $mailable->to(new MailableTestUserStub); $this->assertEquals([['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com']], $mailable->to); + $this->assertTrue($mailable->hasTo(new MailableTestUserStub)); + $this->assertTrue($mailable->hasTo('taylor@laravel.com')); $mailable = new WelcomeMailableStub; $mailable->to(collect([new MailableTestUserStub])); $this->assertEquals([['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com']], $mailable->to); + $this->assertTrue($mailable->hasTo(new MailableTestUserStub)); + $this->assertTrue($mailable->hasTo('taylor@laravel.com')); $mailable = new WelcomeMailableStub; $mailable->to(collect([new MailableTestUserStub, new MailableTestUserStub])); @@ -36,6 +47,8 @@ public function testMailableSetsRecipientsCorrectly() ['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com'], ['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com'], ], $mailable->to); + $this->assertTrue($mailable->hasTo(new MailableTestUserStub)); + $this->assertTrue($mailable->hasTo('taylor@laravel.com')); } public function testMailableBuildsViewData()