Passed
Branch real-time-api (954386)
by James
06:12
created

UrlBuilderTest::testCanCheckIfUrlIsBuilt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
     *
193
     * @throws BuilderLockedException
194
     * @throws URLNotBuiltException
195
     */
196
    public function testCanChangeVersionNumber()
197
    {
198
        $builder = new UrlBuilder();
199
        $builder->create();
200
        $builder->setVersion(22);
201
202
        $this->assertContains('?v=22', $builder->build()->getUrl());
203
    }
204
205
    /**
206
     * @covers \CwsOps\LivePerson\Rest\UrlBuilder::build()
207
     * @covers \CwsOps\LivePerson\Rest\UrlBuilder::getUrl()
208
     * @covers \CwsOps\LivePerson\Rest\UrlBuilder::createUrl()
209
     *
210
     * @throws BuilderLockedException
211
     * @throws URLNotBuiltException
212
     */
213
    public function testCanBuildFullUrl()
214
    {
215
        $builder = new UrlBuilder();
216
217
        $url = $builder->create(true)
218
            ->setDomain('en.liveperson.net')
219
            ->setService('message-history')
220
            ->setAccount('1929283')
221
            ->addActionContext('foo')
222
            ->hasQueryParam(true)
223
            ->addQueryParam('foo', 'bar')
224
            ->setVersion('2912')
225
            ->build()
226
            ->getUrl();
227
228
        $expected = 'https://en.liveperson.net/message-history/api/account/1929283/foo?foo=barv=2912';
229
230
        $this->assertEquals($expected, $url);
231
    }
232
233
    /**
234
     * @covers \CwsOps\LivePerson\Rest\UrlBuilder::isUrlBuilt()
235
     * @throws BuilderLockedException
236
     */
237
    public function testCanCheckIfUrlIsBuilt()
238
    {
239
        $builder = new UrlBuilder();
240
241
        $this->assertFalse($builder->isUrlBuilt());
242
243
        $builder->create(true)
244
            ->setDomain('en.liveperson.net')
245
            ->setService('message-history')
246
            ->setAccount('1929283')
247
            ->addActionContext('foo')
248
            ->hasQueryParam(true)
249
            ->addQueryParam('foo', 'bar')
250
            ->setVersion('2912')
251
            ->build();
252
253
        $this->assertTrue($builder->isUrlBuilt());
254
    }
255
256
    /**
257
     * @covers \CwsOps\LivePerson\Rest\BuilderLockedException
258
     * @covers \CwsOps\LivePerson\Rest\UrlBuilder::isValid()
259
     * @covers \CwsOps\LivePerson\Rest\UrlBuilder::addToUrl()
260
     *
261
     * @throws BuilderLockedException
262
     */
263
    public function testThrowsBuilderLockedException()
264
    {
265
        $builder = new UrlBuilder();
266
267
        $url = $builder->create(true)
268
            ->setDomain('en.liveperson.net')
269
            ->setService('message-history')
270
            ->setAccount('1929283')
271
            ->addActionContext('foo')
272
            ->hasQueryParam(true)
273
            ->addQueryParam('foo', 'bar')
274
            ->setVersion('2912')
275
            ->build();
276
277
        $this->expectException(BuilderLockedException::class);
278
        $this->expectExceptionMessage('The URLBuilder is currently locked');
279
        $this->expectExceptionCode(500);
280
281
        try {
282
            $url->setDomain('en.liveperson.net');
283
        } catch (\Exception $e) {
284
            $this->assertInstanceOf(BuilderLockedException::class, $e);
285
        }
286
287
288
        $this->expectException(BuilderLockedException::class);
289
        $url->setAction('s');
290
291
        $this->expectException(BuilderLockedException::class);
292
        $url->setService('message-history');
293
294
        $this->expectException(BuilderLockedException::class);
295
        $url->setAccount('1929283');
296
297
        $this->expectException(BuilderLockedException::class);
298
        $url->addActionContext('foo');
299
300
        $this->expectException(BuilderLockedException::class);
301
        $url->hasQueryParam(true);
302
303
        $this->expectException(BuilderLockedException::class);
304
        $url->addQueryParam('foo', 'bar');
305
306
        $this->expectException(BuilderLockedException::class);
307
        $url->setVersion('2912');
308
    }
309
310
    /**
311
     * @covers \CwsOps\LivePerson\Rest\URLNotBuiltException
312
     *
313
     * @throws URLNotBuiltException
314
     */
315
    public function testThrowsUrlNotBuiltException()
316
    {
317
        $builder = new UrlBuilder();
318
319
        $this->expectException(URLNotBuiltException::class);
320
321
        $builder->getUrl();
322
    }
323
}
324