C2dmTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/C2dm.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_C2dm
  34. */
  35. class Zend_Mobile_Push_C2dmTest extends PHPUnit_Framework_TestCase
  36. {
  37. public function setUp()
  38. {
  39. $this->adapter = new Zend_Http_Client_Adapter_Test();
  40. $this->client = new Zend_Http_Client();
  41. $this->client->setAdapter($this->adapter);
  42. $this->c2dm = new Zend_Mobile_Push_C2dm();
  43. $this->c2dm->setLoginToken('testing');
  44. $this->c2dm->setHttpClient($this->client);
  45. $this->message = new Zend_Mobile_Push_Message_C2dm();
  46. $this->message->setId('testing');
  47. $this->message->setToken('testing');
  48. $this->message->addData('testKey', 'testValue');
  49. }
  50. /**
  51. * @expectedException Zend_Mobile_Push_Exception
  52. */
  53. public function testSetLoginTokenThrowsExceptionOnNonString()
  54. {
  55. $this->c2dm->setLoginToken(array());
  56. }
  57. public function testSetLoginToken()
  58. {
  59. $loginToken = 'a-login-token';
  60. $this->c2dm->setLoginToken($loginToken);
  61. $this->assertEquals($loginToken, $this->c2dm->getLoginToken());
  62. }
  63. public function testGetHttpClientReturnsDefault()
  64. {
  65. $c2dm = new Zend_Mobile_Push_C2dm();
  66. $this->assertEquals('Zend_Http_Client', get_class($c2dm->getHttpClient()));
  67. $this->assertTrue($c2dm->getHttpClient() instanceof Zend_Http_Client);
  68. }
  69. public function testSetHttpClient()
  70. {
  71. $client = new Zend_Http_Client();
  72. $this->c2dm->setHttpClient($client);
  73. $this->assertEquals($client, $this->c2dm->getHttpClient());
  74. }
  75. /**
  76. * @expectedException Zend_Mobile_Push_Exception
  77. */
  78. public function testSendThrowsExceptionWithNonValidMessage()
  79. {
  80. $msg = new Zend_Mobile_Push_Message_C2dm();
  81. $this->c2dm->send($msg);
  82. }
  83. /**
  84. * @expectedException Zend_Mobile_Push_Exception_ServerUnavailable
  85. */
  86. public function testSendThrowsExceptionWhenServerUnavailable()
  87. {
  88. $this->adapter->setResponse('HTTP/1.1 500 Internal Server Error' . "\r\n\r\n");
  89. $this->c2dm->send($this->message);
  90. }
  91. /**
  92. * @expectedException Zend_Mobile_Push_Exception_InvalidAuthToken
  93. */
  94. public function testSendThrowsExceptionWhenInvalidAuthToken()
  95. {
  96. $this->adapter->setResponse('HTTP/1.1 401 Unauthorized' . "\r\n\r\n");
  97. $this->c2dm->send($this->message);
  98. }
  99. /**
  100. * @expectedException Zend_Mobile_Push_Exception_QuotaExceeded
  101. */
  102. public function testSendThrowsExceptionWhenQuotaExceeded()
  103. {
  104. $this->adapter->setResponse(
  105. 'HTTP/1.1 200 OK' . "\r\n" .
  106. 'Context-Type: text/html' . "\r\n\r\n" .
  107. 'Error=QuotaExceeded'
  108. );
  109. $this->c2dm->send($this->message);
  110. }
  111. /**
  112. * @expectedException Zend_Mobile_Push_Exception_DeviceQuotaExceeded
  113. */
  114. public function testSendThrowsExceptionWhenDeviceQuotaExceeded()
  115. {
  116. $this->adapter->setResponse(
  117. 'HTTP/1.1 200 OK' . "\r\n" .
  118. 'Context-Type: text/html' . "\r\n\r\n" .
  119. 'Error=DeviceQuotaExceeded'
  120. );
  121. $this->c2dm->send($this->message);
  122. }
  123. /**
  124. * @expectedException Zend_Mobile_Push_Exception_InvalidToken
  125. */
  126. public function testSendThrowsExceptionWhenMissingRegistration()
  127. {
  128. $this->adapter->setResponse(
  129. 'HTTP/1.1 200 OK' . "\r\n" .
  130. 'Context-Type: text/html' . "\r\n\r\n" .
  131. 'Error=MissingRegistration'
  132. );
  133. $this->c2dm->send($this->message);
  134. }
  135. /**
  136. * @expectedException Zend_Mobile_Push_Exception_InvalidToken
  137. */
  138. public function testSendThrowsExceptionWhenInvalidRegistration()
  139. {
  140. $this->adapter->setResponse(
  141. 'HTTP/1.1 200 OK' . "\r\n" .
  142. 'Context-Type: text/html' . "\r\n\r\n" .
  143. 'Error=InvalidRegistration'
  144. );
  145. $this->c2dm->send($this->message);
  146. }
  147. /**
  148. * @expectedException Zend_Mobile_Push_Exception_InvalidToken
  149. */
  150. public function testSendThrowsExceptionWhenMismatchSenderId()
  151. {
  152. $this->adapter->setResponse(
  153. 'HTTP/1.1 200 OK' . "\r\n" .
  154. 'Context-Type: text/html' . "\r\n\r\n" .
  155. 'Error=MismatchSenderId'
  156. );
  157. $this->c2dm->send($this->message);
  158. }
  159. /**
  160. * @expectedException Zend_Mobile_Push_Exception_InvalidToken
  161. */
  162. public function testSendThrowsExceptionWhenNotRegistered()
  163. {
  164. $this->adapter->setResponse(
  165. 'HTTP/1.1 200 OK' . "\r\n" .
  166. 'Context-Type: text/html' . "\r\n\r\n" .
  167. 'Error=NotRegistered'
  168. );
  169. $this->c2dm->send($this->message);
  170. }
  171. /**
  172. * @expectedException Zend_Mobile_Push_Exception_InvalidPayload
  173. */
  174. public function testSendThrowsExceptionWhenMessageTooBig()
  175. {
  176. $this->adapter->setResponse(
  177. 'HTTP/1.1 200 OK' . "\r\n" .
  178. 'Context-Type: text/html' . "\r\n\r\n" .
  179. 'Error=MessageTooBig'
  180. );
  181. $this->c2dm->send($this->message);
  182. }
  183. /**
  184. * @expectedException Zend_Mobile_Push_Exception_InvalidTopic
  185. */
  186. public function testSendThrowsExceptionWhenMissingCollapseKey()
  187. {
  188. $this->adapter->setResponse(
  189. 'HTTP/1.1 200 OK' . "\r\n" .
  190. 'Context-Type: text/html' . "\r\n\r\n" .
  191. 'Error=MissingCollapseKey'
  192. );
  193. $this->c2dm->send($this->message);
  194. }
  195. /**
  196. * @expectedException Zend_Mobile_Push_Exception
  197. */
  198. public function testSendThrowsExceptionWhenUnknownError()
  199. {
  200. $this->adapter->setResponse(
  201. 'HTTP/1.1 200 OK' . "\r\n" .
  202. 'Context-Type: text/html' . "\r\n\r\n" .
  203. 'Error=somethinghappened'
  204. );
  205. $this->c2dm->send($this->message);
  206. }
  207. }