MessageTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace tests;
  3. class MessageTest extends Base
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. }
  9. public function testMessageSendText()
  10. {
  11. $randomFromUsername = Utils::randomUserName();
  12. $randomPassword = Utils::randomPassword();
  13. $randomToUsername = Utils::randomUserName();
  14. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $randomFromUsername, 'password' => $randomPassword), array('username' => $randomToUsername, 'password' => $randomPassword))));
  15. $this->assertArrayNotHasKey('code', $this->message->text('users', array($randomToUsername), array('msg' => 'hello', 'ext' => array('ext1' => 'val1')), $randomFromUsername));
  16. $this->assertTrue($this->user->delete($randomFromUsername));
  17. $this->assertTrue($this->user->delete($randomToUsername));
  18. }
  19. public function testMessageSendImage()
  20. {
  21. $randomFromUsername = Utils::randomUserName();
  22. $randomPassword = Utils::randomPassword();
  23. $randomToUsername = Utils::randomUserName();
  24. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $randomFromUsername, 'password' => $randomPassword), array('username' => $randomToUsername, 'password' => $randomPassword))));
  25. $data = $this->attachment->uploadFile(dirname(__FILE__).'/assets/1.png');
  26. $this->assertArrayHasKey('uuid', $data);
  27. $msg = array(
  28. 'filename' => '1.png',
  29. 'uuid' => $data['uuid'],
  30. 'secret' => $data['share-secret'],
  31. 'size' => array(
  32. 'width' => 36,
  33. 'height' => 36,
  34. ),
  35. );
  36. $this->assertArrayNotHasKey('code', $this->message->image('users', array($randomToUsername), $msg, $randomFromUsername));
  37. $this->assertTrue($this->user->delete($randomFromUsername));
  38. $this->assertTrue($this->user->delete($randomToUsername));
  39. }
  40. public function testMessageSendVoice()
  41. {
  42. $randomFromUsername = Utils::randomUserName();
  43. $randomPassword = Utils::randomPassword();
  44. $randomToUsername = Utils::randomUserName();
  45. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $randomFromUsername, 'password' => $randomPassword), array('username' => $randomToUsername, 'password' => $randomPassword))));
  46. $data = $this->attachment->uploadFile(dirname(__FILE__).'/assets/mario.amr');
  47. $this->assertArrayHasKey('uuid', $data);
  48. $msg = array(
  49. 'filename' => 'mario.amr',
  50. 'uuid' => $data['uuid'],
  51. 'secret' => $data['share-secret'],
  52. 'length' => 89,
  53. );
  54. $this->assertArrayNotHasKey('code', $this->message->audio('users', array($randomToUsername), $msg, $randomFromUsername));
  55. $this->assertTrue($this->user->delete($randomFromUsername));
  56. $this->assertTrue($this->user->delete($randomToUsername));
  57. }
  58. public function testMessageSendVideo()
  59. {
  60. $randomFromUsername = Utils::randomUserName();
  61. $randomPassword = Utils::randomPassword();
  62. $randomToUsername = Utils::randomUserName();
  63. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $randomFromUsername, 'password' => $randomPassword), array('username' => $randomToUsername, 'password' => $randomPassword))));
  64. $movie = dirname(__FILE__).'/assets/movie.ogg';
  65. $data = $this->attachment->uploadFile($movie);
  66. $this->assertArrayHasKey('uuid', $data);
  67. $thumb = $this->attachment->uploadFile(dirname(__FILE__).'/assets/1.png');
  68. $this->assertArrayHasKey('uuid', $thumb);
  69. $msg = array(
  70. 'filename' => 'movie.ogg', // 视频文件名称
  71. 'uuid' => $data['uuid'], // 成功上传视频文件返回的UUID
  72. 'secret' => $data['share-secret'], // 成功上传视频文件后返回的secret
  73. 'thumb_uuid' => $thumb['uuid'], // 成功上传视频缩略图返回的 UUID
  74. 'thumb_secret' => $thumb['share-secret'], // 成功上传视频缩略图后返回的secret
  75. 'length' => 3, // 视频播放长度
  76. 'file_length' => filesize($movie), // 视频文件大小(单位:字节)
  77. );
  78. $this->assertArrayNotHasKey('code', $this->message->video('users', array($randomToUsername), $msg, $randomFromUsername));
  79. $this->assertTrue($this->user->delete($randomFromUsername));
  80. $this->assertTrue($this->user->delete($randomToUsername));
  81. }
  82. public function testMessageSendFile()
  83. {
  84. $randomFromUsername = Utils::randomUserName();
  85. $randomPassword = Utils::randomPassword();
  86. $randomToUsername = Utils::randomUserName();
  87. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $randomFromUsername, 'password' => $randomPassword), array('username' => $randomToUsername, 'password' => $randomPassword))));
  88. $data = $this->attachment->uploadFile(dirname(__FILE__).'/assets/1.txt');
  89. $this->assertArrayHasKey('uuid', $data);
  90. $msg = array(
  91. 'filename' => '1.txt', // 文件名称
  92. 'uuid' => $data['uuid'], // 成功上传文件返回的UUID
  93. 'secret' => $data['share-secret'], // 成功上传文件后返回的secret
  94. );
  95. $this->assertArrayNotHasKey('code', $this->message->file('users', array($randomToUsername), $msg, $randomFromUsername));
  96. $this->assertTrue($this->user->delete($randomFromUsername));
  97. $this->assertTrue($this->user->delete($randomToUsername));
  98. }
  99. public function testMessageSendLocation()
  100. {
  101. $randomFromUsername = Utils::randomUserName();
  102. $randomPassword = Utils::randomPassword();
  103. $randomToUsername = Utils::randomUserName();
  104. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $randomFromUsername, 'password' => $randomPassword), array('username' => $randomToUsername, 'password' => $randomPassword))));
  105. $msg = array(
  106. 'lat' => '39.966', // 纬度
  107. 'lng' => '116.322', // 经度
  108. 'addr' => '中国北京市海淀区中关村', // 地址
  109. );
  110. $this->assertArrayNotHasKey('code', $this->message->location('users', array($randomToUsername), $msg, $randomFromUsername));
  111. $this->assertTrue($this->user->delete($randomFromUsername));
  112. $this->assertTrue($this->user->delete($randomToUsername));
  113. }
  114. public function testMessageSendCommand()
  115. {
  116. $randomFromUsername = Utils::randomUserName();
  117. $randomPassword = Utils::randomPassword();
  118. $randomToUsername = Utils::randomUserName();
  119. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $randomFromUsername, 'password' => $randomPassword), array('username' => $randomToUsername, 'password' => $randomPassword))));
  120. $msg = array(
  121. 'event' => 'notification', // 自定义键值
  122. 'id' => '123', // 自定义键值
  123. );
  124. $this->assertArrayNotHasKey('code', $this->message->cmd('users', array($randomToUsername), $msg, $randomFromUsername));
  125. $this->assertTrue($this->user->delete($randomFromUsername));
  126. $this->assertTrue($this->user->delete($randomToUsername));
  127. }
  128. public function testMessageSendCustom()
  129. {
  130. $randomFromUsername = Utils::randomUserName();
  131. $randomPassword = Utils::randomPassword();
  132. $randomToUsername = Utils::randomUserName();
  133. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $randomFromUsername, 'password' => $randomPassword), array('username' => $randomToUsername, 'password' => $randomPassword))));
  134. $msg = array(
  135. // 用户自定义的事件类型,必须是string,值必须满足正则表达式 [a-zA-Z0-9-_/\.]{1,32},最短1个字符 最长32个字符
  136. 'customEvent' => 'xxx',
  137. // 用户自定义的事件属性,类型必须是Map<String,String>,最多可以包含16个元素。customExts 是可选的,不需要可以不传
  138. 'customExts' => array(
  139. 'asd' => '123',
  140. ),
  141. );
  142. $this->assertArrayNotHasKey('code', $this->message->custom('users', array($randomToUsername), $msg, $randomFromUsername));
  143. $this->assertTrue($this->user->delete($randomFromUsername));
  144. $this->assertTrue($this->user->delete($randomToUsername));
  145. }
  146. public function testMessageSendExtension()
  147. {
  148. $randomFromUsername = Utils::randomUserName();
  149. $randomPassword = Utils::randomPassword();
  150. $randomToUsername = Utils::randomUserName();
  151. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $randomFromUsername, 'password' => $randomPassword), array('username' => $randomToUsername, 'password' => $randomPassword))));
  152. $msg = array(
  153. // 用户自定义的事件类型,必须是string,值必须满足正则表达式 [a-zA-Z0-9-_/\.]{1,32},最短1个字符 最长32个字符
  154. 'customEvent' => 'xxx',
  155. // 用户自定义的事件属性,类型必须是Map<String,String>,最多可以包含16个元素。customExts 是可选的,不需要可以不传
  156. 'customExts' => array(
  157. 'asd' => '123',
  158. ),
  159. 'ext' => array(
  160. "em_apns_ext" => array(
  161. "em_push_content" => "自定义推送显示"
  162. )
  163. )
  164. );
  165. $this->assertArrayNotHasKey('code', $this->message->custom('users', array($randomToUsername), $msg, $randomFromUsername));
  166. $this->assertTrue($this->user->delete($randomFromUsername));
  167. $this->assertTrue($this->user->delete($randomToUsername));
  168. }
  169. }