mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-25 22:36:06 +03:00
Merge pull request #18 from travelton/bugfix/templatevariables
Refactored counters, batch job trigger adjustment, all recipient types i...
This commit is contained in:
commit
7323f28f3a
@ -27,17 +27,33 @@ class BatchMessage extends MessageBuilder{
|
||||
$this->endpointUrl = $workingDomain . "/messages";
|
||||
}
|
||||
|
||||
public function addToRecipient($address, $variables = null){
|
||||
if($this->toRecipientCount == RECIPIENT_COUNT_LIMIT){
|
||||
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->addRecipient("to", $address, $variables);
|
||||
$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->toRecipientCount;
|
||||
$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;
|
||||
|
@ -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{
|
||||
|
@ -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' <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(){
|
||||
$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"));
|
||||
|
@ -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' <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(){
|
||||
$message = $this->client->MessageBuilder();
|
||||
$message->setFromAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
|
||||
|
Loading…
Reference in New Issue
Block a user