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
|
|
|
* @see \Fresh\SinchBundle\Service\SinchExceptionResolver |
24
|
|
|
*/ |
25
|
|
|
class SinchExceptionResolverTest extends \PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
// region Bad Request exceptions |
28
|
|
|
|
29
|
|
|
public function testSinchParameterValidationException() |
30
|
|
|
{ |
31
|
|
|
$e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::PARAMETER_VALIDATION); |
32
|
|
|
|
33
|
|
|
$this->assertInstanceOf( |
34
|
|
|
'\Fresh\SinchBundle\Exception\BadRequest\SinchParameterValidationException', |
35
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testSinchInvalidRequestException() |
40
|
|
|
{ |
41
|
|
|
$e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::INVALID_REQUEST); |
42
|
|
|
|
43
|
|
|
$this->assertInstanceOf( |
44
|
|
|
'\Fresh\SinchBundle\Exception\BadRequest\SinchInvalidRequestException', |
45
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testSinchMissingParameterException() |
50
|
|
|
{ |
51
|
|
|
$e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::MISSING_PARAMETER); |
52
|
|
|
|
53
|
|
|
$this->assertInstanceOf( |
54
|
|
|
'\Fresh\SinchBundle\Exception\BadRequest\SinchMissingParameterException', |
55
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// endregion |
60
|
|
|
|
61
|
|
|
// region Unauthorized exceptions |
62
|
|
|
|
63
|
|
|
public function testSinchIllegalAuthorizationHeaderException() |
64
|
|
|
{ |
65
|
|
|
$e = $this->getClientException(Response::HTTP_UNAUTHORIZED, SinchErrorCode::ILLEGAL_AUTHORIZATION_HEADER); |
66
|
|
|
|
67
|
|
|
$this->assertInstanceOf( |
68
|
|
|
'\Fresh\SinchBundle\Exception\Unauthorized\SinchIllegalAuthorizationHeaderException', |
69
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// endregion |
74
|
|
|
|
75
|
|
|
// region Payment Required exceptions |
76
|
|
|
|
77
|
|
|
public function testSinchPaymentRequiredException() |
78
|
|
|
{ |
79
|
|
|
$e = $this->getClientException(Response::HTTP_PAYMENT_REQUIRED, SinchErrorCode::THERE_IS_NOT_ENOUGH_FUNDS_TO_SEND_THE_MESSAGE); |
80
|
|
|
|
81
|
|
|
$this->assertInstanceOf( |
82
|
|
|
'\Fresh\SinchBundle\Exception\PaymentRequired\SinchPaymentRequiredException', |
83
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// endregion |
88
|
|
|
|
89
|
|
|
// region Forbidden exceptions |
90
|
|
|
|
91
|
|
|
public function testSinchForbiddenRequestException() |
92
|
|
|
{ |
93
|
|
|
$e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::FORBIDDEN_REQUEST); |
94
|
|
|
|
95
|
|
|
$this->assertInstanceOf( |
96
|
|
|
'\Fresh\SinchBundle\Exception\Forbidden\SinchForbiddenRequestException', |
97
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testSinchInvalidAuthorizationSchemeException() |
102
|
|
|
{ |
103
|
|
|
$e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::INVALID_AUTHORIZATION_SCHEME_FOR_CALLING_THE_METHOD); |
104
|
|
|
|
105
|
|
|
$this->assertInstanceOf( |
106
|
|
|
'\Fresh\SinchBundle\Exception\Forbidden\SinchInvalidAuthorizationSchemeException', |
107
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testSinchNoVerifiedPhoneNumberException() |
112
|
|
|
{ |
113
|
|
|
$e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::NO_VERIFIED_PHONE_NUMBER_ON_YOUR_SINCH_ACCOUNT); |
114
|
|
|
|
115
|
|
|
$this->assertInstanceOf( |
116
|
|
|
'\Fresh\SinchBundle\Exception\Forbidden\SinchNoVerifiedPhoneNumberException', |
117
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::SANDBOX_SMS_ONLY_ALLOWED_TO_BE_SENT_TO_VERIFIED_NUMBERS); |
121
|
|
|
|
122
|
|
|
$this->assertInstanceOf( |
123
|
|
|
'\Fresh\SinchBundle\Exception\Forbidden\SinchNoVerifiedPhoneNumberException', |
124
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// endregion |
129
|
|
|
|
130
|
|
|
// region Internal Server Error exceptions |
131
|
|
|
|
132
|
|
|
public function testSinchInternalErrorException() |
133
|
|
|
{ |
134
|
|
|
$e = $this->getClientException(Response::HTTP_INTERNAL_SERVER_ERROR, SinchErrorCode::INTERNAL_ERROR); |
135
|
|
|
|
136
|
|
|
$this->assertInstanceOf( |
137
|
|
|
'\Fresh\SinchBundle\Exception\InternalServerError\SinchInternalErrorException', |
138
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// endregion |
143
|
|
|
|
144
|
|
|
// region Standard exceptions |
145
|
|
|
|
146
|
|
|
public function testStandardException() |
147
|
|
|
{ |
148
|
|
|
$e = $this->getClientException(Response::HTTP_GATEWAY_TIMEOUT, 0); |
149
|
|
|
|
150
|
|
|
$this->assertInstanceOf( |
151
|
|
|
'\Exception', |
152
|
|
|
SinchExceptionResolver::createAppropriateSinchException($e) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// endregion |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get client exception |
160
|
|
|
* |
161
|
|
|
* @param int $statusCode Status code |
162
|
|
|
* @param int $errorCode Error code |
163
|
|
|
* |
164
|
|
|
* @return ClientException |
165
|
|
|
*/ |
166
|
|
|
private function getClientException($statusCode, $errorCode) |
167
|
|
|
{ |
168
|
|
|
$request = $this->getMockBuilder('Psr\Http\Message\RequestInterface')->disableOriginalConstructor()->getMock(); |
169
|
|
|
$response = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->disableOriginalConstructor()->getMock(); |
170
|
|
|
$body = $this->getMockBuilder('Psr\Http\Message\StreamInterface')->disableOriginalConstructor()->getMock(); |
171
|
|
|
|
172
|
|
|
$response->expects($this->once())->method('getStatusCode')->willReturn($statusCode); |
173
|
|
|
$response->expects($this->once())->method('getBody')->will($this->returnValue($body)); |
174
|
|
|
|
175
|
|
|
$body->expects($this->once())->method('getContents')->will($this->returnValue(<<<JSON |
176
|
|
|
{ |
177
|
|
|
"errorCode": $errorCode, |
178
|
|
|
"message": "Some message" |
179
|
|
|
} |
180
|
|
|
JSON |
181
|
|
|
)); |
182
|
|
|
|
183
|
|
|
return new ClientException(null, $request, $response); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|