TextClientTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. require __DIR__ .'/../vendor/autoload.php';
  3. use CMText\TextClient;
  4. use PHPUnit\Framework\TestCase;
  5. class TextClientTest extends TestCase
  6. {
  7. /**
  8. * Result of sending a Message when the gateway is unavailable.
  9. */
  10. public function testUnavailableGateway()
  11. {
  12. $client = new TextClient('your-api-key', 'unavailablehost');
  13. $result = $client->SendMessage('body-content', 'CM.com', ['00334455667788']);
  14. $this->assertEquals(
  15. \CMText\TextClientStatusCodes::UNKNOWN,
  16. $result->statusCode
  17. );
  18. }
  19. /**
  20. * The maximum amount of Message objects in a request should be respected
  21. */
  22. public function testMessagesLimit()
  23. {
  24. $client = new TextClient('your-api-key');
  25. try{
  26. $client->send(
  27. array_fill(
  28. 0,
  29. TextClient::MESSAGES_MAXIMUM + 1,
  30. new \CMText\Message()
  31. )
  32. );
  33. }catch (\Exception $exception){
  34. $this->assertInstanceOf(
  35. \CMText\Exceptions\MessagesLimitException::class,
  36. $exception
  37. );
  38. }
  39. }
  40. /**
  41. * Building a RichContent Message should result in correctly formatted json
  42. */
  43. public function testRichMessageBuilding()
  44. {
  45. try{
  46. $message = new \CMText\Message('Message Text', 'Sender_name', ['Recipient_PhoneNumber']);
  47. $message
  48. ->WithChannels([\CMText\Channels::WHATSAPP])
  49. ->WithHybridAppKey('your-secret-hybrid-app-key')
  50. ->WithRichMessage(
  51. new \CMText\RichContent\Messages\MediaMessage(
  52. 'cm.com',
  53. 'https://avatars3.githubusercontent.com/u/8234794?s=200&v=4',
  54. 'image/png'
  55. )
  56. );
  57. }catch (\Exception $exception){
  58. $message = null;
  59. }
  60. $this->assertInstanceOf(
  61. \CMText\Message::class,
  62. $message
  63. );
  64. $this->assertJson( json_encode($message) );
  65. $json = $message->jsonSerialize();
  66. $this->assertObjectHasAttribute(
  67. 'allowedChannels',
  68. $json
  69. );
  70. $this->assertObjectHasAttribute(
  71. 'appKey',
  72. $json
  73. );
  74. $this->assertObjectHasAttribute(
  75. 'richContent',
  76. $json
  77. );
  78. }
  79. /**
  80. * Building a RichContent Message should result in correctly formatted json
  81. */
  82. public function testRichMessageBuildingWithSuggestions()
  83. {
  84. try{
  85. $message = new \CMText\Message('Message Text', 'Sender_name', ['Recipient_PhoneNumber']);
  86. $message
  87. ->WithChannels([\CMText\Channels::RCS])
  88. ->WithSuggestions([
  89. new \CMText\RichContent\Suggestions\ReplySuggestion('Opt In', 'OK'),
  90. new \CMText\RichContent\Suggestions\ReplySuggestion('Opt Out', 'STOP'),
  91. ]);
  92. }catch (\Exception $exception){
  93. $message = null;
  94. }
  95. $this->assertInstanceOf(
  96. \CMText\Message::class,
  97. $message
  98. );
  99. $this->assertJson( json_encode($message) );
  100. $json = $message->jsonSerialize();
  101. $this->assertObjectHasAttribute(
  102. 'allowedChannels',
  103. $json
  104. );
  105. $this->assertObjectHasAttribute(
  106. 'richContent',
  107. $json
  108. );
  109. }
  110. /**
  111. * TextClientResult should return a correct object on a weird response
  112. */
  113. public function testTextClientResultForWeirdResponses()
  114. {
  115. // no json content and no content at all should act this way
  116. $body = '{[nojson';
  117. $result = new \CMText\TextClientResult(418, $body);
  118. $this->assertEquals(
  119. \CMText\TextClientStatusCodes::UNKNOWN,
  120. $result->statusCode
  121. );
  122. $this->assertEquals(
  123. $body,
  124. $result->statusMessage
  125. );
  126. }
  127. }