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 ZendService\Google\Exception\RuntimeException; |
19
|
|
|
use ZendService\Google\Gcm\Client; |
20
|
|
|
use ZendService\Google\Gcm\Response; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Gcm adapter tests. |
24
|
|
|
*/ |
25
|
|
|
class GcmAdapterTest extends \PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \Jgut\Tify\Adapter\Gcm\GcmAdapter |
29
|
|
|
*/ |
30
|
|
|
protected $adapter; |
31
|
|
|
|
32
|
|
|
public function setUp() |
33
|
|
|
{ |
34
|
|
|
$message = $this->getMock(GcmMessage::class, [], [], '', false); |
|
|
|
|
35
|
|
|
$message->expects(self::any())->method('getRegistrationIds')->will(self::returnValue(['aaa', 'bbb'])); |
36
|
|
|
|
37
|
|
|
$response = $this->getMock(Response::class, [], [], '', false); |
|
|
|
|
38
|
|
|
$response->expects(self::any())->method('getResults') |
39
|
|
|
->will(self::returnValue(['aaa' => [], 'bbb' => ['error' => 'NotRegistered']])); |
40
|
|
|
|
41
|
|
|
$client = $this->getMock(Client::class, [], [], '', false); |
|
|
|
|
42
|
|
|
$client->expects(self::any())->method('send')->will(self::returnValue($response)); |
43
|
|
|
|
44
|
|
|
$builder = $this->getMock(GcmBuilder::class, [], [], '', false); |
|
|
|
|
45
|
|
|
$builder->expects(self::any())->method('buildPushClient')->will(self::returnValue($client)); |
46
|
|
|
$builder->expects(self::any())->method('buildPushMessage')->will(self::returnValue($message)); |
47
|
|
|
|
48
|
|
|
$this->adapter = new GcmAdapter(['api_key' => 'aaa'], $builder); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @expectedException \Jgut\Tify\Exception\AdapterException |
53
|
|
|
*/ |
54
|
|
|
public function testInvalidApiKey() |
55
|
|
|
{ |
56
|
|
|
new GcmAdapter(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testSend() |
60
|
|
|
{ |
61
|
|
|
$message = $this->getMock(Message::class, [], [], '', false); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$receiver = $this->getMock(GcmReceiver::class, [], [], '', false); |
|
|
|
|
64
|
|
|
$receiver->expects(self::any())->method('getToken')->will(self::returnValue('aaa')); |
65
|
|
|
|
66
|
|
|
$notification = new Notification($message, [$receiver]); |
67
|
|
|
$results = $this->adapter->push($notification); |
68
|
|
|
|
69
|
|
|
self::assertCount(2, $results); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testExceptionErrorCode() |
73
|
|
|
{ |
74
|
|
|
$reflection = new \ReflectionClass(get_class($this->adapter)); |
75
|
|
|
$method = $reflection->getMethod('getErrorCodeFromException'); |
76
|
|
|
$method->setAccessible(true); |
77
|
|
|
|
78
|
|
|
$exception = new RuntimeException('500 Internal Server Error'); |
79
|
|
|
self::assertEquals(GcmAdapter::RESULT_INTERNAL_SERVER_ERROR, $method->invoke($this->adapter, $exception)); |
80
|
|
|
|
81
|
|
|
$exception = new RuntimeException('503 Server Unavailable; Retry-After 200'); |
82
|
|
|
self::assertEquals(GcmAdapter::RESULT_SERVER_UNAVAILABLE, $method->invoke($this->adapter, $exception)); |
83
|
|
|
|
84
|
|
|
$exception = new RuntimeException('401 Forbidden; Authentication Error'); |
85
|
|
|
self::assertEquals(GcmAdapter::RESULT_AUTHENTICATION_ERROR, $method->invoke($this->adapter, $exception)); |
86
|
|
|
|
87
|
|
|
$exception = new RuntimeException('400 Bad Request; invalid message'); |
88
|
|
|
self::assertEquals(GcmAdapter::RESULT_INVALID_MESSAGE, $method->invoke($this->adapter, $exception)); |
89
|
|
|
|
90
|
|
|
$exception = new RuntimeException('Response body did not contain a valid JSON response'); |
91
|
|
|
self::assertEquals(GcmAdapter::RESULT_BAD_FORMATTED_RESPONSE, $method->invoke($this->adapter, $exception)); |
92
|
|
|
|
93
|
|
|
$exception = new RuntimeException('Unknown'); |
94
|
|
|
self::assertEquals(GcmAdapter::RESULT_UNKNOWN, $method->invoke($this->adapter, $exception)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.