SuggestionsTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class SuggestionsTest extends TestCase
  4. {
  5. public function testBaseClassProperties()
  6. {
  7. $this->assertClassHasAttribute(
  8. 'action',
  9. \CMText\RichContent\Suggestions\SuggestionBase::class
  10. );
  11. $this->assertClassHasAttribute(
  12. 'label',
  13. \CMText\RichContent\Suggestions\SuggestionBase::class
  14. );
  15. }
  16. public function testCalendarSuggestion()
  17. {
  18. $calendar = new \CMText\RichContent\Suggestions\CalendarSuggestion(
  19. 'test label',
  20. new \CMText\RichContent\Suggestions\CalendarOptions(
  21. new DateTimeImmutable('2019-12-31T23:59:59+0000'),
  22. new DateTimeImmutable('2020-01-01T01:01:01+0000'),
  23. 'test title',
  24. 'test description'
  25. )
  26. );
  27. $this->assertJson( json_encode($calendar) );
  28. $this->assertInstanceOf(
  29. \CMText\RichContent\Suggestions\CalendarSuggestion::class,
  30. $calendar
  31. );
  32. $json = json_decode( json_encode($calendar) );
  33. $this->assertObjectHasAttribute(
  34. 'calendar',
  35. $json
  36. );
  37. $this->assertObjectHasAttribute(
  38. 'startTime',
  39. $json->calendar
  40. );
  41. $this->assertObjectHasAttribute(
  42. 'endTime',
  43. $json->calendar
  44. );
  45. $this->assertObjectHasAttribute(
  46. 'title',
  47. $json->calendar
  48. );
  49. $this->assertObjectHasAttribute(
  50. 'description',
  51. $json->calendar
  52. );
  53. }
  54. public function testDialSuggestion()
  55. {
  56. $dial = new \CMText\RichContent\Suggestions\DialSuggestion(
  57. 'test label',
  58. '+334455667788'
  59. );
  60. $this->assertJson( json_encode($dial) );
  61. $this->assertInstanceOf(
  62. \CMText\RichContent\Suggestions\DialSuggestion::class,
  63. $dial
  64. );
  65. $json = json_decode( json_encode($dial) );
  66. $this->assertObjectHasAttribute(
  67. 'dial',
  68. $json
  69. );
  70. $this->assertObjectHasAttribute(
  71. 'PhoneNumber',
  72. $json->dial
  73. );
  74. }
  75. public function testOpenUrlSuggestion()
  76. {
  77. $openUrl = new \CMText\RichContent\Suggestions\OpenUrlSuggestion(
  78. 'test Label',
  79. 'test://url'
  80. );
  81. $this->assertJson( json_encode($openUrl) );
  82. $this->assertInstanceOf(
  83. \CMText\RichContent\Suggestions\OpenUrlSuggestion::class,
  84. $openUrl
  85. );
  86. $this->assertObjectHasAttribute(
  87. 'url',
  88. json_decode( json_encode($openUrl) )
  89. );
  90. }
  91. public function testReplySuggestion()
  92. {
  93. $reply = new \CMText\RichContent\Suggestions\ReplySuggestion(
  94. 'test label',
  95. 'test reply text'
  96. );
  97. $this->assertJson( json_encode($reply) );
  98. $this->assertInstanceOf(
  99. \CMText\RichContent\Suggestions\ReplySuggestion::class,
  100. $reply
  101. );
  102. $this->assertObjectHasAttribute(
  103. 'postbackdata',
  104. json_decode( json_encode($reply) )
  105. );
  106. }
  107. public function testViewLocationSuggestion()
  108. {
  109. $viewStaticLocation = new \CMText\RichContent\Suggestions\ViewLocationSuggestion(
  110. 'test label',
  111. new \CMText\RichContent\Common\ViewLocationStatic(
  112. 'CM HQ',
  113. '51.603802',
  114. '4.770821'
  115. )
  116. );
  117. $this->assertJson( json_encode($viewStaticLocation) );
  118. $this->assertInstanceOf(
  119. \CMText\RichContent\Suggestions\ViewLocationSuggestion::class,
  120. $viewStaticLocation
  121. );
  122. $json = json_decode( json_encode($viewStaticLocation) );
  123. $this->assertObjectHasAttribute(
  124. 'viewLocation',
  125. $json
  126. );
  127. $this->assertObjectHasAttribute(
  128. 'latitude',
  129. $json->viewLocation
  130. );
  131. $this->assertObjectHasAttribute(
  132. 'longitude',
  133. $json->viewLocation
  134. );
  135. $this->assertObjectHasAttribute(
  136. 'label',
  137. $json->viewLocation
  138. );
  139. $this->assertObjectNotHasAttribute(
  140. 'radius',
  141. $json->viewLocation
  142. );
  143. $viewDynamicLocation = new \CMText\RichContent\Suggestions\ViewLocationSuggestion(
  144. 'test label',
  145. new \CMText\RichContent\Common\ViewLocationDynamic(
  146. 'CM HQ',
  147. 'Konijnenberg 30, Breda',
  148. 5
  149. )
  150. );
  151. $this->assertJson( json_encode($viewDynamicLocation) );
  152. $this->assertInstanceOf(
  153. \CMText\RichContent\Suggestions\ViewLocationSuggestion::class,
  154. $viewDynamicLocation
  155. );
  156. $json = json_decode( json_encode($viewDynamicLocation) );
  157. $this->assertObjectHasAttribute(
  158. 'viewLocation',
  159. $json
  160. );
  161. $this->assertObjectHasAttribute(
  162. 'label',
  163. $json->viewLocation
  164. );
  165. $this->assertObjectHasAttribute(
  166. 'searchQuery',
  167. $json->viewLocation
  168. );
  169. $this->assertObjectHasAttribute(
  170. 'radius',
  171. $json->viewLocation
  172. );
  173. }
  174. public function testApplySuggestionToRichContent()
  175. {
  176. try {
  177. $richContent = new \CMText\RichContent\RichContent();
  178. $richContent->AddSuggestion(
  179. new \CMText\RichContent\Suggestions\DialSuggestion(
  180. 'test Label',
  181. '+334455667788'
  182. )
  183. );
  184. $this->assertInstanceOf(
  185. \CMText\RichContent\RichContent::class,
  186. $richContent
  187. );
  188. $this->assertJson( json_encode($richContent) );
  189. $json = json_decode( json_encode($richContent) );
  190. $this->assertCount(
  191. 1,
  192. $json->suggestions
  193. );
  194. // add too many Conversation parts
  195. for($i = 0; $i < \CMText\RichContent\RichContent::SUGGESTIONS_LENGTH_LIMIT; ++$i){
  196. $richContent->AddSuggestion(
  197. new \CMText\RichContent\Suggestions\DialSuggestion(
  198. 'test Label',
  199. '+334455667788'
  200. )
  201. );
  202. }
  203. }catch (\CMText\Exceptions\SuggestionsLimitException $suggestionsLengthException){
  204. $this->assertInstanceOf(
  205. \CMText\Exceptions\SuggestionsLimitException::class,
  206. $suggestionsLengthException
  207. );
  208. }
  209. }
  210. }