GcmTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/Response/Gcm.php';
  23. require_once 'Zend/Mobile/Push/Message/Gcm.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Mobile
  27. * @subpackage Push
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Mobile
  31. * @group Zend_Mobile_Push
  32. * @group Zend_Mobile_Push_Gcm
  33. */
  34. class Zend_Mobile_Push_Response_GcmTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function testConstructor()
  37. {
  38. $response = new Zend_Mobile_Push_Response_Gcm();
  39. $this->assertNull($response->getResponse());
  40. $this->assertNull($response->getMessage());
  41. $message = new Zend_Mobile_Push_Message_Gcm();
  42. $response = new Zend_Mobile_Push_Response_Gcm(null, $message);
  43. $this->assertEquals($message, $response->getMessage());
  44. $this->assertNull($response->getResponse());
  45. $message = new Zend_Mobile_Push_Message_Gcm();
  46. $responseArr = json_encode(array(
  47. 'results' => array(
  48. array('message_id' => '1:1234'),
  49. ),
  50. 'success' => 1,
  51. 'failure' => 0,
  52. 'canonical_ids' => 0,
  53. 'multicast_id' => 1,
  54. ));
  55. $response = new Zend_Mobile_Push_Response_Gcm($responseArr, $message);
  56. $this->assertEquals(json_decode($responseArr, true), $response->getResponse());
  57. $this->assertEquals($message, $response->getMessage());
  58. }
  59. /**
  60. * @expectedException Zend_Mobile_Push_Exception_ServerUnavailable
  61. */
  62. public function testConstructorThrowsExceptionOnBadOrEmptyJsonString()
  63. {
  64. $response = new Zend_Mobile_Push_Response_Gcm('{bad');
  65. }
  66. public function testSetGetMessage()
  67. {
  68. $message = new Zend_Mobile_Push_Message_Gcm();
  69. $response = new Zend_Mobile_Push_Response_Gcm();
  70. $response->setMessage($message);
  71. $this->assertEquals($message, $response->getMessage());
  72. }
  73. public function testResponse()
  74. {
  75. $responseArr = array(
  76. 'results' => array(
  77. array('message_id' => '1:234'),
  78. ),
  79. 'success' => 1,
  80. 'failure' => 0,
  81. 'canonical_ids' => 0,
  82. 'multicast_id' => '123',
  83. );
  84. $response = new Zend_Mobile_Push_Response_Gcm();
  85. $response->setResponse($responseArr);
  86. $this->assertEquals($responseArr, $response->getResponse());
  87. $this->assertEquals(1, $response->getSuccessCount());
  88. $this->assertEquals(0, $response->getFailureCount());
  89. $this->assertEquals(0, $response->getCanonicalCount());
  90. // test results non correlated
  91. $expected = array(array('message_id' => '1:234'));
  92. $this->assertEquals($expected, $response->getResults());
  93. $expected = array(0 => '1:234');
  94. $this->assertEquals($expected, $response->getResult(Zend_Mobile_Push_Response_Gcm::RESULT_MESSAGE_ID));
  95. $message = new Zend_Mobile_Push_Message_Gcm();
  96. $message->setToken(array('ABCDEF'));
  97. $response->setMessage($message);
  98. $expected = array('ABCDEF' => '1:234');
  99. $this->assertEquals($expected, $response->getResult(Zend_Mobile_Push_Response_Gcm::RESULT_MESSAGE_ID));
  100. }
  101. }