From af853234e4287fd218b422d795db2ce1e3b9ee8b Mon Sep 17 00:00:00 2001 From: Travis Swientek Date: Fri, 11 Oct 2013 13:36:35 -0700 Subject: [PATCH] Refactored counters, batch job trigger adjustment, all recipient types in variables. --- src/Mailgun/Messages/BatchMessage.php | 38 ++++++++---- src/Mailgun/Messages/MessageBuilder.php | 40 +++++++------ .../Tests/Messages/BatchMessageTest.php | 58 +++++++++++++++---- .../Tests/Messages/MessageBuilderTest.php | 46 ++++++++++++++- 4 files changed, 142 insertions(+), 40 deletions(-) diff --git a/src/Mailgun/Messages/BatchMessage.php b/src/Mailgun/Messages/BatchMessage.php index f1c8094..3152f27 100644 --- a/src/Mailgun/Messages/BatchMessage.php +++ b/src/Mailgun/Messages/BatchMessage.php @@ -26,18 +26,34 @@ class BatchMessage extends MessageBuilder{ $this->workingDomain = $workingDomain; $this->endpointUrl = $workingDomain . "/messages"; } - - public function addToRecipient($address, $variables = null){ - if($this->toRecipientCount == RECIPIENT_COUNT_LIMIT){ - if($this->autoSend == false){ - throw new TooManyParameters(TOO_MANY_RECIPIENTS); + + protected function addRecipient($headerName, $address, $variables){ + if(array_key_exists($headerName, $this->counters['recipients'])){ + if($this->counters['recipients'][$headerName] == RECIPIENT_COUNT_LIMIT){ + if($this->autoSend == false){ + throw new TooManyParameters(TOO_MANY_RECIPIENTS); + } + $this->sendMessage(); } - $this->sendMessage(); } - $this->addRecipient("to", $address, $variables); - if(!array_key_exists("id", $variables)){ - $variables['id'] = $this->toRecipientCount; + $compiledAddress = $this->parseAddress($address, $variables); + + if(isset($this->message[$headerName])){ + array_push($this->message[$headerName], $compiledAddress); + } + elseif($headerName == "h:reply-to"){ + $this->message[$headerName] = $compiledAddress; + } + else{ + $this->message[$headerName] = array($compiledAddress); + } + + if(array_key_exists($headerName, $this->counters['recipients'])){ + $this->counters['recipients'][$headerName] += 1; + if(!array_key_exists("id", $variables)){ + $variables['id'] = $this->counters['recipients'][$headerName]; + } } $this->batchRecipientAttributes["$address"] = $variables; } @@ -63,7 +79,9 @@ class BatchMessage extends MessageBuilder{ $message["recipient-variables"] = json_encode($this->batchRecipientAttributes); $response = $this->restClient->post($this->endpointUrl, $message, $files); $this->batchRecipientAttributes = array(); - $this->toRecipientCount = 0; + $this->counters['recipients']['to'] = 0; + $this->counters['recipients']['cc'] = 0; + $this->counters['recipients']['bcc'] = 0; unset($this->message["to"]); array_push($this->messageIds, $response->http_response_body->id); return $this->messageIds; diff --git a/src/Mailgun/Messages/MessageBuilder.php b/src/Mailgun/Messages/MessageBuilder.php index 8f96e2d..6fa5a0d 100644 --- a/src/Mailgun/Messages/MessageBuilder.php +++ b/src/Mailgun/Messages/MessageBuilder.php @@ -18,14 +18,13 @@ class MessageBuilder{ protected $message = array(); protected $variables = array(); protected $files = array(); - protected $sanitized; - protected $toRecipientCount = 0; - protected $ccRecipientCount = 0; - protected $bccRecipientCount = 0; - protected $attachmentCount = 0; - protected $campaignIdCount = 0; - protected $customOptionCount = 0; - protected $tagCount = 0; + protected $counters = array('recipients' => array('to' => 0, + 'cc' => 0, + 'bcc' => 0), + 'attributes' => array('attachment' => 0, + 'campaign_id' => 0, + 'custom_option' => 0, + 'tag' => 0)); protected function safeGet($params, $key, $default){ if(array_key_exists($key, $params)){ @@ -55,10 +54,6 @@ class MessageBuilder{ } protected function addRecipient($headerName, $address, $variables){ - if($headerName == "to" && $this->toRecipientCount > RECIPIENT_COUNT_LIMIT){ - throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT); - } - $compiledAddress = $this->parseAddress($address, $variables); if(isset($this->message[$headerName])){ @@ -70,22 +65,31 @@ class MessageBuilder{ else{ $this->message[$headerName] = array($compiledAddress); } - if($headerName == "to"){ - $this->toRecipientCount++; + if(array_key_exists($headerName, $this->counters['recipients'])){ + $this->counters['recipients'][$headerName] += 1; } } public function addToRecipient($address, $variables = null){ + if($this->counters['recipients']['to'] > RECIPIENT_COUNT_LIMIT){ + throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT); + } $this->addRecipient("to", $address, $variables); return end($this->message['to']); } public function addCcRecipient($address, $variables = null){ + if($this->counters['recipients']['cc'] > RECIPIENT_COUNT_LIMIT){ + throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT); + } $this->addRecipient("cc", $address, $variables); return end($this->message['cc']); } public function addBccRecipient($address, $variables = null){ + if($this->counters['recipients']['bcc'] > RECIPIENT_COUNT_LIMIT){ + throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT); + } $this->addRecipient("bcc", $address, $variables); return end($this->message['bcc']); } @@ -175,14 +179,14 @@ class MessageBuilder{ } public function addCampaignId($campaignId){ - if($this->campaignIdCount < CAMPAIGN_ID_LIMIT){ + if($this->counters['attributes']['campaign_id'] < CAMPAIGN_ID_LIMIT){ if(isset($this->message['o:campaign'])){ array_push($this->message['o:campaign'] , $campaignId); } else{ $this->message['o:campaign'] = array($campaignId); } - $this->campaignIdCount++; + $this->counters['attributes']['campaign_id'] += 1; return $this->message['o:campaign']; } else{ @@ -191,14 +195,14 @@ class MessageBuilder{ } public function addTag($tag){ - if($this->tagCount < TAG_LIMIT){ + if($this->counters['attributes']['tag'] < TAG_LIMIT){ if(isset($this->message['o:tag'])){ array_push($this->message['o:tag'] , $tag); } else{ $this->message['o:tag'] = array($tag); } - $this->tagCount++; + $this->counters['attributes']['tag'] += 1; return $this->message['o:tag']; } else{ diff --git a/tests/Mailgun/Tests/Messages/BatchMessageTest.php b/tests/Mailgun/Tests/Messages/BatchMessageTest.php index dea0af9..b67a45a 100644 --- a/tests/Mailgun/Tests/Messages/BatchMessageTest.php +++ b/tests/Mailgun/Tests/Messages/BatchMessageTest.php @@ -15,11 +15,56 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase{ $message = $this->client->BatchMessage($this->sampleDomain); $this->assertTrue(is_array($message->getMessage())); } - public function testaddToRecipient(){ + public function testAddRecipient(){ $message = $this->client->BatchMessage($this->sampleDomain); $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); $messageObj= $message->getMessage(); $this->assertEquals(array("to" => array("'Test User' ")), $messageObj); + + $reflectionClass = new \ReflectionClass(get_class($message)); + $property = $reflectionClass->getProperty('counters'); + $property->setAccessible(true); + $array = $property->getValue($message); + $this->assertEquals(1, $array['recipients']['to']); + } + public function testRecipientVariablesOnTo(){ + $message = $this->client->BatchMessage($this->sampleDomain); + $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $messageObj= $message->getMessage(); + $this->assertEquals(array("to" => array("'Test User' ")), $messageObj); + + $reflectionClass = new \ReflectionClass(get_class($message)); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property->setAccessible(true); + $propertyValue = $property->getValue($message); + $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); + $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); + } + public function testRecipientVariablesOnCc(){ + $message = $this->client->BatchMessage($this->sampleDomain); + $message->addCcRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $messageObj= $message->getMessage(); + $this->assertEquals(array("cc" => array("'Test User' ")), $messageObj); + + $reflectionClass = new \ReflectionClass(get_class($message)); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property->setAccessible(true); + $propertyValue = $property->getValue($message); + $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); + $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); + } + public function testRecipientVariablesOnBcc(){ + $message = $this->client->BatchMessage($this->sampleDomain); + $message->addBccRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $messageObj= $message->getMessage(); + $this->assertEquals(array("bcc" => array("'Test User' ")), $messageObj); + + $reflectionClass = new \ReflectionClass(get_class($message)); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property->setAccessible(true); + $propertyValue = $property->getValue($message); + $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); + $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); } public function testAddMultipleBatchRecipients(){ $message = $this->client->BatchMessage($this->sampleDomain); @@ -40,7 +85,7 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase{ $messageObj= $message->getMessage(); $this->assertEquals(1, count($messageObj["to"])); } - public function testResetOnEndBatchMessage(){ + public function testAttributeResetOnEndBatchMessage(){ $message = $this->client->BatchMessage($this->sampleDomain); $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); $message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User")); @@ -50,15 +95,6 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase{ $messageObj= $message->getMessage(); $this->assertTrue(true, empty($messageObj)); } - public function testToRecipientCount() { - $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); - - $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('toRecipientCount'); - $property->setAccessible(true); - $this->assertEquals(1, $property->getValue($message)); - } public function testDefaultIDInVariables() { $message = $this->client->BatchMessage($this->sampleDomain); $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); diff --git a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php index c73d4a6..2c593ee 100644 --- a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php +++ b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php @@ -13,7 +13,21 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase{ $message = $this->client->MessageBuilder(); $this->assertTrue(is_array($message->getMessage())); } - + public function testCountersSetToZero(){ + $message = $this->client->MessageBuilder(); + + $reflectionClass = new \ReflectionClass(get_class($message)); + $property = $reflectionClass->getProperty('counters'); + $property->setAccessible(True); + $propertyValue = $property->getValue($message); + $this->assertEquals(0, $propertyValue['recipients']['to']); + $this->assertEquals(0, $propertyValue['recipients']['cc']); + $this->assertEquals(0, $propertyValue['recipients']['bcc']); + $this->assertEquals(0, $propertyValue['attributes']['attachment']); + $this->assertEquals(0, $propertyValue['attributes']['campaign_id']); + $this->assertEquals(0, $propertyValue['attributes']['custom_option']); + $this->assertEquals(0, $propertyValue['attributes']['tag']); + } public function testAddToRecipient(){ $message = $this->client->MessageBuilder(); $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); @@ -32,6 +46,36 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase{ $messageObj = $message->getMessage(); $this->assertEquals(array("bcc" => array("'Test User' ")), $messageObj); } + public function testToRecipientCount() { + $message = $this->client->MessageBuilder(); + $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + + $reflectionClass = new \ReflectionClass(get_class($message)); + $property = $reflectionClass->getProperty('counters'); + $property->setAccessible(true); + $array = $property->getValue($message); + $this->assertEquals(1, $array['recipients']['to']); + } + public function testCcRecipientCount() { + $message = $this->client->MessageBuilder(); + $message->addCcRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + + $reflectionClass = new \ReflectionClass(get_class($message)); + $property = $reflectionClass->getProperty('counters'); + $property->setAccessible(true); + $array = $property->getValue($message); + $this->assertEquals(1, $array['recipients']['cc']); + } + public function testBccRecipientCount() { + $message = $this->client->MessageBuilder(); + $message->addBccRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + + $reflectionClass = new \ReflectionClass(get_class($message)); + $property = $reflectionClass->getProperty('counters'); + $property->setAccessible(true); + $array = $property->getValue($message); + $this->assertEquals(1, $array['recipients']['bcc']); + } public function testSetFromAddress(){ $message = $this->client->MessageBuilder(); $message->setFromAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));