2013-07-24 01:29:45 +04:00
|
|
|
<?PHP
|
2013-08-13 23:26:34 +04:00
|
|
|
namespace Mailgun\Tests\Messages;
|
2013-07-24 01:29:45 +04:00
|
|
|
|
2014-05-13 17:25:26 +04:00
|
|
|
use Mailgun\Tests\Mock\Mailgun;
|
2013-07-24 01:29:45 +04:00
|
|
|
|
2014-05-14 17:06:05 +04:00
|
|
|
class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
private $client;
|
|
|
|
private $sampleDomain = "samples.mailgun.org";
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->client = new Mailgun("My-Super-Awesome-API-Key");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBlankInstantiation()
|
|
|
|
{
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
|
|
|
$this->assertTrue(is_array($message->getMessage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2013-10-12 00:36:35 +04:00
|
|
|
$property->setAccessible(true);
|
|
|
|
$array = $property->getValue($message);
|
|
|
|
$this->assertEquals(1, $array['recipients']['to']);
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2013-10-12 00:36:35 +04:00
|
|
|
$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']);
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2013-10-12 00:36:35 +04:00
|
|
|
$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']);
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2013-10-12 00:36:35 +04:00
|
|
|
$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']);
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddMultipleBatchRecipients()
|
|
|
|
{
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
|
|
|
for ($i = 0; $i < 100; $i++) {
|
|
|
|
$message->addToRecipient("$i@samples.mailgun.org", array("first" => "Test", "last" => "User $i"));
|
|
|
|
}
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(100, count($messageObj["to"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMaximumBatchSize()
|
|
|
|
{
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
|
|
|
$message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User"));
|
|
|
|
$message->setSubject("This is the subject of the message!");
|
|
|
|
$message->setTextBody("This is the text body of the message!");
|
|
|
|
for ($i = 0; $i < 1001; $i++) {
|
|
|
|
$message->addToRecipient("$i@samples.mailgun.org", array("first" => "Test", "last" => "User $i"));
|
|
|
|
}
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertEquals(1, count($messageObj["to"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAttributeResetOnEndBatchMessage()
|
|
|
|
{
|
2013-10-08 00:53:23 +04:00
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
|
|
|
$message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User"));
|
2014-05-14 17:06:05 +04:00
|
|
|
$message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User"));
|
|
|
|
$message->setSubject("This is the subject of the message!");
|
|
|
|
$message->setTextBody("This is the text body of the message!");
|
|
|
|
$message->finalize();
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertTrue(true, empty($messageObj));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultIDInVariables()
|
|
|
|
{
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
|
|
|
$message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User"));
|
|
|
|
|
2013-10-08 00:53:23 +04:00
|
|
|
$reflectionClass = new \ReflectionClass(get_class($message));
|
2014-05-14 17:06:05 +04:00
|
|
|
$property = $reflectionClass->getProperty('batchRecipientAttributes');
|
|
|
|
$property->setAccessible(true);
|
2013-10-08 00:53:23 +04:00
|
|
|
$propertyValue = $property->getValue($message);
|
|
|
|
$this->assertEquals(1, $propertyValue['test-user@samples.mailgun.org']['id']);
|
|
|
|
}
|
2014-05-14 17:06:05 +04:00
|
|
|
|
|
|
|
public function testgetMessageIds()
|
|
|
|
{
|
2013-10-12 04:31:04 +04:00
|
|
|
$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"));
|
2014-05-14 17:06:05 +04:00
|
|
|
$message->setSubject("This is the subject of the message!");
|
|
|
|
$message->setTextBody("This is the text body of the message!");
|
2013-10-12 04:31:04 +04:00
|
|
|
$message->finalize();
|
|
|
|
|
|
|
|
$this->assertEquals(array("1234"), $message->getMessageIds());
|
|
|
|
}
|
2014-05-13 19:07:44 +04:00
|
|
|
|
|
|
|
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoFrom()
|
|
|
|
{
|
|
|
|
$this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters");
|
|
|
|
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
2014-05-14 17:06:05 +04:00
|
|
|
$message->sendMessage(array(1, 2, 3));
|
2014-05-13 19:07:44 +04:00
|
|
|
}
|
2014-05-14 17:06:05 +04:00
|
|
|
|
2014-05-13 19:07:44 +04:00
|
|
|
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTo()
|
|
|
|
{
|
|
|
|
$this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters");
|
|
|
|
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
2014-05-14 17:06:05 +04:00
|
|
|
$message->sendMessage(array("from" => 1, 2, 3));
|
2014-05-13 19:07:44 +04:00
|
|
|
}
|
2014-05-14 17:06:05 +04:00
|
|
|
|
2014-05-13 19:07:44 +04:00
|
|
|
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoSubject()
|
|
|
|
{
|
|
|
|
$this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters");
|
|
|
|
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
2014-05-14 17:06:05 +04:00
|
|
|
$message->sendMessage(array("from" => 1, "to" => 2, 3));
|
2014-05-13 19:07:44 +04:00
|
|
|
}
|
2014-05-14 17:06:05 +04:00
|
|
|
|
2014-05-13 19:07:44 +04:00
|
|
|
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTextOrHtml()
|
|
|
|
{
|
|
|
|
$this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters");
|
|
|
|
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
2014-05-14 17:06:05 +04:00
|
|
|
$message->sendMessage(array("from" => 1, "to" => 2, "subject" => 3));
|
2014-05-13 19:07:44 +04:00
|
|
|
}
|
2013-07-24 01:29:45 +04:00
|
|
|
}
|
2014-05-14 17:06:05 +04:00
|
|
|
|
2013-07-24 01:29:45 +04:00
|
|
|
?>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|