RichMessageTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. require __DIR__ .'/../vendor/autoload.php';
  3. use PHPUnit\Framework\TestCase;
  4. class RichMessageTest extends TestCase
  5. {
  6. /**
  7. * Result of initializing the TextMessage.
  8. */
  9. public function testInitializeText()
  10. {
  11. $testText = 'test rich text';
  12. try {
  13. $richText = new \CMText\RichContent\Messages\TextMessage($testText);
  14. }catch (\Exception $e){
  15. $richText = null;
  16. }
  17. $this->assertInstanceOf(
  18. \CMText\RichContent\Messages\TextMessage::class,
  19. $richText
  20. );
  21. $this->assertJson( json_encode($richText) );
  22. $json = $richText->jsonSerialize();
  23. $this->assertObjectHasAttribute(
  24. 'text',
  25. $json
  26. );
  27. }
  28. public function testInitializeCard()
  29. {
  30. $text = 'test rich card text';
  31. $header = 'test rich card header text';
  32. $name = 'test media name';
  33. $uri = 'test://uri';
  34. $mimetype = 'test/mimetype';
  35. $media = new \CMText\RichContent\Messages\MediaContent(
  36. $name,
  37. $uri,
  38. $mimetype
  39. );
  40. $richCard = new \CMText\RichContent\Messages\CardMessage(
  41. $text,
  42. $header,
  43. $media
  44. );
  45. $this->assertInstanceOf(
  46. \CMText\RichContent\Messages\MediaContent::class,
  47. $media
  48. );
  49. $this->assertInstanceOf(
  50. \CMText\RichContent\Messages\CardMessage::class,
  51. $richCard
  52. );
  53. $this->assertJson( json_encode($richCard) );
  54. $json = json_decode( json_encode($richCard) );
  55. $this->assertObjectHasAttribute(
  56. 'text',
  57. $json
  58. );
  59. $this->assertObjectHasAttribute(
  60. 'header',
  61. $json
  62. );
  63. $this->assertObjectHasAttribute(
  64. 'media',
  65. $json
  66. );
  67. $this->assertObjectHasAttribute(
  68. 'mediaName',
  69. $json->media
  70. );
  71. $this->assertObjectHasAttribute(
  72. 'mediaUri',
  73. $json->media
  74. );
  75. $this->assertObjectHasAttribute(
  76. 'mimeType',
  77. $json->media
  78. );
  79. }
  80. public function testInitializeCarousel()
  81. {
  82. $carousel = new \CMText\RichContent\Messages\CarouselMessage(
  83. \CMText\RichContent\Messages\CarouselCardWidth::MEDIUM,
  84. [
  85. new \CMText\RichContent\Messages\CardMessage(
  86. 'test rich text',
  87. 'test rich header text',
  88. new \CMText\RichContent\Messages\MediaContent(
  89. 'test name',
  90. 'test://uri',
  91. 'test/mimetype'
  92. )
  93. ),
  94. new \CMText\RichContent\Messages\CardMessage(
  95. 'test rich text',
  96. 'test rich header text',
  97. new \CMText\RichContent\Messages\MediaContent(
  98. 'test name',
  99. 'test://uri',
  100. 'test/mimetype'
  101. )
  102. ),
  103. ]
  104. );
  105. $this->assertInstanceOf(
  106. \CMText\RichContent\Messages\CarouselMessage::class,
  107. $carousel
  108. );
  109. $this->assertJson( json_encode($carousel) );
  110. $json = json_decode( json_encode($carousel) );
  111. $this->assertObjectHasAttribute(
  112. 'carousel',
  113. $json
  114. );
  115. $this->assertObjectHasAttribute(
  116. 'cardWidth',
  117. $json->carousel
  118. );
  119. $this->assertEquals(
  120. \CMText\RichContent\Messages\CarouselCardWidth::MEDIUM,
  121. $json->carousel->cardWidth
  122. );
  123. $this->assertObjectHasAttribute(
  124. 'cards',
  125. $json->carousel
  126. );
  127. $this->assertCount(
  128. 2,
  129. $json->carousel->cards
  130. );
  131. }
  132. public function testInitializeMedia()
  133. {
  134. $media = new \CMText\RichContent\Messages\MediaMessage(
  135. 'test name',
  136. 'test://uri',
  137. 'test/mimetype'
  138. );
  139. $this->assertInstanceOf(
  140. \CMText\RichContent\Messages\MediaMessage::class,
  141. $media
  142. );
  143. $this->assertJson( json_encode($media) );
  144. $json = json_decode( json_encode($media) );
  145. $this->assertObjectHasAttribute(
  146. 'media',
  147. $json
  148. );
  149. $this->assertObjectHasAttribute(
  150. 'mediaName',
  151. $json->media
  152. );
  153. $this->assertObjectHasAttribute(
  154. 'mediaUri',
  155. $json->media
  156. );
  157. $this->assertObjectHasAttribute(
  158. 'mimeType',
  159. $json->media
  160. );
  161. }
  162. public function testApplyMessageToRichContent()
  163. {
  164. try {
  165. $richContent = new \CMText\RichContent\RichContent();
  166. $richContent->AddConversationPart(
  167. new \CMText\RichContent\Messages\TextMessage('test rich text')
  168. );
  169. $this->assertInstanceOf(
  170. \CMText\RichContent\RichContent::class,
  171. $richContent
  172. );
  173. $this->assertJson( json_encode($richContent) );
  174. $json = json_decode( json_encode($richContent) );
  175. $this->assertCount(
  176. 1,
  177. $json->conversation
  178. );
  179. // add too many Conversation parts
  180. for($i = 0; $i < \CMText\RichContent\RichContent::CONVERSATION_LENGTH_LIMIT; ++$i){
  181. $richContent->AddConversationPart(
  182. new \CMText\RichContent\Messages\TextMessage('test rich text')
  183. );
  184. }
  185. }catch (\CMText\Exceptions\ConversationLimitException $conversationLengthException){
  186. $this->assertInstanceOf(
  187. \CMText\Exceptions\ConversationLimitException::class,
  188. $conversationLengthException
  189. );
  190. }
  191. }
  192. public function testInitializeLocationPush()
  193. {
  194. $dynamicLocation = new \CMText\RichContent\Common\ViewLocationDynamic(
  195. 'CM HQ',
  196. 'Konijnenberg 30, Breda'
  197. );
  198. $this->assertInstanceOf(
  199. \CMText\RichContent\Common\ViewLocationBase::class,
  200. $dynamicLocation
  201. );
  202. $location = new \CMText\RichContent\Messages\LocationPushMessage( $dynamicLocation );
  203. $this->assertJson( json_encode($location) );
  204. $json = json_decode( json_encode($location) );
  205. $this->assertObjectHasAttribute(
  206. 'location',
  207. $json
  208. );
  209. $this->assertObjectHasAttribute(
  210. 'label',
  211. $json->location
  212. );
  213. $this->assertObjectHasAttribute(
  214. 'searchQuery',
  215. $json->location
  216. );
  217. $this->assertObjectNotHasAttribute(
  218. 'radius',
  219. $json->location
  220. );
  221. $staticLocation = new CMText\RichContent\Common\ViewLocationStatic(
  222. 'CM HQ',
  223. '51.603802',
  224. '4.770821',
  225. 3
  226. );
  227. $this->assertInstanceOf(
  228. \CMText\RichContent\Common\ViewLocationBase::class,
  229. $staticLocation
  230. );
  231. $location = new CMText\RichContent\Messages\LocationPushMessage( $staticLocation );
  232. $this->assertJson( json_encode($location) );
  233. $json = json_decode( json_encode($location) );
  234. $this->assertObjectHasAttribute(
  235. 'location',
  236. $json
  237. );
  238. $this->assertObjectHasAttribute(
  239. 'latitude',
  240. $json->location
  241. );
  242. $this->assertObjectHasAttribute(
  243. 'longitude',
  244. $json->location
  245. );
  246. $this->assertObjectHasAttribute(
  247. 'label',
  248. $json->location
  249. );
  250. $this->assertObjectHasAttribute(
  251. 'radius',
  252. $json->location
  253. );
  254. }
  255. public function testInitializeContactsMessage()
  256. {
  257. // set up all the Contact properties
  258. $contactProperties['address'] = new \CMText\RichContent\Common\ContactAddress('Breda', 'Netherlands', 'NL');
  259. $contactProperties['birthday'] = new \CMText\RichContent\Common\ContactBirthday( new DateTime() );
  260. $contactProperties['email'] = new \CMText\RichContent\Common\ContactEmail('info@cm.com');
  261. $contactProperties['name'] = new \CMText\RichContent\Common\ContactName('CM.com Be part of it.');
  262. $contactProperties['organization'] = new \CMText\RichContent\Common\ContactOrganization('CM.com', 'Development');
  263. $contactProperties['phonenumber'] = new \CMText\RichContent\Common\ContactPhonenumber('+31765727000');
  264. $contactProperties['url'] = new \CMText\RichContent\Common\ContactUrl('https://cm.com');
  265. // test full initialization
  266. $contact = new \CMText\RichContent\Common\Contact(
  267. $contactProperties['address'],
  268. $contactProperties['birthday'],
  269. $contactProperties['email'],
  270. $contactProperties['name'],
  271. $contactProperties['organization'],
  272. $contactProperties['phonenumber'],
  273. $contactProperties['url']
  274. );
  275. $contactsMessage = new \CMText\RichContent\Messages\ContactsMessage($contact);
  276. $this->assertInstanceOf(
  277. \CMText\RichContent\Messages\ContactsMessage::class,
  278. $contactsMessage
  279. );
  280. $contactsMessage->addContact($contact);
  281. $this->assertJson( json_encode($contactsMessage) );
  282. $json = json_decode(json_encode($contactsMessage));
  283. $this->assertObjectHasAttribute(
  284. 'contacts',
  285. $json
  286. );
  287. $this->assertCount(
  288. 2,
  289. $json->contacts
  290. );
  291. $this->assertObjectHasAttribute(
  292. 'org',
  293. $json->contacts[0]
  294. );
  295. }
  296. }