2013-07-23 09:11:01 +04:00
|
|
|
<?PHP
|
2013-08-13 23:26:34 +04:00
|
|
|
namespace Mailgun\Tests\Messages;
|
2013-07-23 09:11:01 +04:00
|
|
|
|
2014-05-13 17:25:26 +04:00
|
|
|
use Mailgun\Tests\Mock\Mailgun;
|
2013-07-23 09:11:01 +04:00
|
|
|
|
2013-07-25 03:25:24 +04:00
|
|
|
class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase{
|
|
|
|
private $client;
|
2013-07-24 01:29:45 +04:00
|
|
|
|
2013-07-25 03:25:24 +04:00
|
|
|
public function setUp(){
|
2014-05-13 17:25:26 +04:00
|
|
|
$this->client = new Mailgun("My-Super-Awesome-API-Key", "samples.mailgun.org", false);
|
2013-07-23 09:11:01 +04:00
|
|
|
}
|
|
|
|
public function testBlankInstantiation(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-23 09:11:01 +04:00
|
|
|
$this->assertTrue(is_array($message->getMessage()));
|
|
|
|
}
|
2013-10-12 00:36:35 +04:00
|
|
|
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']);
|
|
|
|
}
|
2013-07-24 01:29:45 +04:00
|
|
|
public function testAddToRecipient(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
|
|
|
|
$messageObj = $message->getMessage();
|
2013-08-13 23:26:34 +04:00
|
|
|
$this->assertEquals(array("to" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
|
2013-07-24 01:29:45 +04:00
|
|
|
}
|
|
|
|
public function testAddCcRecipient(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->addCcRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
|
|
|
|
$messageObj = $message->getMessage();
|
2013-08-13 23:26:34 +04:00
|
|
|
$this->assertEquals(array("cc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
|
2013-07-24 01:29:45 +04:00
|
|
|
}
|
|
|
|
public function testAddBccRecipient(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->addBccRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
|
|
|
|
$messageObj = $message->getMessage();
|
2013-08-13 23:26:34 +04:00
|
|
|
$this->assertEquals(array("bcc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
|
2013-07-24 01:29:45 +04:00
|
|
|
}
|
2013-10-12 00:36:35 +04:00
|
|
|
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']);
|
|
|
|
}
|
2013-07-24 01:29:45 +04:00
|
|
|
public function testSetFromAddress(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->setFromAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
|
|
|
|
$messageObj = $message->getMessage();
|
2013-08-13 23:26:34 +04:00
|
|
|
$this->assertEquals(array("from" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
|
2013-07-24 01:29:45 +04:00
|
|
|
}
|
2013-09-13 01:19:23 +04:00
|
|
|
public function testSetReplyTo(){
|
|
|
|
$message = $this->client->MessageBuilder();
|
|
|
|
$message->setReplyToAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("h:reply-to" => "'Test User' <test@samples.mailgun.org>"), $messageObj);
|
|
|
|
}
|
2013-07-24 01:29:45 +04:00
|
|
|
public function testSetSubject(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->setSubject("Test Subject");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("subject" => "Test Subject"), $messageObj);
|
|
|
|
}
|
|
|
|
public function testAddCustomHeader(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->addCustomHeader("My-Header", "123");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("h:My-Header" => array("123")), $messageObj);
|
|
|
|
}
|
|
|
|
public function testSetTextBody(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->setTextBody("This is the text body!");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("text" => "This is the text body!"), $messageObj);
|
|
|
|
}
|
|
|
|
public function testSetHtmlBody(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->setHtmlBody("<html><body>This is an awesome email</body></html>");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("html" => "<html><body>This is an awesome email</body></html>"), $messageObj);
|
|
|
|
}
|
|
|
|
public function testAddAttachments(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->addAttachment("@../TestAssets/mailgun_icon.png");
|
|
|
|
$message->addAttachment("@../TestAssets/rackspace_logo.png");
|
2013-07-25 03:25:24 +04:00
|
|
|
$messageObj = $message->getFiles();
|
2014-01-14 02:53:34 +04:00
|
|
|
$this->assertEquals(array(array('filePath' => "@../TestAssets/mailgun_icon.png",
|
|
|
|
'remoteName' => null),
|
|
|
|
array('filePath' => "@../TestAssets/rackspace_logo.png",
|
|
|
|
'remoteName' => null)), $messageObj["attachment"]);
|
2013-07-24 01:29:45 +04:00
|
|
|
}
|
|
|
|
public function testAddInlineImages(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->addInlineImage("@../TestAssets/mailgun_icon.png");
|
|
|
|
$message->addInlineImage("@../TestAssets/rackspace_logo.png");
|
2013-07-25 03:25:24 +04:00
|
|
|
$messageObj = $message->getFiles();
|
2014-01-14 02:53:34 +04:00
|
|
|
$this->assertEquals(array(array('filePath' => "@../TestAssets/mailgun_icon.png",
|
|
|
|
'remoteName' => null),
|
|
|
|
array('filePath' => "@../TestAssets/rackspace_logo.png",
|
|
|
|
'remoteName' => null)), $messageObj['inline']);
|
|
|
|
}
|
|
|
|
public function testAddAttachmentsPostName(){
|
|
|
|
$message = $this->client->MessageBuilder();
|
|
|
|
$message->addAttachment('@../TestAssets/mailgun_icon.png', 'mg_icon.png');
|
|
|
|
$message->addAttachment('@../TestAssets/rackspace_logo.png', 'rs_logo.png');
|
|
|
|
$messageObj = $message->getFiles();
|
|
|
|
$this->assertEquals(array(array('filePath' => '@../TestAssets/mailgun_icon.png',
|
|
|
|
'remoteName' => 'mg_icon.png'),
|
|
|
|
array('filePath' => '@../TestAssets/rackspace_logo.png',
|
|
|
|
'remoteName' => 'rs_logo.png')), $messageObj["attachment"]);
|
|
|
|
}
|
|
|
|
public function testAddInlineImagePostName(){
|
|
|
|
$message = $this->client->MessageBuilder();
|
|
|
|
$message->addInlineImage('@../TestAssets/mailgun_icon.png', 'mg_icon.png');
|
|
|
|
$message->addInlineImage('@../TestAssets/rackspace_logo.png', 'rs_logo.png');
|
|
|
|
$messageObj = $message->getFiles();
|
|
|
|
$this->assertEquals(array(array('filePath' => '@../TestAssets/mailgun_icon.png',
|
|
|
|
'remoteName' => 'mg_icon.png'),
|
|
|
|
array('filePath' => '@../TestAssets/rackspace_logo.png',
|
|
|
|
'remoteName' => 'rs_logo.png')), $messageObj['inline']);
|
2013-07-24 01:29:45 +04:00
|
|
|
}
|
|
|
|
public function testsetTestMode(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->setTestMode(true);
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:testmode" => "yes"), $messageObj);
|
|
|
|
$message->setTestMode(false);
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:testmode" => "no"), $messageObj);
|
|
|
|
$message->setTestMode("yes");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:testmode" => "yes"), $messageObj);
|
|
|
|
$message->setTestMode("no");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:testmode" => "no"), $messageObj);
|
|
|
|
}
|
|
|
|
public function addCampaignId(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->addCampaignId("ABC123");
|
|
|
|
$message->addCampaignId("XYZ987");
|
|
|
|
$message->addCampaignId("TUV456");
|
|
|
|
$message->addCampaignId("NONO123");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:campaign" => array("ABC123", "XYZ987", "TUV456")), $messageObj);
|
|
|
|
}
|
|
|
|
public function testSetDkim(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->setDkim(true);
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:dkim" => "yes"), $messageObj);
|
|
|
|
$message->setDkim(false);
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:dkim" => "no"), $messageObj);
|
|
|
|
$message->setDkim("yes");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:dkim" => "yes"), $messageObj);
|
|
|
|
$message->setDkim("no");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:dkim" => "no"), $messageObj);
|
|
|
|
}
|
|
|
|
public function testSetClickTracking(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->setClickTracking(true);
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:tracking-clicks" => "yes"), $messageObj);
|
|
|
|
$message->setClickTracking(false);
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:tracking-clicks" => "no"), $messageObj);
|
|
|
|
$message->setClickTracking("yes");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:tracking-clicks" => "yes"), $messageObj);
|
|
|
|
$message->setClickTracking("no");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:tracking-clicks" => "no"), $messageObj);
|
|
|
|
}
|
|
|
|
public function testSetOpenTracking(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->setOpenTracking(true);
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:tracking-opens" => "yes"), $messageObj);
|
|
|
|
$message->setOpenTracking(false);
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:tracking-opens" => "no"), $messageObj);
|
|
|
|
$message->setOpenTracking("yes");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:tracking-opens" => "yes"), $messageObj);
|
|
|
|
$message->setOpenTracking("no");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:tracking-opens" => "no"), $messageObj);
|
|
|
|
}
|
|
|
|
public function testSetDeliveryTime(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->setDeliveryTime("January 15, 2014 8:00AM", "CST");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 -0600"), $messageObj);
|
|
|
|
$message->setDeliveryTime("January 15, 2014 8:00AM", "UTC");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 +0000"), $messageObj);
|
2013-09-04 20:50:50 +04:00
|
|
|
$message->setDeliveryTime("January 15, 2014 8:00AM");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 +0000"), $messageObj);
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->setDeliveryTime("1/15/2014 13:50:01", "CDT");
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 13:50:01 -0600"), $messageObj);
|
2014-05-13 20:05:32 +04:00
|
|
|
// https://github.com/mailgun/mailgun-php/pull/42
|
|
|
|
// https://github.com/mailgun/mailgun-php/issues/43
|
|
|
|
//$message->setDeliveryTime("first saturday of July 2013 8:00AM", "CDT");
|
|
|
|
//$messageObj = $message->getMessage();
|
|
|
|
//$this->assertEquals(array("o:deliverytime" => "Sat, 06 Jul 2013 08:00:00 -0500"), $messageObj);
|
2013-07-24 01:29:45 +04:00
|
|
|
}
|
|
|
|
public function testAddCustomData(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-07-24 01:29:45 +04:00
|
|
|
$message->addCustomData("My-Super-Awesome-Data", array("What" => "Mailgun Rocks!"));
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(array("v:My-Super-Awesome-Data" => "{\"What\":\"Mailgun Rocks!\"}"), $messageObj);
|
|
|
|
}
|
2013-08-13 23:26:34 +04:00
|
|
|
public function testAddCustomParameter(){
|
2013-08-08 21:39:44 +04:00
|
|
|
$message = $this->client->MessageBuilder();
|
2013-08-13 23:26:34 +04:00
|
|
|
$message->addCustomParameter("my-option", "yes");
|
|
|
|
$message->addCustomParameter("o:my-other-option", "no");
|
2013-07-24 01:29:45 +04:00
|
|
|
$messageObj = $message->getMessage();
|
2013-08-13 23:26:34 +04:00
|
|
|
$this->assertEquals(array("my-option" => array("yes"), "o:my-other-option" => array("no")), $messageObj);
|
2013-07-24 01:29:45 +04:00
|
|
|
}
|
2013-07-23 09:11:01 +04:00
|
|
|
}
|
|
|
|
|
2013-07-24 04:34:50 +04:00
|
|
|
?>
|