mailgun-php/tests/Messages/BatchMessageTest.php
Alexandre Dupuy d504472206 fix: named addresses must be surrounded by double quotes (#349)
* fix: named addresses must be surrounded by double quotes

Due to your recent API update, named addresses with special chars (like parentheses) are now rejected if they are surrounded by simple quotes
They must be surrounded by double quotes

'Whoever (SomeCompany)' <some@address.com> is no longer valid and will be rejected by your API
"Whoever (SomeCompany)" <some@address.com> is valid and will be OK

* sprintf instaead of old school concatenation
2017-05-16 14:51:02 +02:00

194 lines
8.2 KiB
PHP

<?PHP
/*
* 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.
*/
namespace Mailgun\Tests\Messages;
use Mailgun\Tests\Mock\Mailgun;
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', ['first' => 'Test', 'last' => 'User']);
$messageObj = $message->getMessage();
$this->assertEquals(['to' => ['"Test User" <test@samples.mailgun.org>']], $messageObj);
$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 testAddRecipientWithoutFirstAndLastName()
{
$message = $this->client->BatchMessage($this->sampleDomain);
$message->addToRecipient('test@samples.mailgun.org', 'this-is-not-an-array');
$messageObj = $message->getMessage();
$this->assertEquals(['to' => ['test@samples.mailgun.org']], $messageObj);
$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 testRecipientVariablesOnTo()
{
$message = $this->client->BatchMessage($this->sampleDomain);
$message->addToRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
$messageObj = $message->getMessage();
$this->assertEquals(['to' => ['"Test User" <test@samples.mailgun.org>']], $messageObj);
$reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('batchRecipientAttributes');
$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']);
}
public function testRecipientVariablesOnCc()
{
$message = $this->client->BatchMessage($this->sampleDomain);
$message->addCcRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
$messageObj = $message->getMessage();
$this->assertEquals(['cc' => ['"Test User" <test@samples.mailgun.org>']], $messageObj);
$reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('batchRecipientAttributes');
$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']);
}
public function testRecipientVariablesOnBcc()
{
$message = $this->client->BatchMessage($this->sampleDomain);
$message->addBccRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
$messageObj = $message->getMessage();
$this->assertEquals(['bcc' => ['"Test User" <test@samples.mailgun.org>']], $messageObj);
$reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('batchRecipientAttributes');
$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']);
}
public function testAddMultipleBatchRecipients()
{
$message = $this->client->BatchMessage($this->sampleDomain);
for ($i = 0; $i < 100; ++$i) {
$message->addToRecipient("$i@samples.mailgun.org", ['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', ['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", ['first' => 'Test', 'last' => "User $i"]);
}
$messageObj = $message->getMessage();
$this->assertEquals(1, count($messageObj['to']));
}
public function testAttributeResetOnEndBatchMessage()
{
$message = $this->client->BatchMessage($this->sampleDomain);
$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();
$messageObj = $message->getMessage();
$this->assertTrue(true, empty($messageObj));
}
public function testDefaultIDInVariables()
{
$message = $this->client->BatchMessage($this->sampleDomain);
$message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']);
$reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('batchRecipientAttributes');
$property->setAccessible(true);
$propertyValue = $property->getValue($message);
$this->assertEquals(1, $propertyValue['test-user@samples.mailgun.org']['id']);
}
public function testgetMessageIds()
{
$message = $this->client->BatchMessage($this->sampleDomain);
$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();
$this->assertEquals(['1234'], $message->getMessageIds());
}
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoFrom()
{
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain);
$message->sendMessage([1, 2, 3]);
}
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTo()
{
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain);
$message->sendMessage(['from' => 1, 2, 3]);
}
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoSubject()
{
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain);
$message->sendMessage(['from' => 1, 'to' => 2, 3]);
}
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTextOrHtml()
{
$this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain);
$message->sendMessage(['from' => 1, 'to' => 2, 'subject' => 3]);
}
}