2013-07-24 01:29:45 +04:00
|
|
|
<?PHP
|
2016-07-24 14:42:47 +03:00
|
|
|
|
2016-09-18 10:56:14 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-2016 Mailgun
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
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;
|
2016-07-24 14:42:47 +03:00
|
|
|
private $sampleDomain = 'samples.mailgun.org';
|
2014-05-14 17:06:05 +04:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->client = new Mailgun('My-Super-Awesome-API-Key');
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBlankInstantiation()
|
|
|
|
{
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
|
|
|
$this->assertTrue(is_array($message->getMessage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddRecipient()
|
|
|
|
{
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
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();
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->assertEquals(['to' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
|
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');
|
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);
|
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();
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->assertEquals(['to' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
|
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('batchRecipientAttributes');
|
2013-10-12 00:36:35 +04:00
|
|
|
$property->setAccessible(true);
|
|
|
|
$propertyValue = $property->getValue($message);
|
2016-07-24 14:42:47 +03:00
|
|
|
$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);
|
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();
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->assertEquals(['cc' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
|
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('batchRecipientAttributes');
|
2013-10-12 00:36:35 +04:00
|
|
|
$property->setAccessible(true);
|
|
|
|
$propertyValue = $property->getValue($message);
|
2016-07-24 14:42:47 +03:00
|
|
|
$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);
|
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();
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->assertEquals(['bcc' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
|
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('batchRecipientAttributes');
|
2013-10-12 00:36:35 +04:00
|
|
|
$property->setAccessible(true);
|
|
|
|
$propertyValue = $property->getValue($message);
|
2016-07-24 14:42:47 +03:00
|
|
|
$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++) {
|
2016-07-24 14:42:47 +03:00
|
|
|
$message->addToRecipient("$i@samples.mailgun.org", ['first' => 'Test', 'last' => "User $i"]);
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
$messageObj = $message->getMessage();
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->assertEquals(100, count($messageObj['to']));
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMaximumBatchSize()
|
|
|
|
{
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
2016-07-24 14:42:47 +03:00
|
|
|
$message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']);
|
|
|
|
$message->setSubject('This is the subject of the message!');
|
|
|
|
$message->setTextBody('This is the text body of the message!');
|
2014-05-14 17:06:05 +04:00
|
|
|
for ($i = 0; $i < 1001; $i++) {
|
2016-07-24 14:42:47 +03:00
|
|
|
$message->addToRecipient("$i@samples.mailgun.org", ['first' => 'Test', 'last' => "User $i"]);
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
$messageObj = $message->getMessage();
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->assertEquals(1, count($messageObj['to']));
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAttributeResetOnEndBatchMessage()
|
|
|
|
{
|
2013-10-08 00:53:23 +04:00
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
2016-07-24 14:42:47 +03:00
|
|
|
$message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
|
|
|
|
$message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']);
|
|
|
|
$message->setSubject('This is the subject of the message!');
|
|
|
|
$message->setTextBody('This is the text body of the message!');
|
2014-05-14 17:06:05 +04:00
|
|
|
$message->finalize();
|
|
|
|
$messageObj = $message->getMessage();
|
|
|
|
$this->assertTrue(true, empty($messageObj));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultIDInVariables()
|
|
|
|
{
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
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
|
|
|
|
2013-10-08 00:53:23 +04:00
|
|
|
$reflectionClass = new \ReflectionClass(get_class($message));
|
2016-07-24 14:42:47 +03:00
|
|
|
$property = $reflectionClass->getProperty('batchRecipientAttributes');
|
2014-05-14 17:06:05 +04:00
|
|
|
$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);
|
2016-07-24 14:42:47 +03:00
|
|
|
$message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
|
|
|
|
$message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']);
|
|
|
|
$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();
|
|
|
|
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->assertEquals(['1234'], $message->getMessageIds());
|
2013-10-12 04:31:04 +04:00
|
|
|
}
|
2014-05-13 19:07:44 +04:00
|
|
|
|
|
|
|
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoFrom()
|
|
|
|
{
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
|
2014-05-13 19:07:44 +04:00
|
|
|
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
2016-07-24 14:42:47 +03:00
|
|
|
$message->sendMessage([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()
|
|
|
|
{
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
|
2014-05-13 19:07:44 +04:00
|
|
|
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
2016-07-24 14:42:47 +03:00
|
|
|
$message->sendMessage(['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()
|
|
|
|
{
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
|
2014-05-13 19:07:44 +04:00
|
|
|
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
2016-07-24 14:42:47 +03:00
|
|
|
$message->sendMessage(['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()
|
|
|
|
{
|
2016-07-24 14:42:47 +03:00
|
|
|
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
|
2014-05-13 19:07:44 +04:00
|
|
|
|
|
|
|
$message = $this->client->BatchMessage($this->sampleDomain);
|
2016-07-24 14:42:47 +03:00
|
|
|
$message->sendMessage(['from' => 1, 'to' => 2, 'subject' => 3]);
|
2014-05-13 19:07:44 +04:00
|
|
|
}
|
2013-07-24 01:29:45 +04:00
|
|
|
}
|