|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Push notification services abstraction (http://github.com/juliangut/tify) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/juliangut/tify for the canonical source repository |
|
6
|
|
|
* |
|
7
|
|
|
* @license https://github.com/juliangut/tify/blob/master/LICENSE |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Jgut\Tify\Tests\Adapter\Gcm; |
|
11
|
|
|
|
|
12
|
|
|
use Jgut\Tify\Adapter\Gcm\GcmAdapter; |
|
13
|
|
|
use Jgut\Tify\Adapter\Gcm\GcmBuilder; |
|
14
|
|
|
use Jgut\Tify\Adapter\Gcm\GcmMessage; |
|
15
|
|
|
use Jgut\Tify\Message; |
|
16
|
|
|
use Jgut\Tify\Notification; |
|
17
|
|
|
use Jgut\Tify\Receiver\GcmReceiver; |
|
18
|
|
|
use Jgut\Tify\Result; |
|
19
|
|
|
use ZendService\Google\Exception\RuntimeException; |
|
20
|
|
|
use ZendService\Google\Gcm\Client; |
|
21
|
|
|
use ZendService\Google\Gcm\Response; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Gcm adapter tests. |
|
25
|
|
|
*/ |
|
26
|
|
|
class GcmAdapterTest extends \PHPUnit_Framework_TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var \Jgut\Tify\Adapter\Gcm\GcmAdapter |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $adapter; |
|
32
|
|
|
|
|
33
|
|
|
public function setUp() |
|
34
|
|
|
{ |
|
35
|
|
|
$message = $this->getMockBuilder(GcmMessage::class)->disableOriginalConstructor()->getMock(); |
|
36
|
|
|
$message->expects(self::any())->method('getRegistrationIds')->will(self::returnValue(['aaa', 'bbb', 'ccc'])); |
|
37
|
|
|
|
|
38
|
|
|
$response = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock(); |
|
39
|
|
|
$response->expects(self::any())->method('getResults') |
|
40
|
|
|
->will(self::returnValue(['aaa' => [], 'bbb' => ['error' => 'NotRegistered']])); |
|
41
|
|
|
|
|
42
|
|
|
$client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock(); |
|
43
|
|
|
$client->expects(self::any())->method('send')->will(self::returnValue($response)); |
|
44
|
|
|
|
|
45
|
|
|
$builder = $this->getMockBuilder(GcmBuilder::class)->disableOriginalConstructor()->getMock(); |
|
46
|
|
|
$builder->expects(self::any())->method('buildPushClient')->will(self::returnValue($client)); |
|
47
|
|
|
$builder->expects(self::any())->method('buildPushMessage')->will(self::returnValue($message)); |
|
48
|
|
|
|
|
49
|
|
|
$this->adapter = new GcmAdapter(['api_key' => 'aaa'], $builder); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @expectedException \Jgut\Tify\Exception\AdapterException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testInvalidApiKey() |
|
56
|
|
|
{ |
|
57
|
|
|
new GcmAdapter(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testSend() |
|
61
|
|
|
{ |
|
62
|
|
|
$message = $this->getMockBuilder(Message::class)->disableOriginalConstructor()->getMock(); |
|
63
|
|
|
|
|
64
|
|
|
$receiver = $this->getMockBuilder(GcmReceiver::class)->disableOriginalConstructor()->getMock(); |
|
65
|
|
|
$receiver->expects(self::any())->method('getToken')->will(self::returnValue('aaa')); |
|
66
|
|
|
|
|
67
|
|
|
$notification = new Notification($message, [$receiver]); |
|
68
|
|
|
/* @var Result[] $results */ |
|
69
|
|
|
$results = $this->adapter->push($notification); |
|
70
|
|
|
|
|
71
|
|
|
self::assertCount(3, $results); |
|
72
|
|
|
|
|
73
|
|
|
self::assertEquals('aaa', $results[0]->getToken()); |
|
74
|
|
|
self::assertTrue($results[0]->isSuccess()); |
|
75
|
|
|
|
|
76
|
|
|
self::assertEquals('bbb', $results[1]->getToken()); |
|
77
|
|
|
self::assertTrue($results[1]->isError()); |
|
78
|
|
|
|
|
79
|
|
|
self::assertEquals('ccc', $results[2]->getToken()); |
|
80
|
|
|
self::assertTrue($results[2]->isError()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testExceptionErrorCode() |
|
84
|
|
|
{ |
|
85
|
|
|
$reflection = new \ReflectionClass(get_class($this->adapter)); |
|
86
|
|
|
$method = $reflection->getMethod('getErrorCodeFromException'); |
|
87
|
|
|
$method->setAccessible(true); |
|
88
|
|
|
|
|
89
|
|
|
$exception = new RuntimeException('500 Internal Server Error'); |
|
90
|
|
|
self::assertEquals(GcmAdapter::RESPONSE_INTERNAL_SERVER_ERROR, $method->invoke($this->adapter, $exception)); |
|
91
|
|
|
|
|
92
|
|
|
$exception = new RuntimeException('503 Server Unavailable; Retry-After 200'); |
|
93
|
|
|
self::assertEquals(GcmAdapter::RESPONSE_SERVER_UNAVAILABLE, $method->invoke($this->adapter, $exception)); |
|
94
|
|
|
|
|
95
|
|
|
$exception = new RuntimeException('401 Forbidden; Authentication Error'); |
|
96
|
|
|
self::assertEquals(GcmAdapter::RESPONSE_AUTHENTICATION_ERROR, $method->invoke($this->adapter, $exception)); |
|
97
|
|
|
|
|
98
|
|
|
$exception = new RuntimeException('400 Bad Request; invalid message'); |
|
99
|
|
|
self::assertEquals(GcmAdapter::RESPONSE_INVALID_MESSAGE, $method->invoke($this->adapter, $exception)); |
|
100
|
|
|
|
|
101
|
|
|
$exception = new RuntimeException('Response body did not contain a valid JSON response'); |
|
102
|
|
|
self::assertEquals(GcmAdapter::RESPONSE_BADLY_FORMATTED_RESPONSE, $method->invoke($this->adapter, $exception)); |
|
103
|
|
|
|
|
104
|
|
|
$exception = new RuntimeException('Unknown'); |
|
105
|
|
|
self::assertEquals(GcmAdapter::RESPONSE_UNKNOWN_ERROR, $method->invoke($this->adapter, $exception)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|