GcmTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/Message/Gcm.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Mobile
  26. * @subpackage Push
  27. * @copyright Copyright (c) 2005-2011 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_Gcm
  32. */
  33. class Zend_Mobile_Push_Message_GcmTest extends PHPUnit_Framework_TestCase
  34. {
  35. /**
  36. * @expectedException Zend_Mobile_Push_Message_Exception
  37. */
  38. public function testAddDataThrowsExceptionOnNonStringKey()
  39. {
  40. $msg = new Zend_Mobile_Push_Message_Gcm();
  41. $msg->addData(array(), 'value');
  42. }
  43. /**
  44. * @expectedException Zend_Mobile_Push_Message_Exception
  45. */
  46. public function testAddDataThrowsExceptionOnNonScalarValue()
  47. {
  48. $msg = new Zend_Mobile_Push_Message_Gcm();
  49. $msg->addData('key', new stdClass);
  50. }
  51. public function testSetData()
  52. {
  53. $data = array('key' => 'value');
  54. $data2 = array('key2' => 'value2');
  55. $msg = new Zend_Mobile_Push_Message_Gcm();
  56. $msg->setData($data);
  57. $this->assertEquals($data, $msg->getData());
  58. $msg->setData($data2);
  59. $this->assertEquals($data2, $msg->getData());
  60. }
  61. public function testTokens()
  62. {
  63. $msg = new Zend_Mobile_Push_Message_Gcm();
  64. $msg->setToken('foo');
  65. $this->assertEquals(array('foo'), $msg->getToken());
  66. $msg->setToken(array('foo', 'bar'));
  67. $this->assertEquals(array('foo', 'bar'), $msg->getToken());
  68. $msg->setToken('bar');
  69. $msg->addToken('foo');
  70. $this->assertEquals(array('bar', 'foo'), $msg->getToken());
  71. $msg->clearToken();
  72. $this->assertEquals(array(), $msg->getToken());
  73. }
  74. public function testDelayWhileIdle()
  75. {
  76. $msg = new Zend_Mobile_Push_Message_Gcm();
  77. $msg->setDelayWhileIdle(true);
  78. $this->assertTrue($msg->getDelayWhileIdle());
  79. $msg->setDelayWhileIdle(false);
  80. $this->assertFalse($msg->getDelayWhileIdle());
  81. }
  82. /**
  83. * @expectedException Zend_Mobile_Push_Message_Exception
  84. */
  85. public function testDelayWhileIdleThrowsExceptionOnInvalidValue()
  86. {
  87. $msg = new Zend_Mobile_Push_Message_Gcm();
  88. $msg->setDelayWhileIdle('true');
  89. }
  90. public function testTtl()
  91. {
  92. $msg = new Zend_Mobile_Push_Message_Gcm();
  93. $msg->setTtl(10);
  94. $this->assertEquals(10, $msg->getTtl());
  95. }
  96. /**
  97. * @expectedException Zend_Mobile_Push_Message_Exception
  98. */
  99. public function testTtlThrowsExceptionOnInvalidValue()
  100. {
  101. $msg = new Zend_Mobile_Push_Message_Gcm();
  102. $msg->setTtl('foo');
  103. }
  104. public function testValidateWithoutTokenReturnsFalse()
  105. {
  106. $msg = new Zend_Mobile_Push_Message_Gcm();
  107. $this->assertFalse($msg->validate());
  108. }
  109. public function testValidateToken()
  110. {
  111. $msg = new Zend_Mobile_Push_Message_Gcm();
  112. $msg->setToken('a-token!');
  113. $this->assertTrue($msg->validate());
  114. }
  115. public function testValidateWithTtlAndNoIdReturnsFalse()
  116. {
  117. $msg = new Zend_Mobile_Push_Message_Gcm();
  118. $msg->setToken('foo');
  119. $msg->setTtl(10);
  120. $this->assertFalse($msg->validate());
  121. }
  122. }