1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshSinchBundle |
4
|
|
|
* |
5
|
|
|
* (c) Artem Genvald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fresh\SinchBundle\Tests\Service; |
12
|
|
|
|
13
|
|
|
use Fresh\SinchBundle\Helper\SinchErrorCode; |
14
|
|
|
use Fresh\SinchBundle\Service\SinchExceptionResolver; |
15
|
|
|
use GuzzleHttp\Exception\ClientException; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* FreshSinchExtensionTest |
20
|
|
|
* |
21
|
|
|
* @author Artem Genvald <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class SinchExceptionResolverTest extends \PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
// region Bad Request exceptions |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test SinchParameterValidationException |
29
|
|
|
*/ |
30
|
|
|
public function testSinchParameterValidationException() |
31
|
|
|
{ |
32
|
|
|
$e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::PARAMETER_VALIDATION); |
33
|
|
|
|
34
|
|
|
$this->assertInstanceOf( |
35
|
|
|
'\Fresh\SinchBundle\Exception\BadRequest\SinchParameterValidationException', |
36
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Test SinchInvalidRequestException |
42
|
|
|
*/ |
43
|
|
|
public function testSinchInvalidRequestException() |
44
|
|
|
{ |
45
|
|
|
$e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::INVALID_REQUEST); |
46
|
|
|
|
47
|
|
|
$this->assertInstanceOf( |
48
|
|
|
'\Fresh\SinchBundle\Exception\BadRequest\SinchInvalidRequestException', |
49
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Test SinchMissingParameterException |
55
|
|
|
*/ |
56
|
|
|
public function testSinchMissingParameterException() |
57
|
|
|
{ |
58
|
|
|
$e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::MISSING_PARAMETER); |
59
|
|
|
|
60
|
|
|
$this->assertInstanceOf( |
61
|
|
|
'\Fresh\SinchBundle\Exception\BadRequest\SinchMissingParameterException', |
62
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// endregion |
67
|
|
|
|
68
|
|
|
// region Unauthorized exceptions |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Test SinchIllegalAuthorizationHeaderException |
72
|
|
|
*/ |
73
|
|
|
public function testSinchIllegalAuthorizationHeaderException() |
74
|
|
|
{ |
75
|
|
|
$e = $this->getClientException(Response::HTTP_UNAUTHORIZED, SinchErrorCode::ILLEGAL_AUTHORIZATION_HEADER); |
76
|
|
|
|
77
|
|
|
$this->assertInstanceOf( |
78
|
|
|
'\Fresh\SinchBundle\Exception\Unauthorized\SinchIllegalAuthorizationHeaderException', |
79
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// endregion |
84
|
|
|
|
85
|
|
|
// region Payment Required exceptions |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test SinchPaymentRequiredException |
89
|
|
|
*/ |
90
|
|
|
public function testSinchPaymentRequiredException() |
91
|
|
|
{ |
92
|
|
|
$e = $this->getClientException(Response::HTTP_PAYMENT_REQUIRED, SinchErrorCode::THERE_IS_NOT_ENOUGH_FUNDS_TO_SEND_THE_MESSAGE); |
93
|
|
|
|
94
|
|
|
$this->assertInstanceOf( |
95
|
|
|
'\Fresh\SinchBundle\Exception\PaymentRequired\SinchPaymentRequiredException', |
96
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// endregion |
101
|
|
|
|
102
|
|
|
// region Forbidden exceptions |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Test SinchForbiddenRequestException |
106
|
|
|
*/ |
107
|
|
|
public function testSinchForbiddenRequestException() |
108
|
|
|
{ |
109
|
|
|
$e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::FORBIDDEN_REQUEST); |
110
|
|
|
|
111
|
|
|
$this->assertInstanceOf( |
112
|
|
|
'\Fresh\SinchBundle\Exception\Forbidden\SinchForbiddenRequestException', |
113
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Test SinchInvalidAuthorizationSchemeException |
119
|
|
|
*/ |
120
|
|
|
public function testSinchInvalidAuthorizationSchemeException() |
121
|
|
|
{ |
122
|
|
|
$e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::INVALID_AUTHORIZATION_SCHEME_FOR_CALLING_THE_METHOD); |
123
|
|
|
|
124
|
|
|
$this->assertInstanceOf( |
125
|
|
|
'\Fresh\SinchBundle\Exception\Forbidden\SinchInvalidAuthorizationSchemeException', |
126
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Test SinchNoVerifiedPhoneNumberException |
132
|
|
|
*/ |
133
|
|
|
public function testSinchNoVerifiedPhoneNumberException() |
134
|
|
|
{ |
135
|
|
|
$e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::NO_VERIFIED_PHONE_NUMBER_ON_YOUR_SINCH_ACCOUNT); |
136
|
|
|
|
137
|
|
|
$this->assertInstanceOf( |
138
|
|
|
'\Fresh\SinchBundle\Exception\Forbidden\SinchNoVerifiedPhoneNumberException', |
139
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::SANDBOX_SMS_ONLY_ALLOWED_TO_BE_SENT_TO_VERIFIED_NUMBERS); |
143
|
|
|
|
144
|
|
|
$this->assertInstanceOf( |
145
|
|
|
'\Fresh\SinchBundle\Exception\Forbidden\SinchNoVerifiedPhoneNumberException', |
146
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// endregion |
151
|
|
|
|
152
|
|
|
// region Internal Server Error exceptions |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Test SinchInternalErrorException |
156
|
|
|
*/ |
157
|
|
|
public function testSinchInternalErrorException() |
158
|
|
|
{ |
159
|
|
|
$e = $this->getClientException(Response::HTTP_INTERNAL_SERVER_ERROR, SinchErrorCode::INTERNAL_ERROR); |
160
|
|
|
|
161
|
|
|
$this->assertInstanceOf( |
162
|
|
|
'\Fresh\SinchBundle\Exception\InternalServerError\SinchInternalErrorException', |
163
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// endregion |
168
|
|
|
|
169
|
|
|
// region Standard exceptions |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Test standard exception |
173
|
|
|
*/ |
174
|
|
|
public function testStandardException() |
175
|
|
|
{ |
176
|
|
|
$e = $this->getClientException(Response::HTTP_GATEWAY_TIMEOUT, 0); |
177
|
|
|
|
178
|
|
|
$this->assertInstanceOf( |
179
|
|
|
'\Exception', |
180
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// endregion |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get client exception |
188
|
|
|
* |
189
|
|
|
* @param int $statusCode Status code |
190
|
|
|
* @param int $errorCode Error code |
191
|
|
|
* |
192
|
|
|
* @return ClientException |
193
|
|
|
*/ |
194
|
|
|
private function getClientException($statusCode, $errorCode) |
195
|
|
|
{ |
196
|
|
|
$request = $this->getMockBuilder('Psr\Http\Message\RequestInterface')->disableOriginalConstructor()->getMock(); |
197
|
|
|
$response = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->disableOriginalConstructor()->getMock(); |
198
|
|
|
$body = $this->getMockBuilder('Psr\Http\Message\StreamInterface')->disableOriginalConstructor()->getMock(); |
199
|
|
|
|
200
|
|
|
$response->expects($this->once())->method('getStatusCode')->willReturn($statusCode); |
201
|
|
|
$response->expects($this->once())->method('getBody')->will($this->returnValue($body)); |
202
|
|
|
|
203
|
|
|
$body->expects($this->once())->method('getContents')->will($this->returnValue(<<<JSON |
204
|
|
|
{ |
205
|
|
|
"errorCode": $errorCode, |
206
|
|
|
"message": "Some message" |
207
|
|
|
} |
208
|
|
|
JSON |
209
|
|
|
)); |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
return new ClientException(null, $request, $response); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|