mailgun-php/tests/Messages/BatchMessageTest.php

173 lines
7.5 KiB
PHP
Raw Normal View History

<?PHP
2016-07-24 14:42:47 +03:00
2013-08-13 23:26:34 +04:00
namespace Mailgun\Tests\Messages;
use Mailgun\Tests\Mock\Mailgun;
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');
$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');
$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');
$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');
$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()
{
$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
$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);
$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()
{
$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!');
$message->finalize();
2016-07-24 14:42:47 +03:00
$this->assertEquals(['1234'], $message->getMessageIds());
}
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoFrom()
{
2016-07-24 14:42:47 +03:00
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain);
2016-07-24 14:42:47 +03:00
$message->sendMessage([1, 2, 3]);
}
2014-05-14 17:06:05 +04:00
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTo()
{
2016-07-24 14:42:47 +03:00
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain);
2016-07-24 14:42:47 +03:00
$message->sendMessage(['from' => 1, 2, 3]);
}
2014-05-14 17:06:05 +04:00
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoSubject()
{
2016-07-24 14:42:47 +03:00
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain);
2016-07-24 14:42:47 +03:00
$message->sendMessage(['from' => 1, 'to' => 2, 3]);
}
2014-05-14 17:06:05 +04:00
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTextOrHtml()
{
2016-07-24 14:42:47 +03:00
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain);
2016-07-24 14:42:47 +03:00
$message->sendMessage(['from' => 1, 'to' => 2, 'subject' => 3]);
}
}