ApnsTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/Message/Apns.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Mobile
  26. * @subpackage Push
  27. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @group Zend_Mobile
  30. * @group Zend_Mobile_Push
  31. * @group Zend_Mobile_Push_Apns
  32. */
  33. class Zend_Mobile_Push_Message_ApnsTest extends PHPUnit_Framework_TestCase
  34. {
  35. public function setUp()
  36. {
  37. $this->message = new Zend_Mobile_Push_Message_Apns();
  38. }
  39. public function testSetAlertTextReturnsCorrectly()
  40. {
  41. $text = 'my alert';
  42. $ret = $this->message->setAlert($text);
  43. $this->assertTrue($ret instanceof Zend_Mobile_Push_Message_Apns);
  44. $checkText = $this->message->getAlert();
  45. $this->assertTrue(is_array($checkText));
  46. $this->assertEquals($checkText['body'], $text);
  47. }
  48. /**
  49. * @expectedException Zend_Mobile_Push_Message_Exception
  50. */
  51. public function testSetAlertThrowsExceptionOnTextNonString()
  52. {
  53. $this->message->setAlert(array());
  54. }
  55. /**
  56. * @expectedException Zend_Mobile_Push_Message_Exception
  57. */
  58. public function testSetAlertThrowsExceptionOnActionLocKeyNonString()
  59. {
  60. $this->message->setAlert('text', array());
  61. }
  62. /**
  63. * @expectedException Zend_Mobile_Push_Message_Exception
  64. */
  65. public function testSetAlertThrowsExceptionOnLocKeyNonString()
  66. {
  67. $this->message->setAlert('text', 'button', array());
  68. }
  69. /**
  70. * @expectedException Zend_Mobile_Push_Message_Exception
  71. */
  72. public function testSetAlertThrowsExceptionOnLocArgsNonArray()
  73. {
  74. $this->message->setAlert('text', 'button', 'action', 'whoa');
  75. }
  76. /**
  77. * @expectedException Zend_Mobile_Push_Message_Exception
  78. */
  79. public function testSetAlertThrowsExceptionOnLaunchImageNonString()
  80. {
  81. $this->message->setAlert('text', 'button', 'action', array('locale'), array());
  82. }
  83. public function testSetBadgeReturnsCorrectNumber()
  84. {
  85. $num = 5;
  86. $this->message->setBadge($num);
  87. $this->assertEquals($this->message->getBadge(), $num);
  88. }
  89. /**
  90. * @expectedException Zend_Mobile_Push_Message_Exception
  91. */
  92. public function testSetBadgeNonNumericThrowsException()
  93. {
  94. $this->message->setBadge('string!');
  95. }
  96. /**
  97. * @expectedException Zend_Mobile_Push_Message_Exception
  98. */
  99. public function testSetBadgeNegativeNumberThrowsException()
  100. {
  101. $this->message->setBadge(-5);
  102. }
  103. public function testSetBadgeAllowsNull()
  104. {
  105. $this->message->setBadge(null);
  106. $this->assertNull($this->message->getBadge());
  107. }
  108. public function testSetExpireReturnsInteger()
  109. {
  110. $expire = 100;
  111. $this->message->setExpire($expire);
  112. $this->assertEquals($this->message->getExpire(), $expire);
  113. }
  114. /**
  115. * @expectedException Zend_Mobile_Push_Message_Exception
  116. */
  117. public function testSetExpireNonNumericThrowsException()
  118. {
  119. $this->message->setExpire('sting!');
  120. }
  121. public function testSetSoundReturnsString()
  122. {
  123. $sound = 'test';
  124. $this->message->setSound($sound);
  125. $this->assertEquals($this->message->getSound(), $sound);
  126. }
  127. /**
  128. * @expectedException Zend_Mobile_Push_Message_Exception
  129. */
  130. public function testSetSoundThrowsExceptionOnNonString()
  131. {
  132. $this->message->setSound(array());
  133. }
  134. public function testAddCustomDataReturnsSetData()
  135. {
  136. $addKey1 = 'test1';
  137. $addValue1 = array('val', 'ue', '1');
  138. $addKey2 = 'test2';
  139. $addValue2 = 'value2';
  140. $expected = array($addKey1 => $addValue1);
  141. $this->message->addCustomData($addKey1, $addValue1);
  142. $this->assertEquals($this->message->getCustomData(), $expected);
  143. $expected[$addKey2] = $addValue2;
  144. $this->message->addCustomData($addKey2, $addValue2);
  145. $this->assertEquals($this->message->getCustomData(), $expected);
  146. }
  147. /**
  148. * @expectedException Zend_Mobile_Push_Message_Exception
  149. */
  150. public function testAddCustomDataThrowsExceptionOnNonStringKey()
  151. {
  152. $this->message->addCustomData(array('key'), 'val');
  153. }
  154. /**
  155. * @expectedException Zend_Mobile_Push_Message_Exception
  156. */
  157. public function testAddCustomDataThrowsExceptionOnReservedKeyAps()
  158. {
  159. $this->message->addCustomData('aps', 'val');
  160. }
  161. public function testClearCustomDataClearsData()
  162. {
  163. $this->message->addCustomData('key', 'val');
  164. $this->message->clearCustomData();
  165. $this->assertEquals($this->message->getCustomData(), array());
  166. }
  167. public function testSetCustomData()
  168. {
  169. $data = array('key' => 'val', 'key2' => array(1, 2, 3, 4, 5));
  170. $this->message->setCustomData($data);
  171. $this->assertEquals($this->message->getCustomData(), $data);
  172. }
  173. public function testValidateReturnsFalseWithoutToken()
  174. {
  175. $this->assertFalse($this->message->validate());
  176. }
  177. public function testValidateReturnsFalseIdNotNumeric()
  178. {
  179. $this->message->setToken('abc');
  180. $this->message->setId('def');
  181. $this->assertFalse($this->message->validate());
  182. }
  183. public function testValidateReturnsTrueWhenProperlySet()
  184. {
  185. $this->message->setToken('abc');
  186. $this->assertTrue($this->message->validate());
  187. $this->message->setId(12345);
  188. $this->assertTrue($this->message->validate());
  189. }
  190. }