1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: james |
5
|
|
|
* Date: 12/07/2018 |
6
|
|
|
* Time: 21:45 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace CwsOps\LivePerson\Tests; |
10
|
|
|
|
11
|
|
|
use CwsOps\LivePerson\Rest\BuilderLockedException; |
12
|
|
|
use CwsOps\LivePerson\Rest\UrlBuilder; |
13
|
|
|
use CwsOps\LivePerson\Rest\URLNotBuiltException; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @codeCoverageIgnore |
18
|
|
|
* Class UrlBuilderTest |
19
|
|
|
* |
20
|
|
|
* |
21
|
|
|
* |
22
|
|
|
* @coversDefaultClass \CwsOps\LivePerson\Rest\UrlBuilder |
23
|
|
|
* |
24
|
|
|
* @package CwsOps\LivePerson\Tests |
25
|
|
|
*/ |
26
|
|
|
class UrlBuilderTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
public function testCanInitWithNoOptions() |
30
|
|
|
{ |
31
|
|
|
$builder = new UrlBuilder(); |
32
|
|
|
|
33
|
|
|
$this->assertInstanceOf(UrlBuilder::class, $builder); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::create() |
38
|
|
|
* |
39
|
|
|
* @throws BuilderLockedException |
40
|
|
|
*/ |
41
|
|
|
public function testReturnsInstanceOfSelf() |
42
|
|
|
{ |
43
|
|
|
$builder = new UrlBuilder(); |
44
|
|
|
|
45
|
|
|
$this->assertInstanceOf(UrlBuilder::class, $builder->create(true)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::build() |
50
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::getUrl() |
51
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::create() |
52
|
|
|
* |
53
|
|
|
* @throws BuilderLockedException |
54
|
|
|
* @throws URLNotBuiltException |
55
|
|
|
*/ |
56
|
|
|
public function testCanBuildAndCreate() |
57
|
|
|
{ |
58
|
|
|
$builder = new UrlBuilder(); |
59
|
|
|
$builder->create(); |
60
|
|
|
|
61
|
|
|
$this->assertStringStartsWith('http', $builder->build()->getUrl()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::build() |
66
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::getUrl() |
67
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::create() |
68
|
|
|
* |
69
|
|
|
* @throws BuilderLockedException |
70
|
|
|
* @throws URLNotBuiltException |
71
|
|
|
*/ |
72
|
|
|
public function testCanBuildSecureUrl() |
73
|
|
|
{ |
74
|
|
|
$builder = new UrlBuilder(); |
75
|
|
|
$builder->create(); |
76
|
|
|
|
77
|
|
|
$this->assertStringStartsWith('https', $builder->build()->getUrl()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::setDomain() |
82
|
|
|
* |
83
|
|
|
* @throws BuilderLockedException |
84
|
|
|
* @throws URLNotBuiltException |
85
|
|
|
*/ |
86
|
|
|
public function testCanSetDomain() |
87
|
|
|
{ |
88
|
|
|
$builder = new UrlBuilder(); |
89
|
|
|
$builder->create(); |
90
|
|
|
$builder->setDomain('testDomain'); |
91
|
|
|
|
92
|
|
|
$this->assertContains('testDomain', $builder->build()->getUrl()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::setService() |
97
|
|
|
* |
98
|
|
|
* @throws BuilderLockedException |
99
|
|
|
* @throws URLNotBuiltException |
100
|
|
|
*/ |
101
|
|
|
public function testCanSetService() |
102
|
|
|
{ |
103
|
|
|
$builder = new UrlBuilder(); |
104
|
|
|
$builder->create(); |
105
|
|
|
$builder->setService('testService'); |
106
|
|
|
|
107
|
|
|
$this->assertContains('testService', $builder->build()->getUrl()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::setAction() |
112
|
|
|
* |
113
|
|
|
* @throws BuilderLockedException |
114
|
|
|
* @throws URLNotBuiltException |
115
|
|
|
*/ |
116
|
|
|
public function testCanSetAction() |
117
|
|
|
{ |
118
|
|
|
$builder = new UrlBuilder(); |
119
|
|
|
$builder->create(); |
120
|
|
|
$builder->setAction('testAction'); |
121
|
|
|
|
122
|
|
|
$this->assertContains('testAction', $builder->build()->getUrl()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::setAccount() |
128
|
|
|
* |
129
|
|
|
* @throws BuilderLockedException |
130
|
|
|
* @throws URLNotBuiltException |
131
|
|
|
*/ |
132
|
|
|
public function testCanSetAccount() |
133
|
|
|
{ |
134
|
|
|
$builder = new UrlBuilder(); |
135
|
|
|
$builder->create(); |
136
|
|
|
$builder->setAccount('128182'); |
137
|
|
|
|
138
|
|
|
$this->assertContains('128182', $builder->build()->getUrl()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::addActionContext() |
143
|
|
|
* |
144
|
|
|
* @throws BuilderLockedException |
145
|
|
|
* @throws URLNotBuiltException |
146
|
|
|
*/ |
147
|
|
|
public function testCanSetActionContext() |
148
|
|
|
{ |
149
|
|
|
$builder = new UrlBuilder(); |
150
|
|
|
$builder->create(); |
151
|
|
|
$builder->addActionContext('testActionContext'); |
152
|
|
|
|
153
|
|
|
$this->assertContains('testActionContext', $builder->build()->getUrl()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::addQueryParam() |
158
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::hasQueryParam() |
159
|
|
|
* |
160
|
|
|
* @throws BuilderLockedException |
161
|
|
|
* @throws URLNotBuiltException |
162
|
|
|
*/ |
163
|
|
|
public function testCanAddAdditionalParams() |
164
|
|
|
{ |
165
|
|
|
$builder = new UrlBuilder(); |
166
|
|
|
$builder->create(); |
167
|
|
|
$builder->hasQueryParam(true); |
168
|
|
|
$builder->addQueryParam('foo', 'bar'); |
169
|
|
|
|
170
|
|
|
$this->assertContains('?foo=bar', $builder->build()->getUrl()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::hasQueryParam() |
175
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::addQueryParam() |
176
|
|
|
* |
177
|
|
|
* @throws BuilderLockedException |
178
|
|
|
* @throws URLNotBuiltException |
179
|
|
|
*/ |
180
|
|
|
public function testCanSetHasQueryParamsToFalse() |
181
|
|
|
{ |
182
|
|
|
$builder = new UrlBuilder(); |
183
|
|
|
$builder->create(); |
184
|
|
|
$builder->hasQueryParam(false); |
185
|
|
|
$builder->addQueryParam('foo', 'bar'); |
186
|
|
|
|
187
|
|
|
$this->assertNotContains('?foo=bar', $builder->build()->getUrl()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::setVersion() |
192
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::createUrl() |
193
|
|
|
* |
194
|
|
|
* @throws BuilderLockedException |
195
|
|
|
* @throws URLNotBuiltException |
196
|
|
|
*/ |
197
|
|
|
public function testCanChangeVersionNumber() |
198
|
|
|
{ |
199
|
|
|
$builder = new UrlBuilder(); |
200
|
|
|
$builder->create(); |
201
|
|
|
$builder->setVersion(22); |
202
|
|
|
|
203
|
|
|
$this->assertContains('?v=22', $builder->build()->getUrl()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::build() |
208
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::getUrl() |
209
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::createUrl() |
210
|
|
|
* |
211
|
|
|
* @throws BuilderLockedException |
212
|
|
|
* @throws URLNotBuiltException |
213
|
|
|
*/ |
214
|
|
|
public function testCanBuildFullUrl() |
215
|
|
|
{ |
216
|
|
|
$builder = new UrlBuilder(); |
217
|
|
|
|
218
|
|
|
$url = $builder->create(true) |
219
|
|
|
->setDomain('en.liveperson.net') |
220
|
|
|
->setService('message-history') |
221
|
|
|
->setAccount('1929283') |
222
|
|
|
->addActionContext('foo') |
223
|
|
|
->hasQueryParam(true) |
224
|
|
|
->addQueryParam('foo', 'bar') |
225
|
|
|
->setVersion('2912') |
226
|
|
|
->build() |
227
|
|
|
->getUrl(); |
228
|
|
|
|
229
|
|
|
$expected = 'https://en.liveperson.net/message-history/api/account/1929283/foo?foo=barv=2912'; |
230
|
|
|
|
231
|
|
|
$this->assertEquals($expected, $url); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::isUrlBuilt() |
236
|
|
|
* @throws BuilderLockedException |
237
|
|
|
*/ |
238
|
|
|
public function testCanCheckIfUrlIsBuilt() |
239
|
|
|
{ |
240
|
|
|
$builder = new UrlBuilder(); |
241
|
|
|
|
242
|
|
|
$this->assertFalse($builder->isUrlBuilt()); |
243
|
|
|
|
244
|
|
|
$builder->create(true) |
245
|
|
|
->setDomain('en.liveperson.net') |
246
|
|
|
->setService('message-history') |
247
|
|
|
->setAccount('1929283') |
248
|
|
|
->addActionContext('foo') |
249
|
|
|
->hasQueryParam(true) |
250
|
|
|
->addQueryParam('foo', 'bar') |
251
|
|
|
->setVersion('2912') |
252
|
|
|
->build(); |
253
|
|
|
|
254
|
|
|
$this->assertTrue($builder->isUrlBuilt()); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @covers \CwsOps\LivePerson\Rest\BuilderLockedException |
259
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::isValid() |
260
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::addToUrl() |
261
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::setAction() |
262
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::setService() |
263
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::setAccount() |
264
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::hasQueryParam() |
265
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::addQueryParam() |
266
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::setVersion() |
267
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::setDomain() |
268
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::create() |
269
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::addActionContext() |
270
|
|
|
* |
271
|
|
|
* |
272
|
|
|
* @throws BuilderLockedException |
273
|
|
|
*/ |
274
|
|
|
public function testThrowsBuilderLockedException() |
275
|
|
|
{ |
276
|
|
|
$builder = new UrlBuilder(); |
277
|
|
|
|
278
|
|
|
$url = $builder->create(true) |
279
|
|
|
->setDomain('en.liveperson.net') |
280
|
|
|
->setService('message-history') |
281
|
|
|
->setAccount('1929283') |
282
|
|
|
->addActionContext('foo') |
283
|
|
|
->hasQueryParam(true) |
284
|
|
|
->addQueryParam('foo', 'bar') |
285
|
|
|
->setVersion('2912') |
286
|
|
|
->build(); |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
try { |
290
|
|
|
$url->create(); |
291
|
|
|
} catch (\Exception $e) { |
292
|
|
|
$this->assertInstanceOf(BuilderLockedException::class, $e); |
293
|
|
|
$this->assertEquals('The URLBuilder is currently locked', $e->getMessage()); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
try { |
297
|
|
|
$url->setDomain('en.liveperson.net'); |
298
|
|
|
} catch (\Exception $e) { |
299
|
|
|
$this->assertInstanceOf(BuilderLockedException::class, $e); |
300
|
|
|
$this->assertEquals('The URLBuilder is currently locked', $e->getMessage()); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
try { |
305
|
|
|
$url->setAction('en.liveperson.net'); |
306
|
|
|
} catch (\Exception $e) { |
307
|
|
|
$this->assertInstanceOf(BuilderLockedException::class, $e); |
308
|
|
|
$this->assertEquals('The URLBuilder is currently locked', $e->getMessage()); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
try { |
312
|
|
|
$url->setService('en.liveperson.net'); |
313
|
|
|
} catch (\Exception $e) { |
314
|
|
|
$this->assertInstanceOf(BuilderLockedException::class, $e); |
315
|
|
|
$this->assertEquals('The URLBuilder is currently locked', $e->getMessage()); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
try { |
319
|
|
|
$url->setAccount('en.liveperson.net'); |
320
|
|
|
} catch (\Exception $e) { |
321
|
|
|
$this->assertInstanceOf(BuilderLockedException::class, $e); |
322
|
|
|
$this->assertEquals('The URLBuilder is currently locked', $e->getMessage()); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
try { |
326
|
|
|
$url->addActionContext('en.liveperson.net'); |
327
|
|
|
} catch (\Exception $e) { |
328
|
|
|
$this->assertInstanceOf(BuilderLockedException::class, $e); |
329
|
|
|
$this->assertEquals('The URLBuilder is currently locked', $e->getMessage()); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
try { |
333
|
|
|
$url->hasQueryParam(true); |
334
|
|
|
} catch (\Exception $e) { |
335
|
|
|
$this->assertInstanceOf(BuilderLockedException::class, $e); |
336
|
|
|
$this->assertEquals('The URLBuilder is currently locked', $e->getMessage()); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
try { |
340
|
|
|
$url->addQueryParam('foo', 'bar'); |
341
|
|
|
} catch (\Exception $e) { |
342
|
|
|
$this->assertInstanceOf(BuilderLockedException::class, $e); |
343
|
|
|
$this->assertEquals('The URLBuilder is currently locked', $e->getMessage()); |
344
|
|
|
} |
345
|
|
|
try { |
346
|
|
|
$url->setVersion('2912'); |
347
|
|
|
} catch (\Exception $e) { |
348
|
|
|
$this->assertInstanceOf(BuilderLockedException::class, $e); |
349
|
|
|
$this->assertEquals('The URLBuilder is currently locked', $e->getMessage()); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @covers \CwsOps\LivePerson\Rest\URLNotBuiltException |
355
|
|
|
* @covers \CwsOps\LivePerson\Rest\UrlBuilder::getUrl() |
356
|
|
|
* |
357
|
|
|
*/ |
358
|
|
|
public function testThrowsUrlNotBuiltException() |
359
|
|
|
{ |
360
|
|
|
$builder = new UrlBuilder(); |
361
|
|
|
|
362
|
|
|
try { |
363
|
|
|
$builder->getUrl(); |
364
|
|
|
} catch (\Exception $e) { |
365
|
|
|
$this->assertInstanceOf(URLNotBuiltException::class, $e); |
366
|
|
|
$this->assertEquals('the URL has not been built, you need to call UrlBuilder::build() before getting the URL', $e->getMessage()); |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|