GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RequestTest::testCanAddCookies()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 35
rs 8.8571
c 2
b 0
f 1
cc 1
eloc 27
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the php-phantomjs.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
namespace JonnyW\PhantomJs\Tests\Unit\Http;
10
11
use JonnyW\PhantomJs\Http\Request;
12
use JonnyW\PhantomJs\Http\RequestInterface;
13
14
/**
15
 * PHP PhantomJs
16
 *
17
 * @author Jon Wenmoth <[email protected]>
18
 */
19
class RequestTest extends \PHPUnit_Framework_TestCase
20
{
21
22
/** +++++++++++++++++++++++++++++++++++ **/
23
/** ++++++++++++++ TESTS ++++++++++++++ **/
24
/** +++++++++++++++++++++++++++++++++++ **/
25
26
    /**
27
     * Test default type is returned by default
28
     * if no type is set.
29
     *
30
     * @access public
31
     * @return void
32
     */
33
    public function testDefaultTypeIsReturnedByDefaultIfNotTypeIsSet()
34
    {
35
        $request = $this->getRequest();
36
37
        $this->assertEquals(RequestInterface::REQUEST_TYPE_DEFAULT, $request->getType());
38
    }
39
40
    /**
41
     * Test custom type can be set.
42
     *
43
     * @access public
44
     * @return void
45
     */
46
    public function testCustomTypeCanBeSet()
47
    {
48
        $requestType = 'testType';
49
50
        $request = $this->getRequest();
51
        $request->setType($requestType);
52
53
        $this->assertEquals($requestType, $request->getType());
54
    }
55
56
    /**
57
     * Test URL can be set via constructor.
58
     *
59
     * @access public
60
     * @return void
61
     */
62
    public function testUrlCanBeSetViaConstructor()
63
    {
64
        $url            = 'http://test.com';
65
        $request = $this->getRequest($url);
66
67
        $this->assertEquals($url, $request->getUrl());
68
    }
69
70
    /**
71
     * Test method can be set via constructor.
72
     *
73
     * @access public
74
     * @return void
75
     */
76
    public function testMethodCanBeSetViaConstructor()
77
    {
78
        $method         = 'GET';
79
        $request = $this->getRequest(null, $method);
80
81
        $this->assertEquals($method, $request->getMethod());
82
    }
83
84
    /**
85
     * Test timeout can be set via constructor.
86
     *
87
     * @access public
88
     * @return void
89
     */
90
    public function testTimeoutCanBeSetViaConstructor()
91
    {
92
        $timeout        = 100000;
93
        $request = $this->getRequest('http://test.com', 'GET', $timeout);
94
95
        $this->assertEquals($timeout, $request->getTimeout());
96
    }
97
98
    /**
99
     * Test invalid method is thrown if method
100
     * is invalid.
101
     *
102
     * @access public
103
     * @return void
104
     */
105
    public function testInvalidMethodIsThrownIfMethodIsInvalid()
106
    {
107
        $this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidMethodException');
108
109
        $request = $this->getRequest();
110
        $request->setMethod('INVALID_METHOD');
111
    }
112
113
    /**
114
     * Test URL does not contain query params if
115
     * mehtod is not HEAD or GET.
116
     *
117
     * @access public
118
     * @return void
119
     */
120
    public function testUrlDoesNotContainQueryParamsIfMethodIsNotHeadOrGet()
121
    {
122
        $url = 'http://test.com';
123
124
        $data = array(
125
            'test_param1' => 'Testing1',
126
            'test_param2' => 'Testing2'
127
        );
128
129
        $request = $this->getRequest();
130
        $request->setMethod('POST');
131
        $request->setUrl($url);
132
        $request->setRequestData($data);
133
134
        $this->assertEquals($url, $request->getUrl());
135
    }
136
137
    /**
138
     * Test URL does contain query params if mehthod
139
     * is GET.
140
     *
141
     * @access public
142
     * @return void
143
     */
144
    public function testUrlDoesContainQueryParamsIfMethodIsGet()
145
    {
146
        $url = 'http://test.com';
147
148
        $data = array(
149
            'test_param1' => 'Testing1',
150
            'test_param2' => 'Testing2'
151
        );
152
153
        $request = $this->getRequest();
154
        $request->setMethod('GET');
155
        $request->setUrl($url);
156
        $request->setRequestData($data);
157
158
        $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
159
160
        $this->assertEquals($expectedUrl, $request->getUrl());
161
    }
162
163
    /**
164
     * Test URL does contain query params if method
165
     * is HEAD.
166
     *
167
     * @access public
168
     * @return void
169
     */
170
    public function testUrlDoesContainQueryParamsIfMethodIsHead()
171
    {
172
        $url = 'http://test.com';
173
174
        $data = array(
175
            'test_param1' => 'Testing1',
176
            'test_param2' => 'Testing2'
177
        );
178
179
        $request = $this->getRequest();
180
        $request->setMethod('HEAD');
181
        $request->setUrl($url);
182
        $request->setRequestData($data);
183
184
        $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
185
186
        $this->assertEquals($expectedUrl, $request->getUrl());
187
    }
188
189
    /**
190
     * Test query params are appended to URL if
191
     * URL contains existng query params.
192
     *
193
     * @access public
194
     * @return void
195
     */
196
    public function testQueryParamsAreAppendedToUrlIfUrlContainsExistingQueryParams()
197
    {
198
        $url = 'http://test.com?existing_param=Existing';
199
200
        $data = array(
201
            'test_param1' => 'Testing1',
202
            'test_param2' => 'Testing2'
203
        );
204
205
        $request = $this->getRequest();
206
        $request->setMethod('GET');
207
        $request->setUrl($url);
208
        $request->setRequestData($data);
209
210
        $expectedUrl = $url . '&test_param1=Testing1&test_param2=Testing2';
211
212
        $this->assertEquals($expectedUrl, $request->getUrl());
213
    }
214
215
    /**
216
     * Test request contains no body if method
217
     * is GET.
218
     *
219
     * @access public
220
     * @return void
221
     */
222
    public function testRequestContainsNoBodyIfMethodIsGet()
223
    {
224
        $data = array(
225
            'test_param1' => 'Testing1',
226
            'test_param2' => 'Testing2'
227
        );
228
229
        $request = $this->getRequest();
230
        $request->setMethod('GET');
231
        $request->setRequestData($data);
232
233
        $this->assertEquals('', $request->getBody());
234
    }
235
236
    /**
237
     * Test request contains no body if method
238
     * is HEAD.
239
     *
240
     * @access public
241
     * @return void
242
     */
243
    public function testRequestContainsNoBodyIfMethodIsHead()
244
    {
245
        $data = array(
246
            'test_param1' => 'Testing1',
247
            'test_param2' => 'Testing2'
248
        );
249
250
        $request = $this->getRequest();
251
        $request->setMethod('HEAD');
252
        $request->setRequestData($data);
253
254
        $this->assertEquals('', $request->getBody());
255
    }
256
257
    /**
258
     * Test request contains a body if method is
259
     * not HEAD or GET.
260
     *
261
     * @access public
262
     * @return void
263
     */
264
    public function testRequestContainsABodyIfMethodIsNotHeadOrGet()
265
    {
266
        $data = array(
267
            'test_param1' => 'Testing1',
268
            'test_param2' => 'Testing2'
269
        );
270
271
        $request = $this->getRequest();
272
        $request->setMethod('POST');
273
        $request->setRequestData($data);
274
275
        $body = 'test_param1=Testing1&test_param2=Testing2';
276
277
        $this->assertEquals($body, $request->getBody());
278
    }
279
280
    /**
281
     * Test request data can be flattened.
282
     *
283
     * @access public
284
     * @return void
285
     */
286
    public function testRequestDataCanBeFalttened()
287
    {
288
        $data = array(
289
            'test_param1' => 'Testing1',
290
            'test_param2' => array(
291
                'Testing2',
292
                'Testing3'
293
            )
294
        );
295
296
        $request = $this->getRequest();
297
        $request->setRequestData($data);
298
299
        $flatData = array(
300
            'test_param1'    => 'Testing1',
301
            'test_param2[0]' => 'Testing2',
302
            'test_param2[1]' => 'Testing3'
303
        );
304
305
        $this->assertEquals($flatData, $request->getRequestData(true));
306
    }
307
308
    /**
309
     * Test raw request data can be accessed.
310
     *
311
     * @access public
312
     * @return void
313
     */
314
    public function testRawRequestDataCanBeAccessed()
315
    {
316
        $data = array(
317
            'test_param1' => 'Testing1',
318
            'test_param2' => array(
319
                'Testing2',
320
                'Testing3'
321
            )
322
        );
323
324
        $request = $this->getRequest();
325
        $request->setRequestData($data);
326
327
        $this->assertEquals($data, $request->getRequestData(false));
328
    }
329
330
    /**
331
     * Test headers can be added.
332
     *
333
     * @access public
334
     * @return void
335
     */
336
    public function testHeadersCanBeAdded()
337
    {
338
        $existingHeaders = array(
339
            'Header1' => 'Header 1'
340
        );
341
342
        $newHeaders = array(
343
            'Header2' => 'Header 2',
344
            'Header3' => 'Header 3'
345
        );
346
347
        $request = $this->getRequest();
348
        $request->setHeaders($existingHeaders);
349
        $request->addHeaders($newHeaders);
350
351
        $expectedHeaders = array_merge($existingHeaders, $newHeaders);
352
353
        $this->assertEquals($expectedHeaders, $request->getHeaders());
354
    }
355
356
    /**
357
     * Test headers can be accessed in
358
     * JSON format
359
     *
360
     * @access public
361
     * @return void
362
     */
363
    public function testHeadersCanBeAccessedInJsonFormat()
364
    {
365
        $headers = array(
366
            'Header1' => 'Header 1',
367
            'Header2' => 'Header 2'
368
        );
369
370
        $request = $this->getRequest();
371
        $request->setHeaders($headers);
372
373
        $expectedHeaders = json_encode($headers);
374
375
        $this->assertEquals($expectedHeaders, $request->getHeaders('json'));
376
    }
377
378
    /**
379
     * Test raw headers can be accessed.
380
     *
381
     * @access public
382
     * @return void
383
     */
384
    public function testRawHeadersCanBeAccessed()
385
    {
386
        $headers = array(
387
            'Header1' => 'Header 1',
388
            'Header2' => 'Header 2'
389
        );
390
391
        $request = $this->getRequest();
392
        $request->setHeaders($headers);
393
394
        $this->assertEquals($headers, $request->getHeaders('default'));
395
    }
396
397
    /**
398
     * Test can add setting.
399
     *
400
     * @access public
401
     * @return void
402
     */
403
    public function testCanAddSetting()
404
    {
405
        $request = $this->getRequest();
406
        $request->addSetting('userAgent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36');
407
        $request->addSetting('localToRemoteUrlAccessEnabled', 'true');
408
        $request->addSetting('resourceTimeout', 3000);
409
410
        $expected = array(
411
            'userAgent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
412
            'localToRemoteUrlAccessEnabled' => 'true',
413
            'resourceTimeout' => 3000
414
        );
415
416
        $this->assertEquals($expected, $request->getSettings());
417
    }
418
419
    /**
420
     * Test set timeout sets resource
421
     * timeout in settings
422
     *
423
     * @access public
424
     * @return void
425
     */
426
    public function testSetTimeoutSetsResourceTimeoutInSettings()
427
    {
428
        $request = $this->getRequest();
429
        $request->setTimeout(1000);
430
431
        $expected = array(
432
            'resourceTimeout' => 1000
433
        );
434
435
        $this->assertEquals($expected, $request->getSettings());
436
    }
437
438
    /**
439
     * Test can add cookies.
440
     *
441
     * @access public
442
     * @return void
443
     */
444
    public function testCanAddCookies()
445
    {
446
        $name     = 'test_cookie';
447
        $value    = 'TESTING_COOKIES';
448
        $path     = '/';
449
        $domain   = 'localhost';
450
        $httpOnly =  false;
451
        $secure   = true;
452
        $expires  = time() + 3600;
453
454
        $request = $this->getRequest();
455
        $request->addCookie(
456
            $name,
457
            $value,
458
            $path,
459
            $domain,
460
            $httpOnly,
461
            $secure,
462
            $expires
463
        );
464
465
        $expected = array(
466
            'name'     => $name,
467
            'value'    => $value,
468
            'path'     => $path,
469
            'domain'   => $domain,
470
            'httponly' => $httpOnly,
471
            'secure'   => $secure,
472
            'expires'  => $expires
473
        );
474
        
475
        $cookies = $request->getCookies();
476
        
477
        $this->assertEquals(array($expected), $cookies['add']);
478
    }
479
480
    /**
481
     * Test can delete cookies.
482
     *
483
     * @access public
484
     * @return void
485
     */
486
    public function testCanDeleteCookies()
487
    {
488
        $name     = 'test_cookie';
489
        $value    = 'TESTING_COOKIES';
490
        $path     = '/';
491
        $domain   = 'localhost';
492
        $httpOnly =  false;
493
        $secure   = true;
494
        $expires  = time() + 3600;
495
496
        $request = $this->getRequest();
497
        $request->addCookie(
498
            $name,
499
            $value,
500
            $path,
501
            $domain,
502
            $httpOnly,
503
            $secure,
504
            $expires
505
        );
506
507
        $request->deleteCookie($name);
508
509
        $cookies = $request->getCookies();
510
511
        $this->assertEquals(array($name), $cookies['delete']);
512
    }
513
514
    /**
515
     * Test can set viewport width.
516
     *
517
     * @access public
518
     * @return void
519
     */
520
    public function testCanSetViewportWidth()
521
    {
522
        $width  = 100;
523
        $height = 200;
524
525
        $request = $this->getRequest();
526
        $request->setViewportSize($width, $height);
527
528
        $this->assertEquals($width, $request->getViewportWidth());
529
    }
530
531
    /**
532
     * Test can set viewport height.
533
     *
534
     * @access public
535
     * @return void
536
     */
537
    public function testCanSetViewportHeight()
538
    {
539
        $width  = 100;
540
        $height = 200;
541
542
        $request = $this->getRequest();
543
        $request->setViewportSize($width, $height);
544
545
        $this->assertEquals($height, $request->getViewportHeight());
546
    }
547
548
/** +++++++++++++++++++++++++++++++++++ **/
549
/** ++++++++++ TEST ENTITIES ++++++++++ **/
550
/** +++++++++++++++++++++++++++++++++++ **/
551
552
    /**
553
     * Get request instance.
554
     *
555
     * @access protected
556
     * @param  string                         $url     (default: null)
557
     * @param  string                         $method  (default: RequestInterface::METHOD_GET)
558
     * @param  int                            $timeout (default: 5000)
559
     * @return \JonnyW\PhantomJs\Http\Request
560
     */
561
    protected function getRequest($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
562
    {
563
        $request = new Request($url, $method, $timeout);
564
565
        return $request;
566
    }
567
}
568