MessageTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. require __DIR__ .'/../vendor/autoload.php';
  3. use CMText\Channels;
  4. use CMText\Message;
  5. use PHPUnit\Framework\TestCase;
  6. class MessageTest extends TestCase
  7. {
  8. /**
  9. * Result of initializing the Message class with parameters.
  10. */
  11. public function testInitializingCorrectly()
  12. {
  13. $testSender = 'test-sender';
  14. $new = new Message(
  15. 'test text',
  16. $testSender,
  17. [
  18. '00334455667788',
  19. ]
  20. );
  21. $this->assertInstanceOf(
  22. Message::class,
  23. $new
  24. );
  25. $json = $new->jsonSerialize();
  26. $this->assertEquals(
  27. $testSender,
  28. $json->from
  29. );
  30. }
  31. /**
  32. * Result of setting properties on a Message object.
  33. */
  34. public function testSettingProperties()
  35. {
  36. $body = 'Ether Ð and Euro € icons, and a ß for German text';
  37. $from = 'test-sender';
  38. $to = [
  39. '11111111111111',
  40. '22222222222222',
  41. '99999999999999',
  42. ];
  43. $reference = 'test-reference';
  44. $dcs = 8;
  45. // initialize an empty Message
  46. $new = new Message();
  47. // set properties on the fly
  48. $new->body = $body;
  49. $new->from = $from;
  50. $new->to = $to;
  51. $new->reference = $reference;
  52. $new->dcs = $dcs;
  53. // get an instance that we can check
  54. $json = $new->jsonSerialize();
  55. // verify the Body content is correctly set
  56. $this->assertEquals(
  57. $body,
  58. $json->body->jsonSerialize()->content
  59. );
  60. // verify the Sender is correctly set
  61. $this->assertEquals(
  62. $from,
  63. $json->from
  64. );
  65. // verify the Reference is correctly set
  66. $this->assertEquals(
  67. $reference,
  68. $json->reference
  69. );
  70. // verify the dcs is correctly set
  71. $this->assertEquals(
  72. $dcs,
  73. $json->dcs
  74. );
  75. // verify the correct amount of Recipients is added
  76. $this->assertCount(
  77. count($to),
  78. $json->to
  79. );
  80. // verify the Recipients are correctly set
  81. while(current($to)) {
  82. $this->assertCount(
  83. 1,
  84. array_filter($json->to, function ($jsonTo) use ($to) {
  85. return $jsonTo == (object)[
  86. 'number' => current($to)
  87. ];
  88. })
  89. );
  90. next($to);
  91. }
  92. // verify Recipient limit is respected
  93. try{
  94. for ($i = 0; $i < Message::RECIPIENTS_MAXIMUM; ++$i){
  95. $new->AddRecipients([
  96. str_pad('00', 13, strval($i))
  97. ]);
  98. }
  99. }catch (\Exception $exception){
  100. $this->assertInstanceOf(
  101. \CMText\Exceptions\RecipientLimitException::class,
  102. $exception
  103. );
  104. }
  105. }
  106. /**
  107. * Tests if supplied channel is returned properly
  108. */
  109. public function testAllowedChannel()
  110. {
  111. $message = new Message();
  112. $message->WithChannels([Channels::WHATSAPP]);
  113. $json = $message->jsonSerialize();
  114. $this->assertEquals($json->allowedChannels, [Channels::WHATSAPP]);
  115. }
  116. /**
  117. * Tests if supplied channels are returned properly
  118. */
  119. public function testAllowedChannels()
  120. {
  121. $message = new Message();
  122. $message->WithChannels([Channels::WHATSAPP, Channels::SMS]);
  123. $json = $message->jsonSerialize();
  124. $this->assertEquals($json->allowedChannels, [Channels::SMS, Channels::WHATSAPP]);
  125. }
  126. /**
  127. * Tests if a Template message is formatted properly
  128. * @throws \CMText\Exceptions\WhatsappTemplateComponentParameterTypeException
  129. */
  130. public function testWithTemplate()
  131. {
  132. $message = new \CMText\Message();
  133. $message->WithTemplate(
  134. new \CMText\RichContent\Messages\TemplateMessage(
  135. new \CMText\RichContent\Templates\Whatsapp\WhatsappTemplate(
  136. 'namespace',
  137. 'elementname',
  138. new \CMText\RichContent\Templates\Whatsapp\Language('nl')
  139. )
  140. )
  141. );
  142. $json = json_decode(json_encode($message));
  143. $this->assertObjectHasAttribute(
  144. 'richContent',
  145. $json
  146. );
  147. $this->assertObjectHasAttribute(
  148. 'conversation',
  149. $json->richContent
  150. );
  151. $this->assertObjectHasAttribute(
  152. 'template',
  153. $json->richContent->conversation[0]
  154. );
  155. $this->assertObjectHasAttribute(
  156. 'whatsapp',
  157. $json->richContent->conversation[0]->template
  158. );
  159. }
  160. public function testWithPayment()
  161. {
  162. $message = new \CMText\Message();
  163. $message
  164. ->WithChannels([Channels::IMESSAGE])
  165. ->WithPayment(
  166. new \CMText\RichContent\Messages\PaymentMessage(
  167. new \CMText\RichContent\Payments\ApplePayConfiguration(
  168. 'merchant-name',
  169. 'product-description',
  170. 'unique-order-guid',
  171. 1,
  172. 'currency-code',
  173. 'recipient-email',
  174. 'recipient-country-code',
  175. 'language-country-code',
  176. true,
  177. true,
  178. [
  179. new \CMText\RichContent\Common\LineItem(
  180. 'product-name',
  181. 'final-or-pending',
  182. 1
  183. )
  184. ]
  185. )
  186. )
  187. );
  188. $json = json_decode(json_encode($message));
  189. $this->assertObjectHasAttribute(
  190. 'richContent',
  191. $json
  192. );
  193. $this->assertObjectHasAttribute(
  194. 'conversation',
  195. $json->richContent
  196. );
  197. $this->assertObjectHasAttribute(
  198. 'payment',
  199. $json->richContent->conversation[0]
  200. );
  201. $this->assertObjectHasAttribute(
  202. 'lineItems',
  203. $json->richContent->conversation[0]->payment
  204. );
  205. }
  206. public function testSettingCustomBodyType()
  207. {
  208. $customMessageBody = new \CMText\MessageBody(
  209. 'content',
  210. \CMText\MessageBodyTypes::TEXT
  211. );
  212. $message = new Message($customMessageBody);
  213. $json = json_decode( json_encode($message) );
  214. $this->assertEquals(
  215. \CMText\MessageBodyTypes::TEXT,
  216. $json->body->type
  217. );
  218. }
  219. public function testSettingBodyIncorrectly()
  220. {
  221. $this->expectException(TypeError::class);
  222. new Message(new Channels());
  223. }
  224. }