1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the AppleApnPush package |
5
|
|
|
* |
6
|
|
|
* (c) Vitaliy Zhuk <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tests\Apple\ApnPush\Protocol; |
13
|
|
|
|
14
|
|
|
use Apple\ApnPush\Encoder\MessageEncoderInterface; |
15
|
|
|
use Apple\ApnPush\Exception\SendMessage\SendMessageException; |
16
|
|
|
use Apple\ApnPush\Model\ApsData; |
17
|
|
|
use Apple\ApnPush\Model\DeviceToken; |
18
|
|
|
use Apple\ApnPush\Model\Message; |
19
|
|
|
use Apple\ApnPush\Model\Receiver; |
20
|
|
|
use Apple\ApnPush\Protocol\Http\Authenticator\AuthenticatorInterface; |
21
|
|
|
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactoryInterface; |
22
|
|
|
use Apple\ApnPush\Protocol\Http\Request; |
23
|
|
|
use Apple\ApnPush\Protocol\Http\Response; |
24
|
|
|
use Apple\ApnPush\Protocol\Http\Sender\HttpSenderInterface; |
25
|
|
|
use Apple\ApnPush\Protocol\Http\UriFactory\UriFactoryInterface; |
26
|
|
|
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolVisitorInterface; |
27
|
|
|
use Apple\ApnPush\Protocol\HttpProtocol; |
28
|
|
|
use PHPUnit\Framework\TestCase; |
29
|
|
|
|
30
|
|
|
class HttpProtocolTest extends TestCase |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var AuthenticatorInterface|\PHPUnit_Framework_MockObject_MockObject |
34
|
|
|
*/ |
35
|
|
|
private $authenticator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var HttpSenderInterface|\PHPUnit_Framework_MockObject_MockObject |
39
|
|
|
*/ |
40
|
|
|
private $httpSender; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var MessageEncoderInterface|\PHPUnit_Framework_MockObject_MockObject |
44
|
|
|
*/ |
45
|
|
|
private $messageEncoder; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var UriFactoryInterface|\PHPUnit_Framework_MockObject_MockObject |
49
|
|
|
*/ |
50
|
|
|
private $uriFactory; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var HttpProtocolVisitorInterface|\PHPUnit_Framework_MockObject_MockObject |
54
|
|
|
*/ |
55
|
|
|
private $visitor; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var ExceptionFactoryInterface|\PHPUnit_Framework_MockObject_MockObject |
59
|
|
|
*/ |
60
|
|
|
private $exceptionFactory; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var HttpProtocol |
64
|
|
|
*/ |
65
|
|
|
private $protocol; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected function setUp() |
71
|
|
|
{ |
72
|
|
|
$this->authenticator = self::createMock(AuthenticatorInterface::class); |
73
|
|
|
$this->httpSender = self::createMock(HttpSenderInterface::class); |
74
|
|
|
$this->messageEncoder = self::createMock(MessageEncoderInterface::class); |
75
|
|
|
$this->uriFactory = self::createMock(UriFactoryInterface::class); |
76
|
|
|
$this->visitor = self::createMock(HttpProtocolVisitorInterface::class); |
77
|
|
|
$this->exceptionFactory = self::createMock(ExceptionFactoryInterface::class); |
78
|
|
|
|
79
|
|
|
$this->protocol = new HttpProtocol( |
80
|
|
|
$this->authenticator, |
81
|
|
|
$this->httpSender, |
82
|
|
|
$this->messageEncoder, |
83
|
|
|
$this->uriFactory, |
84
|
|
|
$this->visitor, |
85
|
|
|
$this->exceptionFactory |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @test |
91
|
|
|
*/ |
92
|
|
|
public function shouldSuccessSend() |
93
|
|
|
{ |
94
|
|
|
$deviceToken = new DeviceToken(str_repeat('af', 32)); |
95
|
|
|
$receiver = new Receiver($deviceToken, 'com.test'); |
96
|
|
|
$message = new Message(new ApsData()); |
97
|
|
|
|
98
|
|
|
$this->messageEncoder->expects(self::once()) |
|
|
|
|
99
|
|
|
->method('encode') |
100
|
|
|
->with($message) |
101
|
|
|
->willReturn('{"aps":{}}'); |
102
|
|
|
|
103
|
|
|
$this->uriFactory->expects(self::once()) |
|
|
|
|
104
|
|
|
->method('create') |
105
|
|
|
->with($deviceToken, false) |
106
|
|
|
->willReturn('https://some.com/'.$deviceToken); |
107
|
|
|
|
108
|
|
|
$this->authenticator->expects(self::once()) |
|
|
|
|
109
|
|
|
->method('authenticate') |
110
|
|
|
->with(self::isInstanceOf(Request::class)) |
111
|
|
|
->willReturnCallback(function (Request $innerRequest) { |
112
|
|
|
return $innerRequest; |
113
|
|
|
}); |
114
|
|
|
|
115
|
|
|
$this->visitor->expects(self::once()) |
|
|
|
|
116
|
|
|
->method('visit') |
117
|
|
|
->with($message, self::isInstanceOf(Request::class)); |
118
|
|
|
|
119
|
|
|
$this->httpSender->expects(self::once()) |
|
|
|
|
120
|
|
|
->method('send') |
121
|
|
|
->with(self::isInstanceOf(Request::class)) |
122
|
|
|
->willReturn(new Response(200, '{}')); |
123
|
|
|
|
124
|
|
|
$this->protocol->send($receiver, $message, false); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @test |
129
|
|
|
* |
130
|
|
|
* @expectedException \Apple\ApnPush\Exception\SendMessage\SendMessageException |
131
|
|
|
*/ |
132
|
|
|
public function shouldFailSend() |
133
|
|
|
{ |
134
|
|
|
$deviceToken = new DeviceToken(str_repeat('af', 32)); |
135
|
|
|
$receiver = new Receiver($deviceToken, 'com.test'); |
136
|
|
|
$message = new Message(new ApsData()); |
137
|
|
|
|
138
|
|
|
$this->messageEncoder->expects(self::once()) |
|
|
|
|
139
|
|
|
->method('encode') |
140
|
|
|
->with($message) |
141
|
|
|
->willReturn('{"aps":{}}'); |
142
|
|
|
|
143
|
|
|
$this->uriFactory->expects(self::once()) |
|
|
|
|
144
|
|
|
->method('create') |
145
|
|
|
->with($deviceToken, false) |
146
|
|
|
->willReturn('https://some.com/'.$deviceToken); |
147
|
|
|
|
148
|
|
|
$this->authenticator->expects(self::once()) |
|
|
|
|
149
|
|
|
->method('authenticate') |
150
|
|
|
->with(self::isInstanceOf(Request::class)) |
151
|
|
|
->willReturnCallback(function (Request $innerRequest) { |
152
|
|
|
return $innerRequest; |
153
|
|
|
}); |
154
|
|
|
|
155
|
|
|
$this->visitor->expects(self::once()) |
|
|
|
|
156
|
|
|
->method('visit') |
157
|
|
|
->with($message, self::isInstanceOf(Request::class)); |
158
|
|
|
|
159
|
|
|
$this->httpSender->expects(self::once()) |
|
|
|
|
160
|
|
|
->method('send') |
161
|
|
|
->with(self::isInstanceOf(Request::class)) |
162
|
|
|
->willReturn(new Response(404, '{}')); |
163
|
|
|
|
164
|
|
|
$this->httpSender->expects(self::once()) |
165
|
|
|
->method('close'); |
166
|
|
|
|
167
|
|
|
$this->exceptionFactory->expects(self::once()) |
|
|
|
|
168
|
|
|
->method('create') |
169
|
|
|
->with(new Response(404, '{}')) |
170
|
|
|
->willReturn(self::createMock(SendMessageException::class)); |
171
|
|
|
|
172
|
|
|
$this->protocol->send($receiver, $message, false); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: