MessageBuilderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?PHP
  2. namespace Mailgun\Tests\Messages;
  3. use Mailgun\Tests\Mock\Mailgun;
  4. class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
  5. {
  6. private $client;
  7. public function setUp()
  8. {
  9. $this->client = new Mailgun("My-Super-Awesome-API-Key", "samples.mailgun.org", false);
  10. }
  11. public function testBlankInstantiation()
  12. {
  13. $message = $this->client->MessageBuilder();
  14. $this->assertTrue(is_array($message->getMessage()));
  15. }
  16. public function testCountersSetToZero()
  17. {
  18. $message = $this->client->MessageBuilder();
  19. $reflectionClass = new \ReflectionClass(get_class($message));
  20. $property = $reflectionClass->getProperty('counters');
  21. $property->setAccessible(true);
  22. $propertyValue = $property->getValue($message);
  23. $this->assertEquals(0, $propertyValue['recipients']['to']);
  24. $this->assertEquals(0, $propertyValue['recipients']['cc']);
  25. $this->assertEquals(0, $propertyValue['recipients']['bcc']);
  26. $this->assertEquals(0, $propertyValue['attributes']['attachment']);
  27. $this->assertEquals(0, $propertyValue['attributes']['campaign_id']);
  28. $this->assertEquals(0, $propertyValue['attributes']['custom_option']);
  29. $this->assertEquals(0, $propertyValue['attributes']['tag']);
  30. }
  31. public function testAddToRecipient()
  32. {
  33. $message = $this->client->MessageBuilder();
  34. $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  35. $messageObj = $message->getMessage();
  36. $this->assertEquals(array("to" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
  37. }
  38. public function testAddCcRecipient()
  39. {
  40. $message = $this->client->MessageBuilder();
  41. $message->addCcRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  42. $messageObj = $message->getMessage();
  43. $this->assertEquals(array("cc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
  44. }
  45. public function testAddBccRecipient()
  46. {
  47. $message = $this->client->MessageBuilder();
  48. $message->addBccRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  49. $messageObj = $message->getMessage();
  50. $this->assertEquals(array("bcc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
  51. }
  52. public function testToRecipientCount()
  53. {
  54. $message = $this->client->MessageBuilder();
  55. $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  56. $reflectionClass = new \ReflectionClass(get_class($message));
  57. $property = $reflectionClass->getProperty('counters');
  58. $property->setAccessible(true);
  59. $array = $property->getValue($message);
  60. $this->assertEquals(1, $array['recipients']['to']);
  61. }
  62. public function testCcRecipientCount()
  63. {
  64. $message = $this->client->MessageBuilder();
  65. $message->addCcRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  66. $reflectionClass = new \ReflectionClass(get_class($message));
  67. $property = $reflectionClass->getProperty('counters');
  68. $property->setAccessible(true);
  69. $array = $property->getValue($message);
  70. $this->assertEquals(1, $array['recipients']['cc']);
  71. }
  72. public function testBccRecipientCount()
  73. {
  74. $message = $this->client->MessageBuilder();
  75. $message->addBccRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  76. $reflectionClass = new \ReflectionClass(get_class($message));
  77. $property = $reflectionClass->getProperty('counters');
  78. $property->setAccessible(true);
  79. $array = $property->getValue($message);
  80. $this->assertEquals(1, $array['recipients']['bcc']);
  81. }
  82. public function testSetFromAddress()
  83. {
  84. $message = $this->client->MessageBuilder();
  85. $message->setFromAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  86. $messageObj = $message->getMessage();
  87. $this->assertEquals(array("from" => array("'Test User' <test@samples.mailgun.org>")), $messageObj);
  88. }
  89. public function testSetReplyTo()
  90. {
  91. $message = $this->client->MessageBuilder();
  92. $message->setReplyToAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User"));
  93. $messageObj = $message->getMessage();
  94. $this->assertEquals(array("h:reply-to" => "'Test User' <test@samples.mailgun.org>"), $messageObj);
  95. }
  96. public function testSetSubject()
  97. {
  98. $message = $this->client->MessageBuilder();
  99. $message->setSubject("Test Subject");
  100. $messageObj = $message->getMessage();
  101. $this->assertEquals(array("subject" => "Test Subject"), $messageObj);
  102. }
  103. public function testAddCustomHeader()
  104. {
  105. $message = $this->client->MessageBuilder();
  106. $message->addCustomHeader("My-Header", "123");
  107. $messageObj = $message->getMessage();
  108. $this->assertEquals(array("h:My-Header" => array("123")), $messageObj);
  109. }
  110. public function testSetTextBody()
  111. {
  112. $message = $this->client->MessageBuilder();
  113. $message->setTextBody("This is the text body!");
  114. $messageObj = $message->getMessage();
  115. $this->assertEquals(array("text" => "This is the text body!"), $messageObj);
  116. }
  117. public function testSetHtmlBody()
  118. {
  119. $message = $this->client->MessageBuilder();
  120. $message->setHtmlBody("<html><body>This is an awesome email</body></html>");
  121. $messageObj = $message->getMessage();
  122. $this->assertEquals(array("html" => "<html><body>This is an awesome email</body></html>"), $messageObj);
  123. }
  124. public function testAddAttachments()
  125. {
  126. $message = $this->client->MessageBuilder();
  127. $message->addAttachment("@../TestAssets/mailgun_icon.png");
  128. $message->addAttachment("@../TestAssets/rackspace_logo.png");
  129. $messageObj = $message->getFiles();
  130. $this->assertEquals(
  131. array(
  132. array(
  133. 'filePath' => "@../TestAssets/mailgun_icon.png",
  134. 'remoteName' => null
  135. ),
  136. array(
  137. 'filePath' => "@../TestAssets/rackspace_logo.png",
  138. 'remoteName' => null
  139. )
  140. ),
  141. $messageObj["attachment"]
  142. );
  143. }
  144. public function testAddInlineImages()
  145. {
  146. $message = $this->client->MessageBuilder();
  147. $message->addInlineImage("@../TestAssets/mailgun_icon.png");
  148. $message->addInlineImage("@../TestAssets/rackspace_logo.png");
  149. $messageObj = $message->getFiles();
  150. $this->assertEquals(
  151. array(
  152. array(
  153. 'filePath' => "@../TestAssets/mailgun_icon.png",
  154. 'remoteName' => null
  155. ),
  156. array(
  157. 'filePath' => "@../TestAssets/rackspace_logo.png",
  158. 'remoteName' => null
  159. )
  160. ),
  161. $messageObj['inline']
  162. );
  163. }
  164. public function testAddAttachmentsPostName()
  165. {
  166. $message = $this->client->MessageBuilder();
  167. $message->addAttachment('@../TestAssets/mailgun_icon.png', 'mg_icon.png');
  168. $message->addAttachment('@../TestAssets/rackspace_logo.png', 'rs_logo.png');
  169. $messageObj = $message->getFiles();
  170. $this->assertEquals(
  171. array(
  172. array(
  173. 'filePath' => '@../TestAssets/mailgun_icon.png',
  174. 'remoteName' => 'mg_icon.png'
  175. ),
  176. array(
  177. 'filePath' => '@../TestAssets/rackspace_logo.png',
  178. 'remoteName' => 'rs_logo.png'
  179. )
  180. ),
  181. $messageObj["attachment"]
  182. );
  183. }
  184. public function testAddInlineImagePostName()
  185. {
  186. $message = $this->client->MessageBuilder();
  187. $message->addInlineImage('@../TestAssets/mailgun_icon.png', 'mg_icon.png');
  188. $message->addInlineImage('@../TestAssets/rackspace_logo.png', 'rs_logo.png');
  189. $messageObj = $message->getFiles();
  190. $this->assertEquals(
  191. array(
  192. array(
  193. 'filePath' => '@../TestAssets/mailgun_icon.png',
  194. 'remoteName' => 'mg_icon.png'
  195. ),
  196. array(
  197. 'filePath' => '@../TestAssets/rackspace_logo.png',
  198. 'remoteName' => 'rs_logo.png'
  199. )
  200. ),
  201. $messageObj['inline']
  202. );
  203. }
  204. public function testsetTestMode()
  205. {
  206. $message = $this->client->MessageBuilder();
  207. $message->setTestMode(true);
  208. $messageObj = $message->getMessage();
  209. $this->assertEquals(array("o:testmode" => "yes"), $messageObj);
  210. $message->setTestMode(false);
  211. $messageObj = $message->getMessage();
  212. $this->assertEquals(array("o:testmode" => "no"), $messageObj);
  213. $message->setTestMode("yes");
  214. $messageObj = $message->getMessage();
  215. $this->assertEquals(array("o:testmode" => "yes"), $messageObj);
  216. $message->setTestMode("no");
  217. $messageObj = $message->getMessage();
  218. $this->assertEquals(array("o:testmode" => "no"), $messageObj);
  219. }
  220. public function addCampaignId()
  221. {
  222. $message = $this->client->MessageBuilder();
  223. $message->addCampaignId("ABC123");
  224. $message->addCampaignId("XYZ987");
  225. $message->addCampaignId("TUV456");
  226. $message->addCampaignId("NONO123");
  227. $messageObj = $message->getMessage();
  228. $this->assertEquals(array("o:campaign" => array("ABC123", "XYZ987", "TUV456")), $messageObj);
  229. }
  230. public function testSetDkim()
  231. {
  232. $message = $this->client->MessageBuilder();
  233. $message->setDkim(true);
  234. $messageObj = $message->getMessage();
  235. $this->assertEquals(array("o:dkim" => "yes"), $messageObj);
  236. $message->setDkim(false);
  237. $messageObj = $message->getMessage();
  238. $this->assertEquals(array("o:dkim" => "no"), $messageObj);
  239. $message->setDkim("yes");
  240. $messageObj = $message->getMessage();
  241. $this->assertEquals(array("o:dkim" => "yes"), $messageObj);
  242. $message->setDkim("no");
  243. $messageObj = $message->getMessage();
  244. $this->assertEquals(array("o:dkim" => "no"), $messageObj);
  245. }
  246. public function testSetClickTracking()
  247. {
  248. $message = $this->client->MessageBuilder();
  249. $message->setClickTracking(true);
  250. $messageObj = $message->getMessage();
  251. $this->assertEquals(array("o:tracking-clicks" => "yes"), $messageObj);
  252. $message->setClickTracking(false);
  253. $messageObj = $message->getMessage();
  254. $this->assertEquals(array("o:tracking-clicks" => "no"), $messageObj);
  255. $message->setClickTracking("yes");
  256. $messageObj = $message->getMessage();
  257. $this->assertEquals(array("o:tracking-clicks" => "yes"), $messageObj);
  258. $message->setClickTracking("no");
  259. $messageObj = $message->getMessage();
  260. $this->assertEquals(array("o:tracking-clicks" => "no"), $messageObj);
  261. }
  262. public function testSetOpenTracking()
  263. {
  264. $message = $this->client->MessageBuilder();
  265. $message->setOpenTracking(true);
  266. $messageObj = $message->getMessage();
  267. $this->assertEquals(array("o:tracking-opens" => "yes"), $messageObj);
  268. $message->setOpenTracking(false);
  269. $messageObj = $message->getMessage();
  270. $this->assertEquals(array("o:tracking-opens" => "no"), $messageObj);
  271. $message->setOpenTracking("yes");
  272. $messageObj = $message->getMessage();
  273. $this->assertEquals(array("o:tracking-opens" => "yes"), $messageObj);
  274. $message->setOpenTracking("no");
  275. $messageObj = $message->getMessage();
  276. $this->assertEquals(array("o:tracking-opens" => "no"), $messageObj);
  277. }
  278. public function testSetDeliveryTime()
  279. {
  280. $message = $this->client->MessageBuilder();
  281. $message->setDeliveryTime("January 15, 2014 8:00AM", "CST");
  282. $messageObj = $message->getMessage();
  283. $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 -0600"), $messageObj);
  284. $message->setDeliveryTime("January 15, 2014 8:00AM", "UTC");
  285. $messageObj = $message->getMessage();
  286. $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 +0000"), $messageObj);
  287. $message->setDeliveryTime("January 15, 2014 8:00AM");
  288. $messageObj = $message->getMessage();
  289. $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 +0000"), $messageObj);
  290. $message->setDeliveryTime("1/15/2014 13:50:01", "CDT");
  291. $messageObj = $message->getMessage();
  292. $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 13:50:01 -0600"), $messageObj);
  293. // https://github.com/mailgun/mailgun-php/pull/42
  294. // https://github.com/mailgun/mailgun-php/issues/43
  295. //$message->setDeliveryTime("first saturday of July 2013 8:00AM", "CDT");
  296. //$messageObj = $message->getMessage();
  297. //$this->assertEquals(array("o:deliverytime" => "Sat, 06 Jul 2013 08:00:00 -0500"), $messageObj);
  298. }
  299. public function testAddCustomData()
  300. {
  301. $message = $this->client->MessageBuilder();
  302. $message->addCustomData("My-Super-Awesome-Data", array("What" => "Mailgun Rocks!"));
  303. $messageObj = $message->getMessage();
  304. $this->assertEquals(array("v:My-Super-Awesome-Data" => "{\"What\":\"Mailgun Rocks!\"}"), $messageObj);
  305. }
  306. public function testAddCustomParameter()
  307. {
  308. $message = $this->client->MessageBuilder();
  309. $message->addCustomParameter("my-option", "yes");
  310. $message->addCustomParameter("o:my-other-option", "no");
  311. $messageObj = $message->getMessage();
  312. $this->assertEquals(array("my-option" => array("yes"), "o:my-other-option" => array("no")), $messageObj);
  313. }
  314. public function testSetMessage()
  315. {
  316. $message = array(1, 2, 3, 4, 5);
  317. $messageBuilder = $this->client->MessageBuilder();
  318. $messageBuilder->setMessage($message);
  319. $this->assertEquals($message, $messageBuilder->getMessage());
  320. }
  321. }