MpnsTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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-2015 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/Mpns.php';
  23. require_once 'Zend/Mobile/Push/Message/Mpns/Raw.php';
  24. require_once 'Zend/Mobile/Push/Message/Mpns/Tile.php';
  25. require_once 'Zend/Mobile/Push/Message/Mpns/Toast.php';
  26. require_once 'Zend/Http/Client.php';
  27. require_once 'Zend/Http/Client/Adapter/Test.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Mobile
  31. * @subpackage Push
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Mobile
  35. * @group Zend_Mobile_Push
  36. * @group Zend_Mobile_Push_Mpns
  37. */
  38. class Zend_Mobile_Push_MpnsTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function setUp()
  41. {
  42. $this->adapter = new Zend_Http_Client_Adapter_Test();
  43. $this->client = new Zend_Http_Client();
  44. $this->client->setAdapter($this->adapter);
  45. $this->mpns = new Zend_Mobile_Push_Mpns();
  46. $this->mpns->setHttpClient($this->client);
  47. }
  48. public function getMessage($type) {
  49. switch ($type) {
  50. case 'tile':
  51. $message = new Zend_Mobile_Push_Message_Mpns_Tile();
  52. break;
  53. case 'toast':
  54. $message = new Zend_Mobile_Push_Message_Mpns_Toast();
  55. break;
  56. default:
  57. $message = new Zend_Mobile_Push_Message_Mpns_Raw();
  58. $message->setMessage('<w><oa h="" /></w>');
  59. }
  60. $message->setToken('http://this.is.a.url.com');
  61. return $message;
  62. }
  63. public function testGetHttpClientReturnsDefault()
  64. {
  65. $mpns = new Zend_Mobile_Push_Mpns();
  66. $this->assertEquals('Zend_Http_Client', get_class($mpns->getHttpClient()));
  67. $this->assertTrue($mpns->getHttpClient() instanceof Zend_Http_Client);
  68. }
  69. public function testSetHttpClient()
  70. {
  71. $client = new Zend_Http_Client();
  72. $this->mpns->setHttpClient($client);
  73. $this->assertEquals($client, $this->mpns->getHttpClient());
  74. }
  75. /**
  76. * @expectedException Zend_Mobile_Push_Exception
  77. */
  78. public function testSendThrowsExceptionWithNonValidMessage()
  79. {
  80. $msg = new Zend_Mobile_Push_Message_Mpns_Tile();
  81. $this->mpns->send($msg);
  82. }
  83. /**
  84. * @expectedException Zend_Mobile_Push_Exception_DeviceQuotaExceeded
  85. */
  86. public function testSendThrowsExceptionWhenDeviceQuotaExceeded()
  87. {
  88. $this->adapter->setResponse('HTTP/1.1 200 OK' . "\r\n" . 'NotificationStatus: QueueFull' . "\r\n\r\n");
  89. $this->mpns->send($this->getMessage('raw'));
  90. }
  91. /**
  92. * @expectedException Zend_Mobile_Push_Exception_InvalidPayload
  93. */
  94. public function testSendThrowsExceptionWhenInvalidPayload()
  95. {
  96. $this->adapter->setResponse('HTTP/1.1 400 Bad Request' . "\r\n\r\n");
  97. $this->mpns->send($this->getMessage('raw'));
  98. }
  99. /**
  100. * @expectedException Zend_Mobile_Push_Exception_InvalidToken
  101. */
  102. public function testSendThrowsExceptionWhenInvalidToken()
  103. {
  104. $this->adapter->setResponse('HTTP/1.1 401 Unauthorized' . "\r\n\r\n");
  105. $this->mpns->send($this->getMessage('raw'));
  106. }
  107. /**
  108. * @expectedException Zend_Mobile_Push_Exception_InvalidToken
  109. */
  110. public function testSendThrowsExceptionWhenDeviceNotRegistered()
  111. {
  112. $this->adapter->setResponse('HTTP/1.1 404 Not Found' . "\r\n\r\n");
  113. $this->mpns->send($this->getMessage('raw'));
  114. }
  115. /**
  116. * @expectedException Zend_Mobile_Push_Exception
  117. */
  118. public function testSendThrowsExceptionWhenMethodNotPost()
  119. {
  120. $this->adapter->setResponse('HTTP/1.1 405 Method Not Allowed' . "\r\n\r\n");
  121. $this->mpns->send($this->getMessage('raw'));
  122. }
  123. /**
  124. * @expectedException Zend_Mobile_Push_Exception_QuotaExceeded
  125. */
  126. public function testSendThrowsExceptionWhenServiceQuotaExceeded()
  127. {
  128. $this->adapter->setResponse('HTTP/1.1 406 Not Acceptable');
  129. $this->mpns->send($this->getMessage('raw'));
  130. }
  131. /**
  132. * @expectedException Zend_Mobile_Push_Exception_InvalidToken
  133. */
  134. public function testSendThrowsExceptionWhenInvalidToken2()
  135. {
  136. $this->adapter->setResponse('HTTP/1.1 412 Precondition Failed' . "\r\n\r\n");
  137. $this->mpns->send($this->getMessage('raw'));
  138. }
  139. /**
  140. * @expectedException Zend_Mobile_Push_Exception_ServerUnavailable
  141. */
  142. public function testSendThrowsExceptionWhenServerUnavailable()
  143. {
  144. $this->adapter->setResponse('HTTP/1.1 503 Service Unavailable' . "\r\n\r\n");
  145. $this->mpns->send($this->getMessage('raw'));
  146. }
  147. public function testAllOk()
  148. {
  149. $this->adapter->setResponse('HTTP/1.1 200 OK' . "\r\n\r\n");
  150. $this->mpns->send($this->getMessage('raw'));
  151. $toast = $this->getMessage('toast');
  152. $toast->setTitle('Foo');
  153. $toast->setMessage('Bar');
  154. $this->mpns->send($toast);
  155. $tile = $this->getMessage('tile');
  156. $tile->setBackgroundImage('red.jpg');
  157. $tile->setCount(1);
  158. $tile->setTitle('Foo Bar');
  159. // other optional attributes for wp7.1+
  160. $tile->setTileId('/SomeAction.xaml');
  161. $tile->setBackBackgroundImage('blue.jpg');
  162. $tile->setBackTitle('Bar');
  163. $tile->setBackContent('Foo');
  164. }
  165. }