DevicePayloadTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace JPush\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. class DevicePayloadTest extends TestCase {
  5. protected function setUp() {
  6. global $client;
  7. $this->device = $client->device();
  8. $this->test_tag = 'jpush_tag';
  9. }
  10. function testGetDevices() {
  11. global $registration_id;
  12. $response = $this->device->getDevices($registration_id);
  13. $this->assertEquals('200', $response['http_code']);
  14. echo "HTTP HEADERS ARE: ";
  15. print_r($response['headers']);
  16. $body = $response['body'];
  17. $this->assertTrue(is_array($body));
  18. $this->assertEquals(3, count($body));
  19. $this->assertArrayHasKey('tags', $body);
  20. $this->assertArrayHasKey('alias', $body);
  21. $this->assertArrayHasKey('mobile', $body);
  22. $this->assertTrue(is_array($body['tags']));
  23. }
  24. /**
  25. * @expectedException \JPush\Exceptions\APIRequestException
  26. * @expectedExceptionCode 7002
  27. */
  28. function testGetDevicesWithInvalidRid() {
  29. $response = $this->device->getDevices('INVALID_REGISTRATION_ID');
  30. }
  31. function testUpdateDevicesAlias() {
  32. global $registration_id;
  33. $result = $this->device->getDevices($registration_id);
  34. $old_alias = $result['body']['alias'];
  35. if ($old_alias == null) {
  36. $old_alias = '';
  37. }
  38. $new_alias = 'jpush_alias';
  39. if ($old_alias == $new_alias) {
  40. $new_alias = $new_alias . time();
  41. }
  42. $response = $this->device->updateAlias($registration_id, $new_alias);
  43. $this->assertEquals('200', $response['http_code']);
  44. $response = $this->device->updateAlias($registration_id, $old_alias);
  45. $this->assertEquals('200', $response['http_code']);
  46. }
  47. function testUpdateDevicesTags() {
  48. global $registration_id;
  49. $new_tag = $this->test_tag;
  50. $response = $this->device->addTags($registration_id, array($new_tag));
  51. $this->assertEquals('200', $response['http_code']);
  52. $response = $this->device->removeTags($registration_id, array($new_tag));
  53. $this->assertEquals('200', $response['http_code']);
  54. }
  55. function testGetTags() {
  56. $response = $this->device->getTags();
  57. $this->assertEquals('200', $response['http_code']);
  58. $body = $response['body'];
  59. $this->assertTrue(is_array($body));
  60. $this->assertEquals(1, count($body));
  61. $this->assertArrayHasKey('tags', $body);
  62. }
  63. function testIsDeviceInTag() {
  64. global $registration_id;
  65. $test_tag = $this->test_tag;
  66. $this->device->addTags($registration_id, array($test_tag));
  67. $response = $this->device->isDeviceInTag($registration_id, $test_tag);
  68. $this->assertEquals('200', $response['http_code']);
  69. $body = $response['body'];
  70. $this->assertTrue(is_array($body));
  71. $this->assertTrue($body['result']);
  72. $this->device->removeTags($registration_id, array($test_tag));
  73. $response = $this->device->isDeviceInTag($registration_id, $test_tag);
  74. $this->assertEquals('200', $response['http_code']);
  75. $body = $response['body'];
  76. $this->assertTrue(is_array($body));
  77. $this->assertFalse($body['result']);
  78. }
  79. function testUpdateTag() {
  80. global $registration_id;
  81. $test_tag = $this->test_tag;
  82. $response = $this->device->addDevicesToTag($test_tag, array($registration_id));
  83. $this->assertEquals('200', $response['http_code']);
  84. $response = $this->device->removeDevicesFromTag($test_tag, array($registration_id));
  85. $this->assertEquals('200', $response['http_code']);
  86. }
  87. function testDeleteTag() {}
  88. function testGetAliasDevices() {
  89. $test_tag = $this->test_tag;
  90. $response = $this->device->getAliasDevices($test_tag);
  91. $this->assertEquals('200', $response['http_code']);
  92. $body = $response['body'];
  93. $this->assertTrue(is_array($body));
  94. $this->assertEquals(1, count($body));
  95. $this->assertArrayHasKey('registration_ids', $body);
  96. }
  97. function testDeleteAlias() {}
  98. function testGetDevicesStatus() {
  99. global $registration_id;
  100. $response = $this->device->getDevicesStatus($registration_id);
  101. $this->assertEquals('200', $response['http_code']);
  102. $body = $response['body'];
  103. $this->assertTrue(is_array($body));
  104. $this->assertEquals(1, count($body));
  105. }
  106. }