1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yproximite\WannaSpeakBundle\Tests\Api; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\HttpClient\Response\MockResponse; |
9
|
|
|
use Yproximite\WannaSpeakBundle\Api\CallTrackings; |
10
|
|
|
use Yproximite\WannaSpeakBundle\Api\CallTrackingsInterface; |
11
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\DidAlreadyReservedException; |
12
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\DidNotExistsOrNotOwnedException; |
13
|
|
|
use Yproximite\WannaSpeakBundle\Tests\HttpClientTestTrait; |
14
|
|
|
|
15
|
|
|
class CallTrackingsTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
use HttpClientTestTrait; |
18
|
|
|
|
19
|
|
|
public function testGetNumbersDefault(): void |
20
|
|
|
{ |
21
|
|
|
$callTrackings = new CallTrackings( |
22
|
|
|
$this->createHttpClient(new MockResponse( |
23
|
|
|
(string) json_encode($responseArray = [ |
24
|
|
|
'error' => null, |
25
|
|
|
'data' => [ |
26
|
|
|
'dids' => [ |
27
|
|
|
[ |
28
|
|
|
'did' => '3323098XXXX', |
29
|
|
|
'destination' => '3324333XXXX', |
30
|
|
|
'name' => 'The Name', |
31
|
|
|
'customerid' => null, |
32
|
|
|
'tag1' => 'tag 1', |
33
|
|
|
'tag2' => 'tag 2', |
34
|
|
|
'tag3' => '', |
35
|
|
|
'tag4' => '', |
36
|
|
|
'tag5' => '', |
37
|
|
|
'tag6' => '', |
38
|
|
|
'tag7' => '', |
39
|
|
|
'tag8' => '', |
40
|
|
|
'tag9' => '', |
41
|
|
|
'tag10' => '', |
42
|
|
|
'assigneddate' => '2016-11-18 11:36:50', |
43
|
|
|
'startdate' => '2019-10-28 00:00:00', |
44
|
|
|
'enddate' => '0000-00-00 00:00:00', |
45
|
|
|
'email' => '', |
46
|
|
|
'sms' => '', |
47
|
|
|
'emailcondition' => '0', |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
]) |
52
|
|
|
)) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
static::assertSame( |
56
|
|
|
$responseArray, |
57
|
|
|
$callTrackings->getNumbers() |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function testGetNumbersAvailable(): void |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$callTrackings = new CallTrackings( |
64
|
|
|
$this->createHttpClient(new MockResponse( |
65
|
|
|
(string) json_encode($responseArray = [ |
66
|
|
|
'error' => null, |
67
|
|
|
'data' => [ |
68
|
|
|
'dids' => [ |
69
|
|
|
'33176280XXX', |
70
|
|
|
'33178903XXX', |
71
|
|
|
], |
72
|
|
|
], |
73
|
|
|
]) |
74
|
|
|
)) |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
static::assertSame( |
78
|
|
|
$responseArray, |
79
|
|
|
$callTrackings->getNumbers(CallTrackingsInterface::NUMBERS_AVAILABLE) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function testGetNumbersDeleted(): void |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$callTrackings = new CallTrackings( |
86
|
|
|
$this->createHttpClient(new MockResponse( |
87
|
|
|
(string) json_encode($responseArray = [ |
88
|
|
|
'error' => null, |
89
|
|
|
'data' => [ |
90
|
|
|
'dids' => [ |
91
|
|
|
'33176280XXX', |
92
|
|
|
'33178903XXX', |
93
|
|
|
], |
94
|
|
|
], |
95
|
|
|
]) |
96
|
|
|
)) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
static::assertSame( |
100
|
|
|
$responseArray, |
101
|
|
|
$callTrackings->getNumbers(CallTrackingsInterface::NUMBERS_AVAILABLE) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testGetNumbersWithInvalidMethod(): void |
106
|
|
|
{ |
107
|
|
|
$this->expectException(\InvalidArgumentException::class); |
108
|
|
|
$this->expectExceptionMessage('Method "foobar" for listing numbers is valid, valid values are "list", "available", "deleted".'); |
109
|
|
|
|
110
|
|
|
$callTrackings = new CallTrackings( |
111
|
|
|
$this->createHttpClient(/* ... */) |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$callTrackings->getNumbers('foobar'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testAdd(): void |
118
|
|
|
{ |
119
|
|
|
$callTrackings = new CallTrackings( |
120
|
|
|
$this->createHttpClient(new MockResponse( |
121
|
|
|
(string) json_encode($responseData = [ |
122
|
|
|
'error' => null, |
123
|
|
|
'data' => [ |
124
|
|
|
'ok' => true, |
125
|
|
|
'did' => '33176280XXX', |
126
|
|
|
'unique' => false, |
127
|
|
|
], |
128
|
|
|
]) |
129
|
|
|
)) |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
static::assertSame( |
133
|
|
|
$responseData, |
134
|
|
|
$callTrackings->add('33176280XXX', '33700XXYYZZ', 'The calltracking name') |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testAddWhenDidIsAlreadyUsed(): void |
139
|
|
|
{ |
140
|
|
|
$this->expectException(DidAlreadyReservedException::class); |
141
|
|
|
|
142
|
|
|
$callTrackings = new CallTrackings( |
143
|
|
|
$this->createHttpClient(new MockResponse( |
144
|
|
|
(string) json_encode($responseData = [ |
145
|
|
|
'error' => [ |
146
|
|
|
'nb' => 407, |
147
|
|
|
'txt' => 'DID already reserved or not tested', |
148
|
|
|
], |
149
|
|
|
]) |
150
|
|
|
)) |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$callTrackings->add('33176280XXX', '33700XXYYZZ', 'The calltracking name'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
View Code Duplication |
public function testModify(): void |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$callTrackings = new CallTrackings( |
159
|
|
|
$this->createHttpClient(new MockResponse( |
160
|
|
|
(string) json_encode($responseData = [ |
161
|
|
|
'error' => null, |
162
|
|
|
'data' => [ |
163
|
|
|
'ok' => true, |
164
|
|
|
], |
165
|
|
|
]) |
166
|
|
|
)) |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
static::assertSame( |
170
|
|
|
$responseData, |
171
|
|
|
$callTrackings->modify('33176280XXX') |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testModifyUnknown(): void |
176
|
|
|
{ |
177
|
|
|
$this->expectException(DidNotExistsOrNotOwnedException::class); |
178
|
|
|
|
179
|
|
|
$callTrackings = new CallTrackings( |
180
|
|
|
$this->createHttpClient(new MockResponse( |
181
|
|
|
(string) json_encode([ |
182
|
|
|
'error' => [ |
183
|
|
|
'nb' => 407, |
184
|
|
|
'txt' => 'DID not exists or not owned', |
185
|
|
|
], |
186
|
|
|
]) |
187
|
|
|
)) |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$callTrackings->modify('ABCDEF'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
View Code Duplication |
public function testDelete(): void |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$callTrackings = new CallTrackings( |
196
|
|
|
$this->createHttpClient(new MockResponse( |
197
|
|
|
(string) json_encode($responseData = [ |
198
|
|
|
'error' => null, |
199
|
|
|
'data' => [ |
200
|
|
|
'ok' => true, |
201
|
|
|
], |
202
|
|
|
]) |
203
|
|
|
)) |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
static::assertSame( |
207
|
|
|
$responseData, |
208
|
|
|
$callTrackings->delete('33176280XXX') |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function testDeleteUnknown(): void |
213
|
|
|
{ |
214
|
|
|
$this->expectException(DidNotExistsOrNotOwnedException::class); |
215
|
|
|
|
216
|
|
|
$callTrackings = new CallTrackings( |
217
|
|
|
$this->createHttpClient(new MockResponse( |
218
|
|
|
(string) json_encode([ |
219
|
|
|
'error' => [ |
220
|
|
|
'nb' => 407, |
221
|
|
|
'txt' => 'DID not exists or not owned', |
222
|
|
|
], |
223
|
|
|
]) |
224
|
|
|
)) |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
$callTrackings->delete('ABCDEF'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
View Code Duplication |
public function testExpires(): void |
|
|
|
|
231
|
|
|
{ |
232
|
|
|
$callTrackings = new CallTrackings( |
233
|
|
|
$this->createHttpClient(new MockResponse( |
234
|
|
|
(string) json_encode($responseData = [ |
235
|
|
|
'error' => null, |
236
|
|
|
'data' => [ |
237
|
|
|
'ok' => true, |
238
|
|
|
], |
239
|
|
|
]) |
240
|
|
|
)) |
241
|
|
|
); |
242
|
|
|
|
243
|
|
|
static::assertSame( |
244
|
|
|
$responseData, |
245
|
|
|
$callTrackings->expires('33176280XXX', new \DateTime('2020-11-19')) |
246
|
|
|
); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
View Code Duplication |
public function testExpiresUnknown(): void |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
$this->expectException(DidNotExistsOrNotOwnedException::class); |
252
|
|
|
|
253
|
|
|
$callTrackings = new CallTrackings( |
254
|
|
|
$this->createHttpClient(new MockResponse( |
255
|
|
|
(string) json_encode([ |
256
|
|
|
'error' => [ |
257
|
|
|
'nb' => 407, |
258
|
|
|
'txt' => 'DID not exists or not owned', |
259
|
|
|
], |
260
|
|
|
]) |
261
|
|
|
)) |
262
|
|
|
); |
263
|
|
|
|
264
|
|
|
$callTrackings->expires('ABCDEF', new \DateTime('2020-11-19')); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.