GcmTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mobile
  17. * @subpackage Push
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. require_once 'Zend/Mobile/Push/Gcm.php';
  23. require_once 'Zend/Http/Client.php';
  24. require_once 'Zend/Http/Client/Adapter/Test.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Mobile
  28. * @subpackage Push
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @group Zend_Mobile
  32. * @group Zend_Mobile_Push
  33. * @group Zend_Mobile_Push_Gcm
  34. */
  35. class Zend_Mobile_Push_gcmTest extends PHPUnit_Framework_TestCase
  36. {
  37. protected function _createJSONResponse($id, $success, $failure, $ids, $results)
  38. {
  39. return json_encode(array(
  40. 'multicast_id' => $id,
  41. 'success' => $success,
  42. 'failure' => $failure,
  43. 'canonical_ids' => $ids,
  44. 'results' => $results
  45. ));
  46. }
  47. public function setUp()
  48. {
  49. $this->adapter = new Zend_Http_Client_Adapter_Test();
  50. $this->client = new Zend_Http_Client();
  51. $this->client->setAdapter($this->adapter);
  52. $this->gcm = new Zend_Mobile_Push_Gcm();
  53. $this->gcm->setApiKey('testing');
  54. $this->gcm->setHttpClient($this->client);
  55. $this->message = new Zend_Mobile_Push_Message_Gcm();
  56. $this->message->addToken('testing');
  57. $this->message->addData('testKey', 'testValue');
  58. }
  59. /**
  60. * @expectedException Zend_Mobile_Push_Exception
  61. */
  62. public function testSetApiKeyThrowsExceptionOnNonString()
  63. {
  64. $this->gcm->setApiKey(array());
  65. }
  66. public function testSetApiKey()
  67. {
  68. $key = 'a-login-token';
  69. $this->gcm->setApiKey($key);
  70. $this->assertEquals($key, $this->gcm->getApiKey());
  71. }
  72. public function testGetHttpClientReturnsDefault()
  73. {
  74. $gcm = new Zend_Mobile_Push_gcm();
  75. $this->assertEquals('Zend_Http_Client', get_class($gcm->getHttpClient()));
  76. $this->assertTrue($gcm->getHttpClient() instanceof Zend_Http_Client);
  77. }
  78. public function testSetHttpClient()
  79. {
  80. $client = new Zend_Http_Client();
  81. $this->gcm->setHttpClient($client);
  82. $this->assertEquals($client, $this->gcm->getHttpClient());
  83. }
  84. /**
  85. * @expectedException Zend_Mobile_Push_Exception
  86. */
  87. public function testSendThrowsExceptionWithNonValidMessage()
  88. {
  89. $msg = new Zend_Mobile_Push_Message_Gcm();
  90. $this->gcm->send($msg);
  91. }
  92. /**
  93. * @expectedException Zend_Mobile_Push_Exception
  94. */
  95. public function testSendThrowsExceptionWithTtlNoId()
  96. {
  97. $msg = $this->message;
  98. $msg->setTtl(300);
  99. $this->gcm->send($msg);
  100. }
  101. /**
  102. * @expectedException Zend_Mobile_Push_Exception_ServerUnavailable
  103. */
  104. public function testSendThrowsExceptionWhenServerUnavailable()
  105. {
  106. $this->adapter->setResponse('HTTP/1.1 500 Internal Server Error' . "\r\n\r\n");
  107. $this->gcm->send($this->message);
  108. }
  109. /**
  110. * @expectedException Zend_Mobile_Push_Exception_InvalidAuthToken
  111. */
  112. public function testSendThrowsExceptionWhenInvalidAuthToken()
  113. {
  114. $this->adapter->setResponse('HTTP/1.1 401 Unauthorized' . "\r\n\r\n");
  115. $this->gcm->send($this->message);
  116. }
  117. /**
  118. * @expectedException Zend_Mobile_Push_Exception_InvalidPayload
  119. */
  120. public function testSendThrowsExceptionWhenInvalidPayload()
  121. {
  122. $this->adapter->setResponse('HTTP/1.1 400 Bad Request' . "\r\n\r\n");
  123. $this->gcm->send($this->message);
  124. }
  125. public function testSendResultInvalidRegistrationId()
  126. {
  127. $body = $this->_createJSONResponse(101, 0, 1, 0, array(array('error' => 'InvalidRegistration')));
  128. $this->adapter->setResponse(
  129. 'HTTP/1.1 200 OK' . "\r\n" .
  130. 'Context-Type: text/html' . "\r\n\r\n" .
  131. $body
  132. );
  133. $response = $this->gcm->send($this->message);
  134. $result = $response->getResults();
  135. $result = array_shift($result);
  136. $this->assertEquals('InvalidRegistration', $result['error']);
  137. $this->assertEquals(0, $response->getSuccessCount());
  138. $this->assertEquals(0, $response->getCanonicalCount());
  139. $this->assertEquals(1, $response->getFailureCount());
  140. }
  141. public function testSendResultMismatchSenderId()
  142. {
  143. $body = $this->_createJSONResponse(101, 0, 1, 0, array(array('error' => 'MismatchSenderId')));
  144. $this->adapter->setResponse(
  145. 'HTTP/1.1 200 OK' . "\r\n" .
  146. 'Context-Type: text/html' . "\r\n\r\n" .
  147. $body
  148. );
  149. $response = $this->gcm->send($this->message);
  150. $result = $response->getResults();
  151. $result = array_shift($result);
  152. $this->assertEquals('MismatchSenderId', $result['error']);
  153. $this->assertEquals(0, $response->getSuccessCount());
  154. $this->assertEquals(0, $response->getCanonicalCount());
  155. $this->assertEquals(1, $response->getFailureCount());
  156. }
  157. public function testSendResultNotRegistered()
  158. {
  159. $body = $this->_createJSONResponse(101, 0, 1, 0, array(array('error' => 'NotRegistered')));
  160. $this->adapter->setResponse(
  161. 'HTTP/1.1 200 OK' . "\r\n" .
  162. 'Context-Type: text/html' . "\r\n\r\n" .
  163. $body
  164. );
  165. $response = $this->gcm->send($this->message);
  166. $result = $response->getResults();
  167. $result = array_shift($result);
  168. $this->assertEquals('NotRegistered', $result['error']);
  169. $this->assertEquals(0, $response->getSuccessCount());
  170. $this->assertEquals(0, $response->getCanonicalCount());
  171. $this->assertEquals(1, $response->getFailureCount());
  172. }
  173. public function testSendResultMessageTooBig()
  174. {
  175. $body = $this->_createJSONResponse(101, 0, 1, 0, array(array('error' => 'MessageTooBig')));
  176. $this->adapter->setResponse(
  177. 'HTTP/1.1 200 OK' . "\r\n" .
  178. 'Context-Type: text/html' . "\r\n\r\n" .
  179. $body
  180. );
  181. $response = $this->gcm->send($this->message);
  182. $result = $response->getResults();
  183. $result = array_shift($result);
  184. $this->assertEquals('MessageTooBig', $result['error']);
  185. $this->assertEquals(0, $response->getSuccessCount());
  186. $this->assertEquals(0, $response->getCanonicalCount());
  187. $this->assertEquals(1, $response->getFailureCount());
  188. }
  189. public function testSendResultSuccessful()
  190. {
  191. $body = $this->_createJSONResponse(101, 1, 0, 0, array(array('message_id' => '1:2342')));
  192. $this->adapter->setResponse(
  193. 'HTTP/1.1 200 OK' . "\r\n" .
  194. 'Context-Type: text/html' . "\r\n\r\n" .
  195. $body
  196. );
  197. $response = $this->gcm->send($this->message);
  198. $result = $response->getResults();
  199. $result = array_shift($result);
  200. $this->assertEquals('1:2342', $result['message_id']);
  201. $this->assertEquals(1, $response->getSuccessCount());
  202. $this->assertEquals(0, $response->getCanonicalCount());
  203. $this->assertEquals(0, $response->getFailureCount());
  204. }
  205. public function testSendResultSuccessfulWithRegistrationId()
  206. {
  207. $body = $this->_createJSONResponse(101, 1, 0, 1, array(array('message_id' => '1:2342', 'registration_id' => 'testfoo')));
  208. $this->adapter->setResponse(
  209. 'HTTP/1.1 200 OK' . "\r\n" .
  210. 'Context-Type: text/html' . "\r\n\r\n" .
  211. $body
  212. );
  213. $response = $this->gcm->send($this->message);
  214. $result = $response->getResults();
  215. $result = array_shift($result);
  216. $this->assertEquals('1:2342', $result['message_id']);
  217. $this->assertEquals('testfoo', $result['registration_id']);
  218. $this->assertEquals(1, $response->getSuccessCount());
  219. $this->assertEquals(1, $response->getCanonicalCount());
  220. $this->assertEquals(0, $response->getFailureCount());
  221. }
  222. }