Merge pull request #18 from travelton/bugfix/templatevariables

Refactored counters, batch job trigger adjustment, all recipient types i...
This commit is contained in:
Travis Swientek 2013-10-11 13:48:38 -07:00
commit 7323f28f3a
4 changed files with 142 additions and 40 deletions

View File

@ -27,17 +27,33 @@ class BatchMessage extends MessageBuilder{
$this->endpointUrl = $workingDomain . "/messages"; $this->endpointUrl = $workingDomain . "/messages";
} }
public function addToRecipient($address, $variables = null){ protected function addRecipient($headerName, $address, $variables){
if($this->toRecipientCount == RECIPIENT_COUNT_LIMIT){ if(array_key_exists($headerName, $this->counters['recipients'])){
if($this->autoSend == false){ if($this->counters['recipients'][$headerName] == RECIPIENT_COUNT_LIMIT){
throw new TooManyParameters(TOO_MANY_RECIPIENTS); if($this->autoSend == false){
throw new TooManyParameters(TOO_MANY_RECIPIENTS);
}
$this->sendMessage();
} }
$this->sendMessage();
} }
$this->addRecipient("to", $address, $variables); $compiledAddress = $this->parseAddress($address, $variables);
if(!array_key_exists("id", $variables)){
$variables['id'] = $this->toRecipientCount; 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; $this->batchRecipientAttributes["$address"] = $variables;
} }
@ -63,7 +79,9 @@ class BatchMessage extends MessageBuilder{
$message["recipient-variables"] = json_encode($this->batchRecipientAttributes); $message["recipient-variables"] = json_encode($this->batchRecipientAttributes);
$response = $this->restClient->post($this->endpointUrl, $message, $files); $response = $this->restClient->post($this->endpointUrl, $message, $files);
$this->batchRecipientAttributes = array(); $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"]); unset($this->message["to"]);
array_push($this->messageIds, $response->http_response_body->id); array_push($this->messageIds, $response->http_response_body->id);
return $this->messageIds; return $this->messageIds;

View File

@ -18,14 +18,13 @@ class MessageBuilder{
protected $message = array(); protected $message = array();
protected $variables = array(); protected $variables = array();
protected $files = array(); protected $files = array();
protected $sanitized; protected $counters = array('recipients' => array('to' => 0,
protected $toRecipientCount = 0; 'cc' => 0,
protected $ccRecipientCount = 0; 'bcc' => 0),
protected $bccRecipientCount = 0; 'attributes' => array('attachment' => 0,
protected $attachmentCount = 0; 'campaign_id' => 0,
protected $campaignIdCount = 0; 'custom_option' => 0,
protected $customOptionCount = 0; 'tag' => 0));
protected $tagCount = 0;
protected function safeGet($params, $key, $default){ protected function safeGet($params, $key, $default){
if(array_key_exists($key, $params)){ if(array_key_exists($key, $params)){
@ -55,10 +54,6 @@ class MessageBuilder{
} }
protected function addRecipient($headerName, $address, $variables){ 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); $compiledAddress = $this->parseAddress($address, $variables);
if(isset($this->message[$headerName])){ if(isset($this->message[$headerName])){
@ -70,22 +65,31 @@ class MessageBuilder{
else{ else{
$this->message[$headerName] = array($compiledAddress); $this->message[$headerName] = array($compiledAddress);
} }
if($headerName == "to"){ if(array_key_exists($headerName, $this->counters['recipients'])){
$this->toRecipientCount++; $this->counters['recipients'][$headerName] += 1;
} }
} }
public function addToRecipient($address, $variables = null){ 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); $this->addRecipient("to", $address, $variables);
return end($this->message['to']); return end($this->message['to']);
} }
public function addCcRecipient($address, $variables = null){ 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); $this->addRecipient("cc", $address, $variables);
return end($this->message['cc']); return end($this->message['cc']);
} }
public function addBccRecipient($address, $variables = null){ 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); $this->addRecipient("bcc", $address, $variables);
return end($this->message['bcc']); return end($this->message['bcc']);
} }
@ -175,14 +179,14 @@ class MessageBuilder{
} }
public function addCampaignId($campaignId){ 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'])){ if(isset($this->message['o:campaign'])){
array_push($this->message['o:campaign'] , $campaignId); array_push($this->message['o:campaign'] , $campaignId);
} }
else{ else{
$this->message['o:campaign'] = array($campaignId); $this->message['o:campaign'] = array($campaignId);
} }
$this->campaignIdCount++; $this->counters['attributes']['campaign_id'] += 1;
return $this->message['o:campaign']; return $this->message['o:campaign'];
} }
else{ else{
@ -191,14 +195,14 @@ class MessageBuilder{
} }
public function addTag($tag){ public function addTag($tag){
if($this->tagCount < TAG_LIMIT){ if($this->counters['attributes']['tag'] < TAG_LIMIT){
if(isset($this->message['o:tag'])){ if(isset($this->message['o:tag'])){
array_push($this->message['o:tag'] , $tag); array_push($this->message['o:tag'] , $tag);
} }
else{ else{
$this->message['o:tag'] = array($tag); $this->message['o:tag'] = array($tag);
} }
$this->tagCount++; $this->counters['attributes']['tag'] += 1;
return $this->message['o:tag']; return $this->message['o:tag'];
} }
else{ else{

View File

@ -15,11 +15,56 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase{
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
$this->assertTrue(is_array($message->getMessage())); $this->assertTrue(is_array($message->getMessage()));
} }
public function testaddToRecipient(){ public function testAddRecipient(){
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
$message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
$messageObj= $message->getMessage(); $messageObj= $message->getMessage();
$this->assertEquals(array("to" => array("'Test User' <test@samples.mailgun.org>")), $messageObj); $this->assertEquals(array("to" => array("'Test User' <test@samples.mailgun.org>")), $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' <test@samples.mailgun.org>")), $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' <test@samples.mailgun.org>")), $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' <test@samples.mailgun.org>")), $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(){ public function testAddMultipleBatchRecipients(){
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
@ -40,7 +85,7 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase{
$messageObj= $message->getMessage(); $messageObj= $message->getMessage();
$this->assertEquals(1, count($messageObj["to"])); $this->assertEquals(1, count($messageObj["to"]));
} }
public function testResetOnEndBatchMessage(){ public function testAttributeResetOnEndBatchMessage(){
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
$message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User"));
$message->setFromAddress("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(); $messageObj= $message->getMessage();
$this->assertTrue(true, empty($messageObj)); $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() { public function testDefaultIDInVariables() {
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
$message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User"));

View File

@ -13,7 +13,21 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase{
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$this->assertTrue(is_array($message->getMessage())); $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(){ public function testAddToRecipient(){
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
@ -32,6 +46,36 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase{
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("bcc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj); $this->assertEquals(array("bcc" => array("'Test User' <test@samples.mailgun.org>")), $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(){ public function testSetFromAddress(){
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->setFromAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); $message->setFromAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));