mailgun-php/tests/Messages/MessageBuilderTest.php

375 lines
14 KiB
PHP
Raw Normal View History

2013-07-23 09:11:01 +04:00
<?PHP
2016-07-24 14:42:47 +03:00
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
2016-12-06 21:12:52 +03:00
* of the MIT license. See the LICENSE file for details.
*/
2013-08-13 23:26:34 +04:00
namespace Mailgun\Tests\Messages;
2013-07-23 09:11:01 +04:00
use Mailgun\Tests\Mock\Mailgun;
2013-07-23 09:11:01 +04:00
2014-05-14 17:06:05 +04:00
class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
{
private $client;
public function setUp()
{
$this->client = new Mailgun();
2014-05-14 17:06:05 +04:00
}
public function testBlankInstantiation()
{
$message = $this->client->MessageBuilder();
$this->assertTrue(is_array($message->getMessage()));
}
public function testCountersSetToZero()
{
$message = $this->client->MessageBuilder();
$reflectionClass = new \ReflectionClass(get_class($message));
2016-07-24 14:42:47 +03:00
$property = $reflectionClass->getProperty('counters');
2014-05-14 17:06:05 +04:00
$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']);
2014-05-14 17:06:05 +04:00
$this->assertEquals(0, $propertyValue['attributes']['tag']);
}
public function testAddToRecipient()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addToRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
$this->assertEquals(['to' => ['"Test User" <test@samples.mailgun.org>']], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testAddCcRecipient()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addCcRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
$this->assertEquals(['cc' => ['"Test User" <test@samples.mailgun.org>']], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testAddBccRecipient()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addBccRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
$this->assertEquals(['bcc' => ['"Test User" <test@samples.mailgun.org>']], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testToRecipientCount()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
2014-05-14 17:06:05 +04:00
$reflectionClass = new \ReflectionClass(get_class($message));
2016-07-24 14:42:47 +03:00
$property = $reflectionClass->getProperty('counters');
$property->setAccessible(true);
$array = $property->getValue($message);
$this->assertEquals(1, $array['recipients']['to']);
}
2014-05-14 17:06:05 +04:00
public function testCcRecipientCount()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addCcRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
2014-05-14 17:06:05 +04:00
$reflectionClass = new \ReflectionClass(get_class($message));
2016-07-24 14:42:47 +03:00
$property = $reflectionClass->getProperty('counters');
$property->setAccessible(true);
$array = $property->getValue($message);
$this->assertEquals(1, $array['recipients']['cc']);
}
2014-05-14 17:06:05 +04:00
public function testBccRecipientCount()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addBccRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
2014-05-14 17:06:05 +04:00
$reflectionClass = new \ReflectionClass(get_class($message));
2016-07-24 14:42:47 +03:00
$property = $reflectionClass->getProperty('counters');
$property->setAccessible(true);
$array = $property->getValue($message);
$this->assertEquals(1, $array['recipients']['bcc']);
}
2014-05-14 17:06:05 +04:00
public function testSetFromAddress()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->setFromAddress('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
$this->assertEquals(['from' => ['"Test User" <test@samples.mailgun.org>']], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testSetReplyTo()
{
$message = $this->client->MessageBuilder();
$message->setReplyToAddress('overwritten@samples.mailgun.org');
2016-07-24 14:42:47 +03:00
$message->setReplyToAddress('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
$this->assertEquals(['h:reply-to' => '"Test User" <test@samples.mailgun.org>'], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testSetSubject()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->setSubject('Test Subject');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['subject' => 'Test Subject'], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testAddCustomHeader()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addCustomHeader('My-Header', '123');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
$this->assertEquals(['h:My-Header' => '123'], $messageObj);
}
public function testAddMultipleCustomHeader()
{
$message = $this->client->MessageBuilder();
$message->addCustomHeader('My-Header', '123');
$message->addCustomHeader('My-Header', '456');
$messageObj = $message->getMessage();
$this->assertEquals(['h:My-Header' => ['123', '456']], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testSetTextBody()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->setTextBody('This is the text body!');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['text' => 'This is the text body!'], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testSetHtmlBody()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->setHtmlBody('<html><body>This is an awesome email</body></html>');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['html' => '<html><body>This is an awesome email</body></html>'], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testAddAttachments()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addAttachment('@../TestAssets/mailgun_icon.png');
$message->addAttachment('@../TestAssets/rackspace_logo.png');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getFiles();
$this->assertEquals(
2016-07-24 14:42:47 +03:00
[
[
'filePath' => '@../TestAssets/mailgun_icon.png',
2016-07-24 14:42:47 +03:00
'remoteName' => null,
],
[
'filePath' => '@../TestAssets/rackspace_logo.png',
2016-07-24 14:42:47 +03:00
'remoteName' => null,
],
],
$messageObj['attachment']
2014-05-14 17:06:05 +04:00
);
}
public function testAddInlineImages()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addInlineImage('@../TestAssets/mailgun_icon.png');
$message->addInlineImage('@../TestAssets/rackspace_logo.png');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getFiles();
$this->assertEquals(
2016-07-24 14:42:47 +03:00
[
[
'filePath' => '@../TestAssets/mailgun_icon.png',
2016-07-24 14:42:47 +03:00
'remoteName' => null,
],
[
'filePath' => '@../TestAssets/rackspace_logo.png',
2016-07-24 14:42:47 +03:00
'remoteName' => null,
],
],
2014-05-14 17:06:05 +04:00
$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(
2016-07-24 14:42:47 +03:00
[
[
'filePath' => '@../TestAssets/mailgun_icon.png',
2016-07-24 14:42:47 +03:00
'remoteName' => 'mg_icon.png',
],
[
'filePath' => '@../TestAssets/rackspace_logo.png',
2016-07-24 14:42:47 +03:00
'remoteName' => 'rs_logo.png',
],
],
$messageObj['attachment']
2014-05-14 17:06:05 +04:00
);
}
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(
2016-07-24 14:42:47 +03:00
[
[
'filePath' => '@../TestAssets/mailgun_icon.png',
2016-07-24 14:42:47 +03:00
'remoteName' => 'mg_icon.png',
],
[
'filePath' => '@../TestAssets/rackspace_logo.png',
2016-07-24 14:42:47 +03:00
'remoteName' => 'rs_logo.png',
],
],
2014-05-14 17:06:05 +04:00
$messageObj['inline']
);
}
public function testsetTestMode()
{
$message = $this->client->MessageBuilder();
$message->setTestMode(true);
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:testmode' => 'yes'], $messageObj);
2014-05-14 17:06:05 +04:00
$message->setTestMode(false);
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:testmode' => 'no'], $messageObj);
$message->setTestMode('yes');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:testmode' => 'yes'], $messageObj);
$message->setTestMode('no');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:testmode' => 'no'], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function addCampaignId()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addCampaignId('ABC123');
$message->addCampaignId('XYZ987');
$message->addCampaignId('TUV456');
$message->addCampaignId('NONO123');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:campaign' => ['ABC123', 'XYZ987', 'TUV456']], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testSetDkim()
{
$message = $this->client->MessageBuilder();
$message->setDkim(true);
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:dkim' => 'yes'], $messageObj);
2014-05-14 17:06:05 +04:00
$message->setDkim(false);
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:dkim' => 'no'], $messageObj);
$message->setDkim('yes');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:dkim' => 'yes'], $messageObj);
$message->setDkim('no');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:dkim' => 'no'], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testSetClickTracking()
{
$message = $this->client->MessageBuilder();
$message->setClickTracking(true);
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:tracking-clicks' => 'yes'], $messageObj);
2014-05-14 17:06:05 +04:00
$message->setClickTracking(false);
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:tracking-clicks' => 'no'], $messageObj);
$message->setClickTracking('yes');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:tracking-clicks' => 'yes'], $messageObj);
$message->setClickTracking('no');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:tracking-clicks' => 'no'], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testSetOpenTracking()
{
$message = $this->client->MessageBuilder();
$message->setOpenTracking(true);
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:tracking-opens' => 'yes'], $messageObj);
2014-05-14 17:06:05 +04:00
$message->setOpenTracking(false);
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:tracking-opens' => 'no'], $messageObj);
$message->setOpenTracking('yes');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:tracking-opens' => 'yes'], $messageObj);
$message->setOpenTracking('no');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:tracking-opens' => 'no'], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testSetDeliveryTime()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->setDeliveryTime('January 15, 2014 8:00AM', 'CST');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 -0600'], $messageObj);
$message->setDeliveryTime('January 15, 2014 8:00AM', 'UTC');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 +0000'], $messageObj);
$message->setDeliveryTime('January 15, 2014 8:00AM');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 +0000'], $messageObj);
$message->setDeliveryTime('1/15/2014 13:50:01', 'CDT');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['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
2014-05-14 17:06:05 +04:00
//$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);
}
public function testAddCustomData()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addCustomData('My-Super-Awesome-Data', ['What' => 'Mailgun Rocks!']);
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['v:My-Super-Awesome-Data' => '{"What":"Mailgun Rocks!"}'], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testAddCustomParameter()
{
$message = $this->client->MessageBuilder();
2016-07-24 14:42:47 +03:00
$message->addCustomParameter('my-option', 'yes');
$message->addCustomParameter('o:my-other-option', 'no');
2014-05-14 17:06:05 +04:00
$messageObj = $message->getMessage();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['my-option' => ['yes'], 'o:my-other-option' => ['no']], $messageObj);
2014-05-14 17:06:05 +04:00
}
public function testSetMessage()
{
2016-07-24 14:42:47 +03:00
$message = [1, 2, 3, 4, 5];
2014-05-14 17:03:12 +04:00
$messageBuilder = $this->client->MessageBuilder();
$messageBuilder->setMessage($message);
$this->assertEquals($message, $messageBuilder->getMessage());
}
2013-07-23 09:11:01 +04:00
}