|
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\Apns; |
|
11
|
|
|
|
|
12
|
|
|
use Jgut\Tify\Adapter\Apns\ApnsAdapter; |
|
13
|
|
|
use Jgut\Tify\Adapter\Apns\ApnsBuilder; |
|
14
|
|
|
use Jgut\Tify\Message; |
|
15
|
|
|
use Jgut\Tify\Notification; |
|
16
|
|
|
use Jgut\Tify\Receiver\ApnsReceiver; |
|
17
|
|
|
use ZendService\Apple\Apns\Client\Feedback as FeedbackClient; |
|
18
|
|
|
use ZendService\Apple\Apns\Client\Message as MessageClient; |
|
19
|
|
|
use ZendService\Apple\Apns\Message as ServiceMessage; |
|
20
|
|
|
use ZendService\Apple\Apns\Response\Feedback as FeedbackResponse; |
|
21
|
|
|
use ZendService\Apple\Apns\Response\Message as PushResponse; |
|
22
|
|
|
use ZendService\Apple\Exception\RuntimeException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Apns adapter tests. |
|
26
|
|
|
*/ |
|
27
|
|
|
class ApnsAdapterTest extends \PHPUnit_Framework_TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \Jgut\Tify\Adapter\Apns\ApnsAdapter |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $adapter; |
|
33
|
|
|
|
|
34
|
|
|
public function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
$message = $this->getMockBuilder(ServiceMessage::class)->disableOriginalConstructor()->getMock(); |
|
37
|
|
|
$message->expects(self::any())->method('getToken')->will(self::returnValue(['aaa'])); |
|
38
|
|
|
|
|
39
|
|
|
$pushResponse = $this->getMockBuilder(PushResponse::class)->disableOriginalConstructor()->getMock(); |
|
40
|
|
|
$pushResponse->expects(self::any())->method('getCode')->will(self::returnValue(8)); |
|
41
|
|
|
|
|
42
|
|
|
$pushClient = $this->getMockBuilder(MessageClient::class)->disableOriginalConstructor()->getMock(); |
|
43
|
|
|
$pushClient->expects(self::any())->method('send')->will(self::returnValue($pushResponse)); |
|
44
|
|
|
|
|
45
|
|
|
$feedbackResponse = $this->getMockBuilder(FeedbackResponse::class)->disableOriginalConstructor()->getMock(); |
|
46
|
|
|
$feedbackResponse->expects(self::any())->method('getToken')->will(self::returnValue('aaa')); |
|
47
|
|
|
|
|
48
|
|
|
$feedbackClient = $this->getMockBuilder(FeedbackClient::class)->disableOriginalConstructor()->getMock(); |
|
49
|
|
|
$feedbackClient->expects(self::any())->method('feedback')->will(self::returnValue([$feedbackResponse])); |
|
50
|
|
|
|
|
51
|
|
|
$builder = $this->getMockBuilder(ApnsBuilder::class)->disableOriginalConstructor()->getMock(); |
|
52
|
|
|
$builder->expects(self::any())->method('buildPushClient')->will(self::returnValue($pushClient)); |
|
53
|
|
|
$builder->expects(self::any())->method('buildFeedbackClient')->will(self::returnValue($feedbackClient)); |
|
54
|
|
|
$builder->expects(self::any())->method('buildPushMessage')->will(self::returnValue($message)); |
|
55
|
|
|
|
|
56
|
|
|
$this->adapter = new ApnsAdapter( |
|
57
|
|
|
['certificate' => __DIR__ . '/../../../files/apns_certificate.pem'], |
|
58
|
|
|
false, |
|
59
|
|
|
$builder |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @expectedException \Jgut\Tify\Exception\AdapterException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testInvalidCertificate() |
|
67
|
|
|
{ |
|
68
|
|
|
new ApnsAdapter(['certificate' => 'fake_path']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testSend() |
|
72
|
|
|
{ |
|
73
|
|
|
$message = $this->getMockBuilder(Message::class)->disableOriginalConstructor()->getMock(); |
|
74
|
|
|
|
|
75
|
|
|
$receiver = $this->getMockBuilder(ApnsReceiver::class)->disableOriginalConstructor()->getMock(); |
|
76
|
|
|
|
|
77
|
|
|
$notification = new Notification($message, [$receiver]); |
|
78
|
|
|
$results = $this->adapter->push($notification); |
|
79
|
|
|
|
|
80
|
|
|
self::assertCount(1, $results); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testFeedback() |
|
84
|
|
|
{ |
|
85
|
|
|
self::assertCount(1, $this->adapter->feedback()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testExceptionErrorCode() |
|
89
|
|
|
{ |
|
90
|
|
|
$reflection = new \ReflectionClass(get_class($this->adapter)); |
|
91
|
|
|
$method = $reflection->getMethod('getErrorCodeFromException'); |
|
92
|
|
|
$method->setAccessible(true); |
|
93
|
|
|
|
|
94
|
|
|
$exception = new RuntimeException('Server is unavailable; please retry later'); |
|
95
|
|
|
self::assertEquals(ApnsAdapter::RESPONSE_UNAVAILABLE, $method->invoke($this->adapter, $exception)); |
|
96
|
|
|
|
|
97
|
|
|
$exception = new RuntimeException('Unknown'); |
|
98
|
|
|
self::assertEquals(ApnsAdapter::RESPONSE_UNKNOWN_ERROR, $method->invoke($this->adapter, $exception)); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|