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
Pull Request — master (#145)
by
unknown
05:01
created

testRequestContainsNoBodyIfMethodIsHead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
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\PdfRequest;
12
use JonnyW\PhantomJs\Http\RequestInterface;
13
14
/**
15
 * PHP PhantomJs
16
 *
17
 * @author Jon Wenmoth <[email protected]>
18
 */
19
class PdfRequestTest extends \PHPUnit_Framework_TestCase
20
{
21
22
/** +++++++++++++++++++++++++++++++++++ **/
23
/** ++++++++++++++ TESTS ++++++++++++++ **/
24
/** +++++++++++++++++++++++++++++++++++ **/
25
26
    /**
27
     * Test PDF type is returned by default
28
     * if no type is set.
29
     *
30
     * @access public
31
     * @return void
32
     */
33
    public function testPdfTypeIsReturnedByDefaultIfNotTypeIsSet()
34
    {
35
        $pdfRequest = $this->getPdfRequest();
36
37
        $this->assertEquals(RequestInterface::REQUEST_TYPE_PDF, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
51
        $pdfRequest->setType($requestType);
52
53
        $this->assertEquals($requestType, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest($url);
66
67
        $this->assertEquals($url, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest(null, $method);
80
81
        $this->assertEquals($method, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest('http://test.com', 'GET', $timeout);
94
95
        $this->assertEquals($timeout, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
110
        $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
130
        $pdfRequest->setMethod('POST');
131
        $pdfRequest->setUrl($url);
132
        $pdfRequest->setRequestData($data);
133
134
        $this->assertEquals($url, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
154
        $pdfRequest->setMethod('GET');
155
        $pdfRequest->setUrl($url);
156
        $pdfRequest->setRequestData($data);
157
158
        $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
159
160
        $this->assertEquals($expectedUrl, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
180
        $pdfRequest->setMethod('HEAD');
181
        $pdfRequest->setUrl($url);
182
        $pdfRequest->setRequestData($data);
183
184
        $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
185
186
        $this->assertEquals($expectedUrl, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
206
        $pdfRequest->setMethod('GET');
207
        $pdfRequest->setUrl($url);
208
        $pdfRequest->setRequestData($data);
209
210
        $expectedUrl = $url . '&test_param1=Testing1&test_param2=Testing2';
211
212
        $this->assertEquals($expectedUrl, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
230
        $pdfRequest->setMethod('GET');
231
        $pdfRequest->setRequestData($data);
232
233
        $this->assertEquals('', $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
251
        $pdfRequest->setMethod('HEAD');
252
        $pdfRequest->setRequestData($data);
253
254
        $this->assertEquals('', $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
272
        $pdfRequest->setMethod('POST');
273
        $pdfRequest->setRequestData($data);
274
275
        $body = 'test_param1=Testing1&test_param2=Testing2';
276
277
        $this->assertEquals($body, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
297
        $pdfRequest->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, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
325
        $pdfRequest->setRequestData($data);
326
327
        $this->assertEquals($data, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
348
        $pdfRequest->setHeaders($existingHeaders);
349
        $pdfRequest->addHeaders($newHeaders);
350
351
        $expectedHeaders = array_merge($existingHeaders, $newHeaders);
352
353
        $this->assertEquals($expectedHeaders, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
371
        $pdfRequest->setHeaders($headers);
372
373
        $expectedHeaders = json_encode($headers);
374
375
        $this->assertEquals($expectedHeaders, $pdfRequest->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
        $pdfRequest = $this->getPdfRequest();
392
        $pdfRequest->setHeaders($headers);
393
394
        $this->assertEquals($headers, $pdfRequest->getHeaders('default'));
395
    }
396
397
    /**
398
     * Test not writable exception is thrown if
399
     * output path is not writable.
400
     *
401
     * @access public
402
     * @return void
403
     */
404
    public function tesNotWritableExceptonIsThrownIfOutputPathIsNotWritable()
405
    {
406
        $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotWritableException');
407
408
        $invalidPath = '/invalid/path';
409
410
        $pdfRequest = $this->getPdfRequest();
411
        $pdfRequest->setOutputFile($invalidPath);
412
    }
413
414
    /**
415
     * Test can set output file.
416
     *
417
     * @access public
418
     * @return void
419
     */
420
    public function testCanSetOutputFile()
421
    {
422
        $outputFile = sprintf('%s/test.jpg', sys_get_temp_dir());
423
424
        $pdfRequest = $this->getPdfRequest();
425
        $pdfRequest->setOutputFile($outputFile);
426
427
        $this->assertEquals($outputFile, $pdfRequest->getOutputFile());
428
    }
429
430
    /**
431
     * Test can set viewport width.
432
     *
433
     * @access public
434
     * @return void
435
     */
436
    public function testCanSetViewportWidth()
437
    {
438
        $width  = 100;
439
        $height = 200;
440
441
        $pdfRequest = $this->getPdfRequest();
442
        $pdfRequest->setViewportSize($width, $height);
443
444
        $this->assertEquals($width, $pdfRequest->getViewportWidth());
445
    }
446
447
    /**
448
     * Test can set viewport height.
449
     *
450
     * @access public
451
     * @return void
452
     */
453
    public function testCanSetViewportHeight()
454
    {
455
        $width  = 100;
456
        $height = 200;
457
458
        $pdfRequest = $this->getPdfRequest();
459
        $pdfRequest->setViewportSize($width, $height);
460
461
        $this->assertEquals($height, $pdfRequest->getViewportHeight());
462
    }
463
464
    /**
465
     * Test can set paper width.
466
     *
467
     * @access public
468
     * @return void
469
     */
470
    public function testCanSetPaperWidth()
471
    {
472
        $width  = '10cm';
473
        $height = '20cm';
474
475
        $pdfRequest = $this->getPdfRequest();
476
        $pdfRequest->setPaperSize($width, $height);
477
478
        $this->assertEquals($width, $pdfRequest->getPaperWidth());
479
    }
480
481
    /**
482
     * Test can set paper height.
483
     *
484
     * @access public
485
     * @return void
486
     */
487
    public function testCanSetPaperHeight()
488
    {
489
        $width  = '10cm';
490
        $height = '20cm';
491
492
        $pdfRequest = $this->getPdfRequest();
493
        $pdfRequest->setPaperSize($width, $height);
494
495
        $this->assertEquals($height, $pdfRequest->getPaperHeight());
496
    }
497
498
    /**
499
     * Test can set zoom.
500
     *
501
     * @access public
502
     * @return void
503
     */
504
    public function testCanSetZoom()
505
    {
506
        $zoom = 0.5;
507
508
        $pdfRequest = $this->getPdfRequest();
509
        $pdfRequest->setZoom($zoom);
510
511
        $this->assertSame($zoom, $pdfRequest->getZoom());
512
    }
513
514
/** +++++++++++++++++++++++++++++++++++ **/
515
/** ++++++++++ TEST ENTITIES ++++++++++ **/
516
/** +++++++++++++++++++++++++++++++++++ **/
517
518
    /**
519
     * Get PDF request instance.
520
     *
521
     * @access protected
522
     * @param  string                            $url     (default: null)
523
     * @param  string                            $method  (default: RequestInterface::METHOD_GET)
524
     * @param  int                               $timeout (default: 5000)
525
     * @return \JonnyW\PhantomJs\Http\PdfRequest
526
     */
527
    protected function getPdfRequest($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
528
    {
529
        $pdfRequest = new PdfRequest($url, $method, $timeout);
530
531
        return $pdfRequest;
532
    }
533
}
534