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.
Completed
Push — master ( 8c0c2d...57a10d )
by Jonny
04:02
created

testMethodCanBeSetViaConstructor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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\CaptureRequest;
12
use JonnyW\PhantomJs\Http\RequestInterface;
13
14
/**
15
 * PHP PhantomJs
16
 *
17
 * @author Jon Wenmoth <[email protected]>
18
 */
19
class CaptureRequestTest extends \PHPUnit_Framework_TestCase
20
{
21
22
/** +++++++++++++++++++++++++++++++++++ **/
23
/** ++++++++++++++ TESTS ++++++++++++++ **/
24
/** +++++++++++++++++++++++++++++++++++ **/
25
26
    /**
27
     * Test capture type is returned by default
28
     * if no type is set.
29
     *
30
     * @access public
31
     * @return void
32
     */
33
    public function testCaptureTypeIsReturnedByDefaultIfNotTypeIsSet()
34
    {
35
        $captureRequest = $this->getCaptureRequest();
36
37
        $this->assertSame(RequestInterface::REQUEST_TYPE_CAPTURE, $captureRequest->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
        $captureRequest = $this->getCaptureRequest();
51
        $captureRequest->setType($requestType);
52
53
        $this->assertSame($requestType, $captureRequest->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
        $captureRequest = $this->getCaptureRequest($url);
66
67
        $this->assertSame($url, $captureRequest->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
        $captureRequest = $this->getCaptureRequest(null, $method);
80
81
        $this->assertSame($method, $captureRequest->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
        $captureRequest = $this->getCaptureRequest('http://test.com', 'GET', $timeout);
94
95
        $this->assertSame($timeout, $captureRequest->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
        $captureRequest = $this->getCaptureRequest();
110
        $captureRequest->setMethod('INVALID_METHOD');
111
    }
112
113
    /**
114
     * Test rect width can be set.
115
     *
116
     * @access public
117
     * @return void
118
     */
119
    public function testRectWidthCanBeSet()
120
    {
121
        $width  = 100;
122
        $height = 200;
123
124
        $captureRequest = $this->getCaptureRequest();
125
        $captureRequest->setCaptureDimensions($width, $height);
126
127
        $this->assertSame($width, $captureRequest->getRectWidth());
128
    }
129
130
    /**
131
     * Test rect height can be set.
132
     *
133
     * @access public
134
     * @return void
135
     */
136
    public function testRectHeightCanBeSet()
137
    {
138
        $width  = 100;
139
        $height = 200;
140
141
        $captureRequest = $this->getCaptureRequest();
142
        $captureRequest->setCaptureDimensions($width, $height);
143
144
        $this->assertSame($height, $captureRequest->getRectHeight());
145
    }
146
147
    /**
148
     * Test rect top can be set.
149
     *
150
     * @access public
151
     * @return void
152
     */
153
    public function testRectTopCanBeSet()
154
    {
155
        $width  = 100;
156
        $height = 200;
157
        $top    = 50;
158
159
        $captureRequest = $this->getCaptureRequest();
160
        $captureRequest->setCaptureDimensions($width, $height, $top);
161
162
        $this->assertSame($top, $captureRequest->getRectTop());
163
    }
164
165
    /**
166
     * Test rect left can be set.
167
     *
168
     * @access public
169
     * @return void
170
     */
171
    public function testRectLeftCanBeSet()
172
    {
173
        $width  = 100;
174
        $height = 200;
175
        $left   = 50;
176
177
        $captureRequest = $this->getCaptureRequest();
178
        $captureRequest->setCaptureDimensions($width, $height, 0, $left);
179
180
        $this->assertSame($left, $captureRequest->getRectLeft());
181
    }
182
183
    /**
184
     * Test invalid URL exception is thrown
185
     * if URL is invalid format.
186
     *
187
     * @access public
188
     * @return void
189
     */
190
    public function testInvalidUrlExceptionIsThrownIfUrlIsInvalidFormat()
191
    {
192
        $this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidUrlException');
193
194
        $captureRequest = $this->getCaptureRequest();
195
        $captureRequest->setUrl('\\AnInvalidUrl');
196
    }
197
198
    /**
199
     * Test URL does not contain query params if
200
     * mehtod is not HEAD or GET.
201
     *
202
     * @access public
203
     * @return void
204
     */
205
    public function testUrlDoesNotContainQueryParamsIfMethodIsNotHeadOrGet()
206
    {
207
        $url = 'http://test.com';
208
209
        $data = array(
210
            'test_param1' => 'Testing1',
211
            'test_param2' => 'Testing2'
212
        );
213
214
        $captureRequest = $this->getCaptureRequest();
215
        $captureRequest->setMethod('POST');
216
        $captureRequest->setUrl($url);
217
        $captureRequest->setRequestData($data);
218
219
        $this->assertSame($url, $captureRequest->getUrl());
220
    }
221
222
    /**
223
     * Test URL does contain query params if mehthod
224
     * is GET.
225
     *
226
     * @access public
227
     * @return void
228
     */
229
    public function testUrlDoesContainQueryParamsIfMethodIsGet()
230
    {
231
        $url = 'http://test.com';
232
233
        $data = array(
234
            'test_param1' => 'Testing1',
235
            'test_param2' => 'Testing2'
236
        );
237
238
        $captureRequest = $this->getCaptureRequest();
239
        $captureRequest->setMethod('GET');
240
        $captureRequest->setUrl($url);
241
        $captureRequest->setRequestData($data);
242
243
        $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
244
245
        $this->assertSame($expectedUrl, $captureRequest->getUrl());
246
    }
247
248
    /**
249
     * Test URL does contain query params if method
250
     * is HEAD.
251
     *
252
     * @access public
253
     * @return void
254
     */
255
    public function testUrlDoesContainQueryParamsIfMethodIsHead()
256
    {
257
        $url = 'http://test.com';
258
259
        $data = array(
260
            'test_param1' => 'Testing1',
261
            'test_param2' => 'Testing2'
262
        );
263
264
        $captureRequest = $this->getCaptureRequest();
265
        $captureRequest->setMethod('HEAD');
266
        $captureRequest->setUrl($url);
267
        $captureRequest->setRequestData($data);
268
269
        $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
270
271
        $this->assertSame($expectedUrl, $captureRequest->getUrl());
272
    }
273
274
    /**
275
     * Test query params are appended to URL if
276
     * URL contains existng query params.
277
     *
278
     * @access public
279
     * @return void
280
     */
281
    public function testQueryParamsAreAppendedToUrlIfUrlContainsExistingQueryParams()
282
    {
283
        $url = 'http://test.com?existing_param=Existing';
284
285
        $data = array(
286
            'test_param1' => 'Testing1',
287
            'test_param2' => 'Testing2'
288
        );
289
290
        $captureRequest = $this->getCaptureRequest();
291
        $captureRequest->setMethod('GET');
292
        $captureRequest->setUrl($url);
293
        $captureRequest->setRequestData($data);
294
295
        $expectedUrl = $url . '&test_param1=Testing1&test_param2=Testing2';
296
297
        $this->assertSame($expectedUrl, $captureRequest->getUrl());
298
    }
299
300
    /**
301
     * Test request contains no body if method
302
     * is GET.
303
     *
304
     * @access public
305
     * @return void
306
     */
307
    public function testRequestContainsNoBodyIfMethodIsGet()
308
    {
309
        $data = array(
310
            'test_param1' => 'Testing1',
311
            'test_param2' => 'Testing2'
312
        );
313
314
        $captureRequest = $this->getCaptureRequest();
315
        $captureRequest->setMethod('GET');
316
        $captureRequest->setRequestData($data);
317
318
        $this->assertSame('', $captureRequest->getBody());
319
    }
320
321
    /**
322
     * Test request contains no body if method
323
     * is HEAD.
324
     *
325
     * @access public
326
     * @return void
327
     */
328
    public function testRequestContainsNoBodyIfMethodIsHead()
329
    {
330
        $data = array(
331
            'test_param1' => 'Testing1',
332
            'test_param2' => 'Testing2'
333
        );
334
335
        $captureRequest = $this->getCaptureRequest();
336
        $captureRequest->setMethod('HEAD');
337
        $captureRequest->setRequestData($data);
338
339
        $this->assertSame('', $captureRequest->getBody());
340
    }
341
342
    /**
343
     * Test request contains a body if method is
344
     * not HEAD or GET.
345
     *
346
     * @access public
347
     * @return void
348
     */
349
    public function testRequestContainsABodyIfMethodIsNotHeadOrGet()
350
    {
351
        $data = array(
352
            'test_param1' => 'Testing1',
353
            'test_param2' => 'Testing2'
354
        );
355
356
        $captureRequest = $this->getCaptureRequest();
357
        $captureRequest->setMethod('POST');
358
        $captureRequest->setRequestData($data);
359
360
        $body = 'test_param1=Testing1&test_param2=Testing2';
361
362
        $this->assertSame($body, $captureRequest->getBody());
363
    }
364
365
    /**
366
     * Test request data can be flattened.
367
     *
368
     * @access public
369
     * @return void
370
     */
371
    public function testRequestDataCanBeFalttened()
372
    {
373
        $data = array(
374
            'test_param1' => 'Testing1',
375
            'test_param2' => array(
376
                'Testing2',
377
                'Testing3'
378
            )
379
        );
380
381
        $captureRequest = $this->getCaptureRequest();
382
        $captureRequest->setRequestData($data);
383
384
        $flatData = array(
385
            'test_param1'    => 'Testing1',
386
            'test_param2[0]' => 'Testing2',
387
            'test_param2[1]' => 'Testing3'
388
        );
389
390
        $this->assertSame($flatData, $captureRequest->getRequestData(true));
391
    }
392
393
    /**
394
     * Test raw request data can be accessed.
395
     *
396
     * @access public
397
     * @return void
398
     */
399
    public function testRawRequestDataCanBeAccessed()
400
    {
401
        $data = array(
402
            'test_param1' => 'Testing1',
403
            'test_param2' => array(
404
                'Testing2',
405
                'Testing3'
406
            )
407
        );
408
409
        $captureRequest = $this->getCaptureRequest();
410
        $captureRequest->setRequestData($data);
411
412
        $this->assertSame($data, $captureRequest->getRequestData(false));
413
    }
414
415
    /**
416
     * Test headers can be added.
417
     *
418
     * @access public
419
     * @return void
420
     */
421
    public function testHeadersCanBeAdded()
422
    {
423
        $existingHeaders = array(
424
            'Header1' => 'Header 1'
425
        );
426
427
        $newHeaders = array(
428
            'Header2' => 'Header 2',
429
            'Header3' => 'Header 3'
430
        );
431
432
        $captureRequest = $this->getCaptureRequest();
433
        $captureRequest->setHeaders($existingHeaders);
434
        $captureRequest->addHeaders($newHeaders);
435
436
        $expectedHeaders = array_merge($existingHeaders, $newHeaders);
437
438
        $this->assertSame($expectedHeaders, $captureRequest->getHeaders());
439
    }
440
441
    /**
442
     * Test headers can be accessed in
443
     * JSON format
444
     *
445
     * @access public
446
     * @return void
447
     */
448
    public function testHeadersCanBeAccessedInJsonFormat()
449
    {
450
        $headers = array(
451
            'Header1' => 'Header 1',
452
            'Header2' => 'Header 2'
453
        );
454
455
        $captureRequest = $this->getCaptureRequest();
456
        $captureRequest->setHeaders($headers);
457
458
        $expectedHeaders = json_encode($headers);
459
460
        $this->assertSame($expectedHeaders, $captureRequest->getHeaders('json'));
461
    }
462
463
    /**
464
     * Test raw headers can be accessed.
465
     *
466
     * @access public
467
     * @return void
468
     */
469
    public function testRawHeadersCanBeAccessed()
470
    {
471
        $headers = array(
472
            'Header1' => 'Header 1',
473
            'Header2' => 'Header 2'
474
        );
475
476
        $captureRequest = $this->getCaptureRequest();
477
        $captureRequest->setHeaders($headers);
478
479
        $this->assertSame($headers, $captureRequest->getHeaders('default'));
480
    }
481
482
    /**
483
     * Test not writable exception is thrown if
484
     * output path is not writable.
485
     *
486
     * @access public
487
     * @return void
488
     */
489
    public function testNotWritableExceptonIsThrownIfOutputPathIsNotWritable()
490
    {
491
        $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotWritableException');
492
493
        $invalidPath = '/invalid/path';
494
495
        $captureRequest = $this->getCaptureRequest();
496
        $captureRequest->setOutputFile($invalidPath);
497
    }
498
499
    /**
500
     * Test can set output file.
501
     *
502
     * @access public
503
     * @return void
504
     */
505
    public function testCanSetOutputFile()
506
    {
507
        $outputFile = sprintf('%s/test.jpg', sys_get_temp_dir());
508
509
        $captureRequest = $this->getCaptureRequest();
510
        $captureRequest->setOutputFile($outputFile);
511
512
        $this->assertSame($outputFile, $captureRequest->getOutputFile());
513
    }
514
515
    /**
516
     * Test can set viewport width.
517
     *
518
     * @access public
519
     * @return void
520
     */
521
    public function testCanSetViewportWidth()
522
    {
523
        $width  = 100;
524
        $height = 200;
525
526
        $caputreRequest = $this->getCaptureRequest();
527
        $caputreRequest->setViewportSize($width, $height);
528
529
        $this->assertSame($width, $caputreRequest->getViewportWidth());
530
    }
531
532
    /**
533
     * Test can set viewport height.
534
     *
535
     * @access public
536
     * @return void
537
     */
538
    public function testCanSetViewportHeight()
539
    {
540
        $width  = 100;
541
        $height = 200;
542
543
        $caputreRequest = $this->getCaptureRequest();
544
        $caputreRequest->setViewportSize($width, $height);
545
546
        $this->assertSame($height, $caputreRequest->getViewportHeight());
547
    }
548
549
/** +++++++++++++++++++++++++++++++++++ **/
550
/** ++++++++++ TEST ENTITIES ++++++++++ **/
551
/** +++++++++++++++++++++++++++++++++++ **/
552
553
    /**
554
     * Get capture request instance.
555
     *
556
     * @access protected
557
     * @param  string                                $url     (default: null)
558
     * @param  string                                $method  (default: RequestInterface::METHOD_GET)
559
     * @param  int                                   $timeout (default: 5000)
560
     * @return \JonnyW\PhantomJs\Http\CaptureRequest
561
     */
562
    protected function getCaptureRequest($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
563
    {
564
        $captureRequest = new CaptureRequest($url, $method, $timeout);
565
566
        return $captureRequest;
567
    }
568
}
569