BatchMessageTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?PHP
  2. namespace Mailgun\Tests\Messages;
  3. use Mailgun\Tests\Mock\Mailgun;
  4. class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase
  5. {
  6. private $client;
  7. private $sampleDomain = "samples.mailgun.org";
  8. public function setUp()
  9. {
  10. $this->client = new Mailgun("My-Super-Awesome-API-Key");
  11. }
  12. public function testBlankInstantiation()
  13. {
  14. $message = $this->client->BatchMessage($this->sampleDomain);
  15. $this->assertTrue(is_array($message->getMessage()));
  16. }
  17. public function testAddRecipient()
  18. {
  19. $message = $this->client->BatchMessage($this->sampleDomain);
  20. $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  21. $messageObj = $message->getMessage();
  22. $this->assertEquals(array("to" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
  23. $reflectionClass = new \ReflectionClass(get_class($message));
  24. $property = $reflectionClass->getProperty('counters');
  25. $property->setAccessible(true);
  26. $array = $property->getValue($message);
  27. $this->assertEquals(1, $array['recipients']['to']);
  28. }
  29. public function testRecipientVariablesOnTo()
  30. {
  31. $message = $this->client->BatchMessage($this->sampleDomain);
  32. $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  33. $messageObj = $message->getMessage();
  34. $this->assertEquals(array("to" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
  35. $reflectionClass = new \ReflectionClass(get_class($message));
  36. $property = $reflectionClass->getProperty('batchRecipientAttributes');
  37. $property->setAccessible(true);
  38. $propertyValue = $property->getValue($message);
  39. $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']);
  40. $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']);
  41. }
  42. public function testRecipientVariablesOnCc()
  43. {
  44. $message = $this->client->BatchMessage($this->sampleDomain);
  45. $message->addCcRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  46. $messageObj = $message->getMessage();
  47. $this->assertEquals(array("cc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
  48. $reflectionClass = new \ReflectionClass(get_class($message));
  49. $property = $reflectionClass->getProperty('batchRecipientAttributes');
  50. $property->setAccessible(true);
  51. $propertyValue = $property->getValue($message);
  52. $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']);
  53. $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']);
  54. }
  55. public function testRecipientVariablesOnBcc()
  56. {
  57. $message = $this->client->BatchMessage($this->sampleDomain);
  58. $message->addBccRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  59. $messageObj = $message->getMessage();
  60. $this->assertEquals(array("bcc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
  61. $reflectionClass = new \ReflectionClass(get_class($message));
  62. $property = $reflectionClass->getProperty('batchRecipientAttributes');
  63. $property->setAccessible(true);
  64. $propertyValue = $property->getValue($message);
  65. $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']);
  66. $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']);
  67. }
  68. public function testAddMultipleBatchRecipients()
  69. {
  70. $message = $this->client->BatchMessage($this->sampleDomain);
  71. for ($i = 0; $i < 100; $i++) {
  72. $message->addToRecipient("$i@samples.mailgun.org", array("first" => "Test", "last" => "User $i"));
  73. }
  74. $messageObj = $message->getMessage();
  75. $this->assertEquals(100, count($messageObj["to"]));
  76. }
  77. public function testMaximumBatchSize()
  78. {
  79. $message = $this->client->BatchMessage($this->sampleDomain);
  80. $message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User"));
  81. $message->setSubject("This is the subject of the message!");
  82. $message->setTextBody("This is the text body of the message!");
  83. for ($i = 0; $i < 1001; $i++) {
  84. $message->addToRecipient("$i@samples.mailgun.org", array("first" => "Test", "last" => "User $i"));
  85. }
  86. $messageObj = $message->getMessage();
  87. $this->assertEquals(1, count($messageObj["to"]));
  88. }
  89. public function testAttributeResetOnEndBatchMessage()
  90. {
  91. $message = $this->client->BatchMessage($this->sampleDomain);
  92. $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  93. $message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User"));
  94. $message->setSubject("This is the subject of the message!");
  95. $message->setTextBody("This is the text body of the message!");
  96. $message->finalize();
  97. $messageObj = $message->getMessage();
  98. $this->assertTrue(true, empty($messageObj));
  99. }
  100. public function testDefaultIDInVariables()
  101. {
  102. $message = $this->client->BatchMessage($this->sampleDomain);
  103. $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  104. $reflectionClass = new \ReflectionClass(get_class($message));
  105. $property = $reflectionClass->getProperty('batchRecipientAttributes');
  106. $property->setAccessible(true);
  107. $propertyValue = $property->getValue($message);
  108. $this->assertEquals(1, $propertyValue['test-user@samples.mailgun.org']['id']);
  109. }
  110. public function testgetMessageIds()
  111. {
  112. $message = $this->client->BatchMessage($this->sampleDomain);
  113. $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  114. $message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User"));
  115. $message->setSubject("This is the subject of the message!");
  116. $message->setTextBody("This is the text body of the message!");
  117. $message->finalize();
  118. $this->assertEquals(array("1234"), $message->getMessageIds());
  119. }
  120. public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoFrom()
  121. {
  122. $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters");
  123. $message = $this->client->BatchMessage($this->sampleDomain);
  124. $message->sendMessage(array(1, 2, 3));
  125. }
  126. public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTo()
  127. {
  128. $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters");
  129. $message = $this->client->BatchMessage($this->sampleDomain);
  130. $message->sendMessage(array("from" => 1, 2, 3));
  131. }
  132. public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoSubject()
  133. {
  134. $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters");
  135. $message = $this->client->BatchMessage($this->sampleDomain);
  136. $message->sendMessage(array("from" => 1, "to" => 2, 3));
  137. }
  138. public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTextOrHtml()
  139. {
  140. $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters");
  141. $message = $this->client->BatchMessage($this->sampleDomain);
  142. $message->sendMessage(array("from" => 1, "to" => 2, "subject" => 3));
  143. }
  144. }