PushPayloadTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace JPush\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. class PushPayloadTest extends TestCase {
  5. protected function setUp() {
  6. global $client;
  7. $this->payload = $client->push()
  8. ->setPlatform('all')
  9. ->addAllAudience()
  10. ->setNotificationAlert('Hello JPush');
  11. $this->payload_without_audience = $client->push()
  12. ->setPlatform('all')
  13. ->setNotificationAlert('Hello JPush');
  14. }
  15. public function testSimplePushToAll() {
  16. $payload = $this->payload;
  17. $result = $payload->build();
  18. $this->assertTrue(is_array($result));
  19. $this->assertEquals(4, count($result));
  20. $this->assertArrayHasKey('platform', $result);
  21. $this->assertArrayHasKey('audience', $result);
  22. $this->assertArrayHasKey('notification', $result);
  23. $this->assertArrayHasKey('options', $result);
  24. }
  25. public function testSetPlatform() {
  26. $payload = $this->payload;
  27. $result = $payload->build();
  28. $this->assertEquals('all', $result['platform']);
  29. $result = $payload->setPlatform('ios')->build();
  30. $this->assertTrue(is_array($result['platform']));
  31. $this->assertEquals(1, count($result['platform']));
  32. $this->assertTrue(in_array('ios', $result['platform']));
  33. $result = $payload->setPlatform(array('ios', 'android', 'blackberry'))->build();
  34. $this->assertTrue(is_array($result['platform']));
  35. $this->assertEquals(2, count($result['platform']));
  36. $this->assertFalse(in_array('blackberry', $result['platform']));
  37. }
  38. public function testSetAudience() {
  39. $result = $this->payload->build();
  40. $this->assertEquals('all', $result['audience']);
  41. }
  42. public function testAddTag() {
  43. $payload = $this->payload_without_audience;
  44. $result = $payload->addTag('hello')->build();
  45. $audience = $result['audience'];
  46. $this->assertTrue(is_array($audience['tag']));
  47. $this->assertEquals(1, count($audience['tag']));
  48. $result = $payload->addTag(array('jpush', 'jiguang'))->build();
  49. $this->assertEquals(3, count($result['audience']['tag']));
  50. }
  51. public function testAddTag2() {
  52. $payload = $this->payload_without_audience;
  53. $result = $payload->addTagAnd(array('jpush', 'jiguang'))->build();
  54. $audience = $result['audience'];
  55. $this->assertTrue(is_array($audience['tag_and']));
  56. $this->assertEquals(2, count($audience['tag_and']));
  57. $result = $payload->addTagAnd('hello')->build();
  58. $this->assertEquals(3, count($result['audience']['tag_and']));
  59. }
  60. public function testAddTagAnd1() {
  61. $payload = $this->payload_without_audience;
  62. $result = $payload->addTagAnd('hello')->build();
  63. $audience = $result['audience'];
  64. $this->assertTrue(is_array($audience['tag_and']));
  65. $this->assertEquals(1, count($audience['tag_and']));
  66. $result = $payload->addTagAnd(array('jpush', 'jiguang'))->build();
  67. $this->assertEquals(3, count($result['audience']['tag_and']));
  68. }
  69. public function testAddTagAnd2() {
  70. $payload = $this->payload_without_audience;
  71. $result = $payload->addTagAnd(array('jpush', 'jiguang'))->build();
  72. $audience = $result['audience'];
  73. $this->assertTrue(is_array($audience['tag_and']));
  74. $this->assertEquals(2, count($audience['tag_and']));
  75. $result = $payload->addTagAnd('hello')->build();
  76. $this->assertEquals(3, count($result['audience']['tag_and']));
  77. }
  78. public function testAddRegistrationId1() {
  79. $payload = $this->payload_without_audience;
  80. $result = $payload->addRegistrationId('hello')->build();
  81. $audience = $result['audience'];
  82. $this->assertTrue(is_array($audience['registration_id']));
  83. $this->assertEquals(1, count($audience['registration_id']));
  84. $result = $payload->addRegistrationId(array('jpush', 'jiguang'))->build();
  85. $this->assertEquals(3, count($result['audience']['registration_id']));
  86. }
  87. public function testAddRegistrationId2() {
  88. $payload = $this->payload_without_audience;
  89. $result = $payload->addRegistrationId(array('jpush', 'jiguang'))->build();
  90. $audience = $result['audience'];
  91. $this->assertTrue(is_array($audience['registration_id']));
  92. $this->assertEquals(2, count($audience['registration_id']));
  93. $result = $payload->addRegistrationId('hello')->build();
  94. $this->assertEquals(3, count($result['audience']['registration_id']));
  95. }
  96. public function testSetNotificationAlert() {
  97. $result = $this->payload->build();
  98. $notification = $result['notification'];
  99. $this->assertTrue(is_array($notification));
  100. $this->assertEquals(1, count($notification));
  101. $this->assertEquals('Hello JPush', $result['notification']['alert']);
  102. }
  103. public function testIosNotification() {
  104. $payload = $this->payload;
  105. $result = $payload->iosNotification()->build();
  106. $ios = $result['notification']['ios'];
  107. $this->assertTrue(is_array($ios));
  108. $this->assertEquals(3, count($ios));
  109. $this->assertArrayHasKey('alert', $ios);
  110. $this->assertArrayHasKey('sound', $ios);
  111. $this->assertArrayHasKey('badge', $ios);
  112. $this->assertEquals('', $ios['alert']);
  113. $this->assertEquals('', $ios['sound']);
  114. $this->assertEquals('+1', $ios['badge']);
  115. $result = $payload->iosNotification('hello')->build();
  116. $ios = $result['notification']['ios'];
  117. $this->assertEquals('hello', $result['notification']['ios']['alert']);
  118. }
  119. public function testIosNotificationWithArray() {
  120. $payload = $this->payload;
  121. $alert_array = array(
  122. 'alert_k1' => 'alert_v1',
  123. 'alert_k2' => 'alert_v2',
  124. 'alert_k3' => 'alert_v3',
  125. 'alert_k4' => 'alert_v4'
  126. );
  127. $array = array(
  128. 'sound' => 'jpush.caf',
  129. 'badge' => 2,
  130. 'content-available' => true,
  131. 'category' => 'jiguang',
  132. 'extras' => array(
  133. 'key' => 'value',
  134. 'jiguang'
  135. ),
  136. 'invalid_key' => 'invalid_value'
  137. );
  138. $result = $payload->iosNotification($alert_array, $array)->build();
  139. $ios = $result['notification']['ios'];
  140. $this->assertTrue(is_array($ios['alert']));
  141. $this->assertEquals(6, count($ios));
  142. $this->assertFalse(array_key_exists('invalid_key', $ios));
  143. }
  144. public function testAndroidNotification() {
  145. $payload = $this->payload;
  146. $result = $payload->androidNotification()->build();
  147. $android = $result['notification']['android'];
  148. $this->assertTrue(is_array($android));
  149. $this->assertEquals(1, count($android));
  150. $this->assertArrayHasKey('alert', $android);
  151. $this->assertEquals('', $android['alert']);
  152. $result = $payload->androidNotification('hello')->build();
  153. $android = $result['notification']['android'];
  154. $this->assertEquals('hello', $result['notification']['android']['alert']);
  155. }
  156. public function testAndroidNotificationWithArray() {
  157. $payload = $this->payload;
  158. $array = array(
  159. 'title' => 'hello jpush',
  160. 'builder_id' => 2,
  161. 'extras' => array(
  162. 'key' => 'value',
  163. 'jiguang'
  164. ),
  165. 'invalid_key' => 'invalid_value'
  166. );
  167. $result = $payload->androidNotification('', $array)->build();
  168. $android = $result['notification']['android'];
  169. $this->assertEquals(4, count($android));
  170. $this->assertFalse(array_key_exists('invalid_key', $android));
  171. }
  172. public function testSetSmsMessage() {
  173. $payload = $this->payload;
  174. $smsMessage = array(
  175. 'delay_time' => 60,
  176. 'signid' => 154,
  177. 'temp_id' => 1,
  178. 'temp_para' => array(
  179. 'code' => 357
  180. ),
  181. 'active_filter' => false
  182. );
  183. $result = $payload->setSmsMessage($smsMessage)->build();
  184. $sms = $result['sms_message'];
  185. $this->assertTrue(is_array($sms));
  186. $this->assertEquals(5, count($sms));
  187. $this->assertEquals(60, $sms['delay_time']);
  188. $this->assertEquals(154, $sms['signid']);
  189. $this->assertEquals(1, $sms['temp_id']);
  190. }
  191. public function testMessage() {
  192. $payload = $this->payload;
  193. $result = $payload->message('Hello JPush')->build();
  194. $message = $result['message'];
  195. $this->assertTrue(is_array($message));
  196. $this->assertEquals(1, count($message));
  197. $this->assertEquals('Hello JPush', $message['msg_content']);
  198. $array = array(
  199. 'title' => 'hello jpush',
  200. 'content_type' => '',
  201. 'extras' => array(
  202. 'key' => 'value',
  203. 'jiguang'
  204. ),
  205. 'invalid_key' => 'invalid_value'
  206. );
  207. $result = $payload->message('Hello JPush', $array)->build();
  208. }
  209. public function testOptions() {
  210. $payload = $this->payload;
  211. $result = $payload->options()->build();
  212. $this->assertTrue(array_key_exists('options', $result));
  213. $this->assertEquals(false, $result['options']['apns_production']);
  214. $this->assertTrue(array_key_exists('sendno', $result['options']));
  215. $array = array(
  216. 'sendno' => 100,
  217. 'time_to_live' => 100,
  218. 'apns_production' => true,
  219. 'override_msg_id' => 100,
  220. 'big_push_duration' => 100
  221. );
  222. $result = $payload->options($array)->build();
  223. $options = $result['options'];
  224. $this->assertEquals(5, count($options));
  225. $this->assertArrayHasKey('apns_production', $options);
  226. $this->assertEquals(true, $options['apns_production']);
  227. }
  228. public function testPushToAll() {
  229. $payload = $this->payload;
  230. $platform = array('ios', 'android', 'blackberry');
  231. $ios_alert = array(
  232. 'k1' => 'v1',
  233. 'k2' => 'v2',
  234. 'k3' => 'v3',
  235. 'k4' => 'v4'
  236. );
  237. $ios_notification = array(
  238. 'sound' => 'jpush.caf',
  239. 'badge' => 2,
  240. 'content-available' => true,
  241. 'category' => 'jiguang',
  242. 'extras' => array(
  243. 'key' => 'value',
  244. 'jiguang'
  245. ),
  246. 'invalid_key' => 'invalid_value'
  247. );
  248. $android_notification = array(
  249. 'title' => 'hello jpush',
  250. 'builder_id' => 2,
  251. 'extras' => array(
  252. 'key' => 'value',
  253. 'jiguang'
  254. ),
  255. 'invalid_key' => 'invalid_value'
  256. );
  257. $message = array(
  258. 'title' => 'hello jpush',
  259. 'content_type' => '',
  260. 'extras' => array(
  261. 'key' => 'value',
  262. 'jiguang'
  263. ),
  264. 'invalid_key' => 'invalid_value'
  265. );
  266. $result = $payload->setPlatform($platform)
  267. ->iosNotification($ios_alert, $ios_notification)
  268. ->androidNotification('Hello Android', $android_notification)
  269. ->message('Hello JPush', $message)
  270. ->build();
  271. $response = $payload->send();
  272. $this->assertEquals('200', $response['http_code']);
  273. echo "HTTP HEADERS ARE: ";
  274. print_r($response['headers']);
  275. $body = $response['body'];
  276. $this->assertTrue(is_array($body));
  277. $this->assertEquals(2, count($body));
  278. $this->assertArrayHasKey('sendno', $body);
  279. $this->assertArrayHasKey('msg_id', $body);
  280. }
  281. }