From a94627b02f8c1e293e34af8bc32aea3b3619b6eb Mon Sep 17 00:00:00 2001 From: Florin Marina Date: Wed, 25 May 2022 15:29:40 +0300 Subject: [PATCH 1/5] Add reply to list support --- lib/mail/Mail.php | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/lib/mail/Mail.php b/lib/mail/Mail.php index 05a4c417..7414006f 100644 --- a/lib/mail/Mail.php +++ b/lib/mail/Mail.php @@ -78,6 +78,9 @@ class Mail implements \JsonSerializable /** @var $reply_to ReplyTo Email to be use when replied to */ private $reply_to; + /** @var $reply_to_list ReplyToList Email to be use when replied to */ + private $reply_to_list; + /** @var $personalization Personalization[] Messages and their metadata */ private $personalization; @@ -982,6 +985,56 @@ public function getReplyTo() return $this->reply_to; } + /** + * Add the reply to email address to a Mail object + * + * @param $list array + * both of next formats are supported + * 'replytolist' => [ + * [ + * 'email' => 'email1@domain.com', + * 'name' => 'name one', + * ], [ + * 'email' => 'email2@domain.com', + * 'name' => 'name two', + * ], + * ], + * 'replytolist' => [ + * 'email1@domain.com', + * 'email2@domain.com', + * '', + * ], + */ + public function setReplyToList(array $list) + { + foreach ($list as $l) { + if ($l instanceof ReplyTo ) { + $this->reply_to_list[] = $l; + }else{ + if (is_array($l)) { + if (!empty($l) && $l['email'] !== '') { + $this->reply_to_list[] = new ReplyTo($l['email'], $l['name']); + } + }else{ + if ($l !=='') { + $this->reply_to_list[] = new ReplyTo($l); + } + } + } + } + } + + /** + * Retrieve the reply to list to information attached to a Mail object + * + * @return ReplyToList + */ + + public function getReplyToList() + { + return $this->reply_to_list; + } + /** * Add a subject to a Mail object * @@ -1971,6 +2024,7 @@ static function ($value) { )), 'from' => $this->getFrom(), 'reply_to' => $this->getReplyTo(), + 'reply_to_list' => $this->getReplyToList(), 'subject' => $this->getGlobalSubject(), 'content' => $this->getContents(), 'attachments' => $this->getAttachments(), From dc90d5367190459093ea82b40fe2db0017c05752 Mon Sep 17 00:00:00 2001 From: Florin Marina Date: Thu, 26 May 2022 18:38:04 +0300 Subject: [PATCH 2/5] Fix spaces --- lib/mail/Mail.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mail/Mail.php b/lib/mail/Mail.php index 7414006f..5653cf19 100644 --- a/lib/mail/Mail.php +++ b/lib/mail/Mail.php @@ -1010,12 +1010,12 @@ public function setReplyToList(array $list) foreach ($list as $l) { if ($l instanceof ReplyTo ) { $this->reply_to_list[] = $l; - }else{ + } else { if (is_array($l)) { if (!empty($l) && $l['email'] !== '') { $this->reply_to_list[] = new ReplyTo($l['email'], $l['name']); } - }else{ + } else { if ($l !=='') { $this->reply_to_list[] = new ReplyTo($l); } From 321f0abbfc5f1a3c26ac2fab14d42d2ef547bccc Mon Sep 17 00:00:00 2001 From: Florin Marina Date: Wed, 5 Jun 2024 21:31:58 +0300 Subject: [PATCH 3/5] Update method add Unit testing --- lib/mail/Mail.php | 67 ++++++++++++++--------------- test/unit/ReplyToListTest.php | 79 +++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 33 deletions(-) create mode 100644 test/unit/ReplyToListTest.php diff --git a/lib/mail/Mail.php b/lib/mail/Mail.php index 5653cf19..556249ff 100644 --- a/lib/mail/Mail.php +++ b/lib/mail/Mail.php @@ -986,40 +986,41 @@ public function getReplyTo() } /** - * Add the reply to email address to a Mail object - * - * @param $list array - * both of next formats are supported - * 'replytolist' => [ - * [ - * 'email' => 'email1@domain.com', - * 'name' => 'name one', - * ], [ - * 'email' => 'email2@domain.com', - * 'name' => 'name two', - * ], - * ], - * 'replytolist' => [ - * 'email1@domain.com', - * 'email2@domain.com', - * '', - * ], - */ - public function setReplyToList(array $list) - { - foreach ($list as $l) { - if ($l instanceof ReplyTo ) { - $this->reply_to_list[] = $l; - } else { - if (is_array($l)) { - if (!empty($l) && $l['email'] !== '') { - $this->reply_to_list[] = new ReplyTo($l['email'], $l['name']); - } - } else { - if ($l !=='') { - $this->reply_to_list[] = new ReplyTo($l); - } + * Set a list of ReplyTo email addresses. The following input formats are supported: + * + * 1. only email addresses + * [ + * 'email1@domain.com', + * 'email2@domain.com', + * ] + * 2. items with email address and name + * [ + * 'email' => 'email1@domain.com', + * 'name' => 'John Doe', + * ], [ + * 'email' => 'email2@domain.com', + * 'name' => '', + * ] + * + * @param array $replyToList + * @throws \InvalidArgumentException + */ + public function setReplyToList(array $replyToList) + { + Assert::minItems($replyToList, 'replyToList', 1); + Assert::maxItems($replyToList, 'replyToList', 1000); + + foreach ($replyToList as $replyTo) { + if ($replyTo instanceof ReplyTo ) { + $this->reply_to_list[] = $replyTo; + } elseif (is_array($replyTo)) { + if (! isset($replyTo['email'])) { + throw new \InvalidArgumentException('Email is mandatory on ReplyToList array.'); } + $replyTo['name'] = isset($replyTo['name']) ? $replyTo['name'] : null; + $this->reply_to_list[] = new ReplyTo($replyTo['email'], $replyTo['name']); + } else { + $this->reply_to_list[] = new ReplyTo($replyTo); } } } diff --git a/test/unit/ReplyToListTest.php b/test/unit/ReplyToListTest.php new file mode 100644 index 00000000..45d27cc7 --- /dev/null +++ b/test/unit/ReplyToListTest.php @@ -0,0 +1,79 @@ +setReplyToList($replyToList); + + $this->assertCount(2, $emailObject->reply_to_list); + $this->assertEquals('test1@example.com', $emailObject->reply_to_list[0]->getEmail()); + $this->assertEquals('Test1', $emailObject->reply_to_list[0]->getName()); + $this->assertEquals('test2@example.com', $emailObject->reply_to_list[1]->getEmail()); + $this->assertEquals('Test2', $emailObject->reply_to_list[1]->getName()); + } + + public function testSetReplyToListWithValidReplyToArray() + { + $emailObject = new Mail(); + $replyToList = [ + ['email' => 'test1@example.com', 'name' => 'Test1'], + ['email' => 'test2@example.com'] + ]; + + $emailObject->setReplyToList($replyToList); + + $this->assertCount(2, $emailObject->reply_to_list); + $this->assertEquals('test1@example.com', $emailObject->reply_to_list[0]->getEmail()); + $this->assertEquals('Test1', $emailObject->reply_to_list[0]->getName()); + $this->assertEquals('test2@example.com', $emailObject->reply_to_list[1]->getEmail()); + $this->assertNull($emailObject->reply_to_list[1]->getName()); + } + + public function testSetReplyToListWithInvalidReplyToArray() + { + $emailObject = new Mail(); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Email is mandatory on ReplyToList array.'); + + $replyToList = [ + ['name' => 'Test1'] + ]; + + $emailObject->setReplyToList($replyToList); + } + + public function testSetReplyToListWithSingleEmailString() + { + $emailObject = new Mail(); + $replyToList = [ + 'test1@example.com' + ]; + + $emailObject->setReplyToList($replyToList); + + $this->assertCount(1, $emailObject->reply_to_list); + $this->assertEquals('test1@example.com', $emailObject->reply_to_list[0]->getEmail()); + $this->assertNull($emailObject->reply_to_list[0]->getName()); + } + + public function testSetReplyToListWithMoreThanMaxItems() + { + $emailObject = new Mail(); + $replyToList = array_fill(0, 1001, 'test@example.com'); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Number of items in "replyToList" must be at most 1000.'); + + $emailObject->setReplyToList($replyToList); + } +} From d2097e8c0f63a2a8daeb7d454ae837c55fe3672a Mon Sep 17 00:00:00 2001 From: Florin Marina Date: Wed, 5 Jun 2024 21:41:54 +0300 Subject: [PATCH 4/5] Update method add Unit testing --- test/unit/ReplyToListTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/ReplyToListTest.php b/test/unit/ReplyToListTest.php index 45d27cc7..00bd6e2b 100644 --- a/test/unit/ReplyToListTest.php +++ b/test/unit/ReplyToListTest.php @@ -1,6 +1,7 @@ Date: Thu, 6 Jun 2024 15:20:56 +0300 Subject: [PATCH 5/5] updates test unit class --- test/unit/ReplyToListTest.php | 46 ++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/test/unit/ReplyToListTest.php b/test/unit/ReplyToListTest.php index 00bd6e2b..3e0b45f1 100644 --- a/test/unit/ReplyToListTest.php +++ b/test/unit/ReplyToListTest.php @@ -1,7 +1,10 @@ setReplyToList($replyToList); - $this->assertCount(2, $emailObject->reply_to_list); - $this->assertEquals('test1@example.com', $emailObject->reply_to_list[0]->getEmail()); - $this->assertEquals('Test1', $emailObject->reply_to_list[0]->getName()); - $this->assertEquals('test2@example.com', $emailObject->reply_to_list[1]->getEmail()); - $this->assertEquals('Test2', $emailObject->reply_to_list[1]->getName()); + $this->assertCount(2, $emailObject->getReplyToList()); + $this->assertEquals('test1@example.com', $emailObject->getReplyToList()[0]->getEmail()); + $this->assertEquals('Test1', $emailObject->getReplyToList()[0]->getName()); + $this->assertEquals('test2@example.com', $emailObject->getReplyToList()[1]->getEmail()); + $this->assertEquals('Test2', $emailObject->getReplyToList()[1]->getName()); } public function testSetReplyToListWithValidReplyToArray() @@ -27,16 +30,16 @@ public function testSetReplyToListWithValidReplyToArray() $emailObject = new Mail(); $replyToList = [ ['email' => 'test1@example.com', 'name' => 'Test1'], - ['email' => 'test2@example.com'] + ['email' => 'test2@example.com'], ]; $emailObject->setReplyToList($replyToList); - $this->assertCount(2, $emailObject->reply_to_list); - $this->assertEquals('test1@example.com', $emailObject->reply_to_list[0]->getEmail()); - $this->assertEquals('Test1', $emailObject->reply_to_list[0]->getName()); - $this->assertEquals('test2@example.com', $emailObject->reply_to_list[1]->getEmail()); - $this->assertNull($emailObject->reply_to_list[1]->getName()); + $this->assertCount(2, $emailObject->getReplyToList()); + $this->assertEquals('test1@example.com', $emailObject->getReplyToList()[0]->getEmail()); + $this->assertEquals('Test1', $emailObject->getReplyToList()[0]->getName()); + $this->assertEquals('test2@example.com', $emailObject->getReplyToList()[1]->getEmail()); + $this->assertNull($emailObject->getReplyToList()[1]->getName()); } public function testSetReplyToListWithInvalidReplyToArray() @@ -47,7 +50,7 @@ public function testSetReplyToListWithInvalidReplyToArray() $this->expectExceptionMessage('Email is mandatory on ReplyToList array.'); $replyToList = [ - ['name' => 'Test1'] + ['name' => 'Test1'], ]; $emailObject->setReplyToList($replyToList); @@ -57,24 +60,27 @@ public function testSetReplyToListWithSingleEmailString() { $emailObject = new Mail(); $replyToList = [ - 'test1@example.com' + 'test1@example.com', ]; $emailObject->setReplyToList($replyToList); - $this->assertCount(1, $emailObject->reply_to_list); - $this->assertEquals('test1@example.com', $emailObject->reply_to_list[0]->getEmail()); - $this->assertNull($emailObject->reply_to_list[0]->getName()); + $this->assertCount(1, $emailObject->getReplyToList()); + $this->assertEquals('test1@example.com', $emailObject->getReplyToList()[0]->getEmail()); + $this->assertNull($emailObject->getReplyToList()[0]->getName()); } public function testSetReplyToListWithMoreThanMaxItems() { - $emailObject = new Mail(); + $emailObject = new \SendGrid\Mail\Mail(); + // Create a replyToList with 1001 items $replyToList = array_fill(0, 1001, 'test@example.com'); - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Number of items in "replyToList" must be at most 1000.'); + // Update to expect the correct exception type + $this->expectException(\SendGrid\Mail\TypeException::class); + $this->expectExceptionMessage('Number of elements in "$replyToList" can not be more than 1000.'); + // Call the method that is expected to throw the exception $emailObject->setReplyToList($replyToList); } }