|
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\Http\ExceptionFactory; |
|
13
|
|
|
|
|
14
|
|
|
use Apple\ApnPush\Exception\SendMessage\BadCertificateEnvironmentException; |
|
15
|
|
|
use Apple\ApnPush\Exception\SendMessage\BadCertificateException; |
|
16
|
|
|
use Apple\ApnPush\Exception\SendMessage\BadCollapseIdException; |
|
17
|
|
|
use Apple\ApnPush\Exception\SendMessage\BadDeviceTokenException; |
|
18
|
|
|
use Apple\ApnPush\Exception\SendMessage\BadExpirationDateException; |
|
19
|
|
|
use Apple\ApnPush\Exception\SendMessage\BadMessageIdException; |
|
20
|
|
|
use Apple\ApnPush\Exception\SendMessage\BadPathException; |
|
21
|
|
|
use Apple\ApnPush\Exception\SendMessage\BadPriorityException; |
|
22
|
|
|
use Apple\ApnPush\Exception\SendMessage\BadTopicException; |
|
23
|
|
|
use Apple\ApnPush\Exception\SendMessage\DeviceTokenNotForTopicException; |
|
24
|
|
|
use Apple\ApnPush\Exception\SendMessage\DuplicateHeadersException; |
|
25
|
|
|
use Apple\ApnPush\Exception\SendMessage\ExpiredProviderTokenException; |
|
26
|
|
|
use Apple\ApnPush\Exception\SendMessage\ForbiddenException; |
|
27
|
|
|
use Apple\ApnPush\Exception\SendMessage\IdleTimeoutException; |
|
28
|
|
|
use Apple\ApnPush\Exception\SendMessage\InternalServerErrorException; |
|
29
|
|
|
use Apple\ApnPush\Exception\SendMessage\InvalidProviderTokenException; |
|
30
|
|
|
use Apple\ApnPush\Exception\SendMessage\MethodNotAllowedException; |
|
31
|
|
|
use Apple\ApnPush\Exception\SendMessage\MissingDeviceTokenException; |
|
32
|
|
|
use Apple\ApnPush\Exception\SendMessage\MissingProviderTokenException; |
|
33
|
|
|
use Apple\ApnPush\Exception\SendMessage\MissingTopicException; |
|
34
|
|
|
use Apple\ApnPush\Exception\SendMessage\PayloadEmptyException; |
|
35
|
|
|
use Apple\ApnPush\Exception\SendMessage\PayloadTooLargeException; |
|
36
|
|
|
use Apple\ApnPush\Exception\SendMessage\ServiceUnavailableException; |
|
37
|
|
|
use Apple\ApnPush\Exception\SendMessage\ShutdownException; |
|
38
|
|
|
use Apple\ApnPush\Exception\SendMessage\TooManyProviderTokenUpdatesException; |
|
39
|
|
|
use Apple\ApnPush\Exception\SendMessage\TooManyRequestsException; |
|
40
|
|
|
use Apple\ApnPush\Exception\SendMessage\TopicDisallowedException; |
|
41
|
|
|
use Apple\ApnPush\Exception\SendMessage\UndefinedErrorException; |
|
42
|
|
|
use Apple\ApnPush\Exception\SendMessage\UnregisteredException; |
|
43
|
|
|
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactory; |
|
44
|
|
|
use Apple\ApnPush\Protocol\Http\Response; |
|
45
|
|
|
use PHPUnit\Framework\TestCase; |
|
46
|
|
|
|
|
47
|
|
|
class ExceptionFactoryTest extends TestCase |
|
48
|
|
|
{ |
|
49
|
|
|
/** |
|
50
|
|
|
* @var ExceptionFactory |
|
51
|
|
|
*/ |
|
52
|
|
|
private $exceptionFactory; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function setUp() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->exceptionFactory = new ExceptionFactory(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
* |
|
65
|
|
|
* @expectedException \Apple\ApnPush\Exception\SendMessage\MissingContentInResponseException |
|
66
|
|
|
* @expectedExceptionMessage Missing content in response. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function shouldFailIfContentNotFound() |
|
69
|
|
|
{ |
|
70
|
|
|
$response = new Response(400, ''); |
|
71
|
|
|
throw $this->exceptionFactory->create($response); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @test |
|
76
|
|
|
* |
|
77
|
|
|
* @expectedException \Apple\ApnPush\Exception\SendMessage\InvalidResponseException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function shouldFailIfInvalidJson() |
|
80
|
|
|
{ |
|
81
|
|
|
$response = new Response(400, '{"some}'); |
|
82
|
|
|
throw $this->exceptionFactory->create($response); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @test |
|
87
|
|
|
* |
|
88
|
|
|
* @expectedException \Apple\ApnPush\Exception\SendMessage\MissingErrorReasonInResponseException |
|
89
|
|
|
* @expectedExceptionMessage Missing error reason in response. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function shouldFailIfMissingReason() |
|
92
|
|
|
{ |
|
93
|
|
|
$response = new Response(400, '{"key":"value"}'); |
|
94
|
|
|
throw $this->exceptionFactory->create($response); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @test |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $reason |
|
101
|
|
|
* @param \Exception $expectedException |
|
102
|
|
|
* @param array $extra |
|
103
|
|
|
* |
|
104
|
|
|
* @dataProvider provideReasons |
|
105
|
|
|
*/ |
|
106
|
|
|
public function shouldSuccessCreate($reason, \Exception $expectedException, array $extra = []) |
|
107
|
|
|
{ |
|
108
|
|
|
$json = array_merge([ |
|
109
|
|
|
'reason' => $reason, |
|
110
|
|
|
], $extra); |
|
111
|
|
|
|
|
112
|
|
|
$response = new Response(200, json_encode($json)); |
|
113
|
|
|
$exception = $this->exceptionFactory->create($response); |
|
114
|
|
|
|
|
115
|
|
|
self::assertEquals($expectedException, $exception); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Provide reasons |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
public function provideReasons() |
|
124
|
|
|
{ |
|
125
|
|
|
return [ |
|
126
|
|
|
['BadCollapseId', new BadCollapseIdException()], |
|
127
|
|
|
['BadDeviceToken', new BadDeviceTokenException()], |
|
128
|
|
|
['BadExpirationDate', new BadExpirationDateException()], |
|
129
|
|
|
['BadMessageId', new BadMessageIdException()], |
|
130
|
|
|
['BadPriority', new BadPriorityException()], |
|
131
|
|
|
['BadTopic', new BadTopicException()], |
|
132
|
|
|
['DeviceTokenNotForTopic', new DeviceTokenNotForTopicException()], |
|
133
|
|
|
['DuplicateHeaders', new DuplicateHeadersException()], |
|
134
|
|
|
['IdleTimeout', new IdleTimeoutException()], |
|
135
|
|
|
['MissingDeviceToken', new MissingDeviceTokenException()], |
|
136
|
|
|
['MissingTopic', new MissingTopicException()], |
|
137
|
|
|
['PayloadEmpty', new PayloadEmptyException()], |
|
138
|
|
|
['TopicDisallowed', new TopicDisallowedException()], |
|
139
|
|
|
['BadCertificate', new BadCertificateException()], |
|
140
|
|
|
['BadCertificateEnvironment', new BadCertificateEnvironmentException()], |
|
141
|
|
|
['ExpiredProviderToken', new ExpiredProviderTokenException()], |
|
142
|
|
|
['Forbidden', new ForbiddenException()], |
|
143
|
|
|
['InvalidProviderToken', new InvalidProviderTokenException()], |
|
144
|
|
|
['MissingProviderToken', new MissingProviderTokenException()], |
|
145
|
|
|
['BadPath', new BadPathException()], |
|
146
|
|
|
['MethodNotAllowed', new MethodNotAllowedException()], |
|
147
|
|
|
['Unregistered', new UnregisteredException(new \DateTime()), ['timestamp' => (new \DateTime())->format('U')]], |
|
148
|
|
|
['PayloadTooLarge', new PayloadTooLargeException()], |
|
149
|
|
|
['TooManyProviderTokenUpdates', new TooManyProviderTokenUpdatesException()], |
|
150
|
|
|
['TooManyRequests', new TooManyRequestsException()], |
|
151
|
|
|
['InternalServerError', new InternalServerErrorException()], |
|
152
|
|
|
['ServiceUnavailable', new ServiceUnavailableException()], |
|
153
|
|
|
['Shutdown', new ShutdownException()], |
|
154
|
|
|
['some', new UndefinedErrorException()], |
|
155
|
|
|
]; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|