Example #1
0
    public function testJsonInitialization()
    {
        Response::$type = PayloadMessage::TYPE_JSON;
        $Response1 = Response::initializeByString('{ "multicast_id": 108,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    { "message_id": "1:08" }
  ]
}');
        $this->assertInstanceOf(Status::class, $Response1->getResults()[0]);
        $Status1 = $Response1->getResults()[0];
        $this->assertEmpty($Status1->getCanonicalId());
        $this->assertEmpty($Status1->getError());
        $this->assertEquals('1:08', $Status1->getMessageId());
        $this->assertFalse($Status1->hasCanonicalId());
        $this->assertFalse($Status1->mustRemove());
        $this->assertFalse($Status1->canRetry());
        $Response2 = Response::initializeByString('{ "multicast_id": 216,
  "success": 3,
  "failure": 3,
  "canonical_ids": 1,
  "results": [
    { "message_id": "1:0408" },
    { "error": "Unavailable" },
    { "error": "InvalidRegistration" },
    { "message_id": "1:1516" },
    { "message_id": "1:2342", "registration_id": "32" },
    { "error": "NotRegistered"}
  ]
}');
        $this->assertInstanceOf(Status::class, $Response2->getResults()[0]);
        $this->assertInstanceOf(Status::class, $Response2->getResults()[1]);
        $this->assertInstanceOf(Status::class, $Response2->getResults()[2]);
        $this->assertInstanceOf(Status::class, $Response2->getResults()[3]);
        $this->assertInstanceOf(Status::class, $Response2->getResults()[4]);
        $this->assertInstanceOf(Status::class, $Response2->getResults()[5]);
        $Status21 = $Response2->getResults()[0];
        $this->assertEmpty($Status21->getCanonicalId());
        $this->assertEmpty($Status21->getError());
        $this->assertEquals('1:0408', $Status21->getMessageId());
        $this->assertFalse($Status21->hasCanonicalId());
        $this->assertFalse($Status21->mustRemove());
        $this->assertFalse($Status21->canRetry());
        $Status22 = $Response2->getResults()[1];
        $this->assertEmpty($Status22->getCanonicalId());
        $this->assertEquals(Status::STATUS_UNAVAILABLE, $Status22->getError());
        $this->assertEmpty($Status22->getMessageId());
        $this->assertFalse($Status22->hasCanonicalId());
        $this->assertFalse($Status22->mustRemove());
        $this->assertTrue($Status22->canRetry());
        $Status23 = $Response2->getResults()[2];
        $this->assertEmpty($Status23->getCanonicalId());
        $this->assertEquals(Status::STATUS_INVALID_REGISTRATION, $Status23->getError());
        $this->assertEmpty($Status23->getMessageId());
        $this->assertFalse($Status23->hasCanonicalId());
        $this->assertFalse($Status23->mustRemove());
        $this->assertFalse($Status23->canRetry());
        $Status24 = $Response2->getResults()[3];
        $this->assertEmpty($Status24->getCanonicalId());
        $this->assertEmpty($Status24->getError());
        $this->assertEquals('1:1516', $Status24->getMessageId());
        $this->assertFalse($Status24->hasCanonicalId());
        $this->assertFalse($Status24->mustRemove());
        $this->assertFalse($Status24->canRetry());
        $Status25 = $Response2->getResults()[4];
        $this->assertEquals('32', $Status25->getCanonicalId());
        $this->assertEmpty($Status25->getError());
        $this->assertEquals('1:2342', $Status25->getMessageId());
        $this->assertTrue($Status25->hasCanonicalId());
        $this->assertFalse($Status25->mustRemove());
        $this->assertFalse($Status25->canRetry());
        $Status26 = $Response2->getResults()[5];
        $this->assertEmpty($Status26->getCanonicalId());
        $this->assertEquals(Status::STATUS_NOT_REGISTERED, $Status26->getError());
        $this->assertEmpty($Status26->getMessageId());
        $this->assertFalse($Status26->hasCanonicalId());
        $this->assertTrue($Status26->mustRemove());
        $this->assertFalse($Status26->canRetry());
    }
Example #2
0
 /**
  * Send GCM message method
  * @param PayloadMessage $Message GCM message instance
  * @return Response sent response instance
  * @throws GCMFormatException when request or response format was incorrect
  * @throws GCMUnauthorizedException when was incorrect authorization key
  * @throws GCMServerError when something wrong on the GCM server
  * @throws GCMUnrecoverableError when GCM server is not available
  */
 public function send(PayloadMessage $Message)
 {
     switch ($Message->getType()) {
         case PayloadMessage::TYPE_PLAIN:
             $this->getRequest()->setContentTypeCode(Request::CONTENT_TYPE_UNDEFINED);
             $this->getRequest()->addHeader('Content-Type', self::CONTENT_TYPE_PLAIN_TEXT);
             break;
         case PayloadMessage::TYPE_JSON:
             $this->getRequest()->setContentTypeCode(Request::CONTENT_TYPE_JSON);
             $this->getRequest()->addHeader('Content-Type', self::CONTENT_TYPE_JSON);
             break;
         default:
             throw new GCMFormatException('unsupported request format code \'' . $Message->getType() . '\'');
     }
     $this->getRequest()->setPostData($Message->export());
     try {
         $result = $this->getRequest()->send();
         Response::$type = $Message->getType();
         return Response::initializeByString($result);
     } catch (HttpClientErrorCodeException $Ex) {
         switch ($Ex->getCode()) {
             case '400':
                 throw new GCMFormatException('invalid JSON request with message \'' . $Ex->getMessage() . '\'');
             case '401':
                 throw new GCMUnauthorizedException('invalid authorization key \'' . $this->getAuthorizationKey() . '\'');
             default:
                 throw $Ex;
         }
     } catch (HttpServerErrorCodeException $Ex) {
         switch ($Ex->getCode()) {
             case '500':
                 throw new GCMUnrecoverableError('unrecoverable GCM server error');
             default:
                 $headers = $this->getRequest()->getResponseHeaders();
                 throw new GCMServerError(@$headers['Retry-After']);
         }
     }
 }
Example #3
0
 public function testUnknownTypeResponse()
 {
     Response::$type = 88;
     try {
         Response::initializeByString('{ "multicast_id": 108,
   "success": 1,
   "failure": 0,
   "canonical_ids": 0,
   "results": [
     { "message_id": "1:08" }
   ]
 }');
         $this->fail();
     } catch (GCMFormatException $Ex) {
     }
 }