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 ( ee6cec...dafb41 )
by Jonny
04:30
created

testCanSetRepeatingFooterForPDFRequest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 8.8571
cc 1
eloc 19
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\Integration;
10
11
use JonnyW\PhantomJs\Test\TestCase;
12
use JonnyW\PhantomJs\Client;
13
use JonnyW\PhantomJs\DependencyInjection\ServiceContainer;
14
15
/**
16
 * PHP PhantomJs
17
 *
18
 * @author Jon Wenmoth <[email protected]>
19
 */
20
class ClientTest extends TestCase
21
{
22
    /**
23
     * Test filename
24
     *
25
     * @var string
26
     * @access protected
27
     */
28
    protected $filename;
29
30
    /**
31
     * Test directory
32
     *
33
     * @var string
34
     * @access protected
35
     */
36
    protected $directory;
37
38
/** +++++++++++++++++++++++++++++++++++ **/
39
/** ++++++++++++++ TESTS ++++++++++++++ **/
40
/** +++++++++++++++++++++++++++++++++++ **/
41
42
    /**
43
     * Test additional procedures can be loaded
44
     * through chain loader.
45
     *
46
     * @access public
47
     * @return void
48
     */
49
    public function testAdditionalProceduresCanBeLoadedThroughChainLoader()
50
    {
51
        $content = 'TEST_PROCEDURE';
52
53
        $procedure = <<<EOF
54
    console.log(JSON.stringify({"content": "$content"}, undefined, 4));
55
    phantom.exit(1);
56
EOF;
57
58
        $this->writeProcedure($procedure);
59
60
        $procedureLoaderFactory = $this->getContainer()->get('procedure_loader_factory');
61
        $procedureLoader        = $procedureLoaderFactory->createProcedureLoader($this->directory);
62
63
        $client = $this->getClient();
64
        $client->setProcedure('test');
65
        $client->getProcedureLoader()->addLoader($procedureLoader);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface JonnyW\PhantomJs\Procedu...rocedureLoaderInterface as the method addLoader() does only exist in the following implementations of said interface: JonnyW\PhantomJs\Procedure\ChainProcedureLoader.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
66
67
        $request  = $client->getMessageFactory()->createRequest();
68
        $response = $client->getMessageFactory()->createResponse();
69
70
        $client->send($request, $response);
71
72
        $this->assertSame($content, $response->getContent());
73
    }
74
75
    /**
76
     * Test syntax exception is thrown if request
77
     * procedure contains syntax error.
78
     *
79
     * @access public
80
     * @return void
81
     */
82
    public function testSyntaxExceptionIsThrownIfRequestProcedureContainsSyntaxError()
83
    {
84
        $this->setExpectedException('\JonnyW\PhantomJs\Exception\SyntaxException');
85
86
        $content = 'TEST_PROCEDURE';
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
88
        $procedure = <<<EOF
89
    console.log(;
90
EOF;
91
92
        $this->writeProcedure($procedure);
93
94
        $procedureLoaderFactory = $this->getContainer()->get('procedure_loader_factory');
95
        $procedureLoader        = $procedureLoaderFactory->createProcedureLoader($this->directory);
96
97
        $client = $this->getClient();
98
        $client->setProcedure('test');
99
        $client->getProcedureLoader()->addLoader($procedureLoader);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface JonnyW\PhantomJs\Procedu...rocedureLoaderInterface as the method addLoader() does only exist in the following implementations of said interface: JonnyW\PhantomJs\Procedure\ChainProcedureLoader.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
100
101
        $request  = $client->getMessageFactory()->createRequest();
102
        $response = $client->getMessageFactory()->createResponse();
103
104
        $client->send($request, $response);
105
    }
106
107
    /**
108
     * Test response contains 200 status code if page
109
     * is successfully loaded.
110
     *
111
     * @access public
112
     * @return void
113
     */
114
    public function testResponseContains200StatusCodeIfPageIsSuccessfullyLoaded()
115
    {
116
        $client = $this->getClient();
117
118
        $request  = $client->getMessageFactory()->createRequest();
119
        $response = $client->getMessageFactory()->createResponse();
120
121
        $request->setMethod('GET');
122
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
123
124
        $client->send($request, $response);
125
126
        $this->assertEquals(200, $response->getStatus());
127
    }
128
129
    /**
130
     * Test response contains 200 status code if
131
     * request URL contains reserved characters.
132
     *
133
     * @access public
134
     * @return void
135
     */
136
    public function testResponseContains200StatusCodeIfRequestUrlContainsReservedCharacters()
137
    {
138
        $client = $this->getClient();
139
140
        $request  = $client->getMessageFactory()->createRequest();
141
        $response = $client->getMessageFactory()->createResponse();
142
143
        $request->setMethod('GET');
144
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
145
        $request->setRequestData(array(
146
            'test1' => 'http://test.com',
147
            'test2' => 'A string with an \' ) / # some other invalid [ characters.'
148
        ));
149
150
        $client->send($request, $response);
151
152
        $this->assertEquals(200, $response->getStatus());
153
    }
154
155
    /**
156
     * Test response contains valid body if page is
157
     * successfully loaded.
158
     *
159
     * @access public
160
     * @return void
161
     */
162
    public function testResponseContainsValidBodyIfPageIsSuccessfullyLoaded()
163
    {
164
        $client = $this->getClient();
165
166
        $request  = $client->getMessageFactory()->createRequest();
167
        $response = $client->getMessageFactory()->createResponse();
168
169
        $request->setMethod('GET');
170
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
171
172
        $client->send($request, $response);
173
174
        $this->assertContains('PHANTOMJS_DEFAULT_TEST', $response->getContent());
175
    }
176
177
    /**
178
     * Test response contains console error if a
179
     * javascript error exists on the page.
180
     *
181
     * @access public
182
     * @return void
183
     */
184
    public function testResponseContainsConsoleErrorIfAJavascriptErrorExistsOnThePage()
185
    {
186
        $client = $this->getClient();
187
188
        $request  = $client->getMessageFactory()->createRequest();
189
        $response = $client->getMessageFactory()->createResponse();
190
191
        $request->setMethod('GET');
192
        $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
193
194
        $client->send($request, $response);
195
196
        $console = $response->getConsole();
197
198
        $this->assertCount(1, $console);
199
        $this->assertContains('ReferenceError: Can\'t find variable: invalid', $console[0]['message']);
200
    }
201
202
    /**
203
     * Test response contains console trace if a
204
     * javascript error exists on the page.
205
     *
206
     * @access public
207
     * @return void
208
     */
209
    public function testResponseContainsConsoleTraceIfAJavascriptErrorExistsOnThePage()
210
    {
211
        $client = $this->getClient();
212
213
        $request  = $client->getMessageFactory()->createRequest();
214
        $response = $client->getMessageFactory()->createResponse();
215
216
        $request->setMethod('GET');
217
        $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
218
219
        $client->send($request, $response);
220
221
        $console = $response->getConsole();
222
223
        $this->assertCount(1, $console[0]['trace']);
224
    }
225
226
    /**
227
     * Test response contains headers.
228
     *
229
     * @access public
230
     * @return void
231
     */
232
    public function testResponseContainsHeaders()
233
    {
234
        $client = $this->getClient();
235
236
        $request  = $client->getMessageFactory()->createRequest();
237
        $response = $client->getMessageFactory()->createResponse();
238
239
        $request->setMethod('GET');
240
        $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
241
242
        $client->send($request, $response);
243
244
        $this->assertNotEmpty($response->getHeaders());
245
    }
246
247
    /**
248
     * Test redirect URL is set in response
249
     * if request is redirected.
250
     *
251
     * @access public
252
     * @return void
253
     */
254
    public function testRedirectUrlIsSetInResponseIfRequestIsRedirected()
255
    {
256
        $client = $this->getClient();
257
258
        $request  = $client->getMessageFactory()->createRequest();
259
        $response = $client->getMessageFactory()->createResponse();
260
261
        $request->setMethod('GET');
262
        $request->setUrl('https://jigsaw.w3.org/HTTP/300/302.html');
263
264
        $client->send($request, $response);
265
266
        $this->assertNotEmpty($response->getRedirectUrl());
267
    }
268
269
    /**
270
     * Test POST request sends request data.
271
     *
272
     * @access public
273
     * @return void
274
     */
275
    public function testPostRequestSendsRequestData()
276
    {
277
        $client = $this->getClient();
278
279
        $request  = $client->getMessageFactory()->createRequest();
280
        $response = $client->getMessageFactory()->createResponse();
281
282
        $request->setMethod('POST');
283
        $request->setUrl('http://jonnyw.kiwi/tests/test-post.php');
284
        $request->setRequestData(array(
285
            'test1' => 'http://test.com',
286
            'test2' => 'A string with an \' ) / # some other invalid [ characters.'
287
        ));
288
289
        $client->send($request, $response);
290
291
        $this->assertContains(sprintf('<li>test1=%s</li>', 'http://test.com'), $response->getContent());
292
        $this->assertContains(sprintf('<li>test2=%s</li>', 'A string with an \' ) / # some other invalid [ characters.'), $response->getContent());
293
    }
294
295
    /**
296
     * Test capture request saves file to
297
     * to local disk.
298
     *
299
     * @access public
300
     * @return void
301
     */
302
    public function testCaptureRequestSavesFileToLocalDisk()
303
    {
304
        $this->filename = 'test.jpg';
305
        $file = ($this->directory . '/' . $this->filename);
306
307
        $client = $this->getClient();
308
309
        $request  = $client->getMessageFactory()->createCaptureRequest();
310
        $response = $client->getMessageFactory()->createResponse();
311
312
        $request->setMethod('GET');
313
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
314
        $request->setOutputFile($file);
315
316
        $client->send($request, $response);
317
318
        $this->assertTrue(file_exists($file));
319
    }
320
321
    /**
322
     * Test capture request saves file to
323
     * disk with correct capture dimensions.
324
     *
325
     * @access public
326
     * @return void
327
     */
328
    public function testCaptureRequestSavesFileToDiskWithCorrectCaptureDimensions()
329
    {
330
        $this->filename = 'test.jpg';
331
        $file = ($this->directory . '/' . $this->filename);
332
333
        $width  = 200;
334
        $height = 400;
335
336
        $client = $this->getClient();
337
338
        $request  = $client->getMessageFactory()->createCaptureRequest();
339
        $response = $client->getMessageFactory()->createResponse();
340
341
        $request->setMethod('GET');
342
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
343
        $request->setOutputFile($file);
344
        $request->setCaptureDimensions($width, $height);
345
346
        $client->send($request, $response);
347
348
        $imageInfo = getimagesize($file);
349
350
        $this->assertEquals($width, $imageInfo[0]);
351
        $this->assertEquals($height, $imageInfo[1]);
352
    }
353
354
    /**
355
     * Test PDF request saves pdf to
356
     * to local disk.
357
     *
358
     * @access public
359
     * @return void
360
     */
361
    public function testPdfRequestSavesPdfToLocalDisk()
362
    {
363
        $this->filename = 'test.pdf';
364
        $file = ($this->directory . '/' . $this->filename);
365
366
        $client = $this->getClient();
367
368
        $request  = $client->getMessageFactory()->createPdfRequest();
369
        $response = $client->getMessageFactory()->createResponse();
370
371
        $request->setMethod('GET');
372
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
373
        $request->setOutputFile($file);
374
375
        $client->send($request, $response);
376
377
        $this->assertTrue(file_exists($file));
378
    }
379
380
    /**
381
     * Test PDF request saves file to
382
     * disk with correct paper size.
383
     *
384
     * @access public
385
     * @return void
386
     */
387
    public function testPdfRequestSavesFileToDiskWithCorrectPaperSize()
388
    {
389
        $this->filename = 'test.pdf';
390
        $file = ($this->directory . '/' . $this->filename);
391
392
        $width  = 20;
393
        $height = 30;
394
395
        $client = $this->getClient();
396
397
        $request  = $client->getMessageFactory()->createPdfRequest();
398
        $response = $client->getMessageFactory()->createResponse();
399
400
        $request->setMethod('GET');
401
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
402
        $request->setOutputFile($file);
403
        $request->setPaperSize(sprintf('%scm', $width), sprintf('%scm', $height));
404
        $request->setMargin('0cm');
405
406
        $client->send($request, $response);
407
408
        $pdf = \ZendPdf\PdfDocument::load($file);
409
410
        $pdfWidth  = round(($pdf->pages[0]->getWidth() * 0.0352777778));
411
        $pdfHeight = round(($pdf->pages[0]->getHeight()  * 0.0352777778));
412
413
        $this->assertEquals($width, $pdfWidth);
414
        $this->assertEquals($height, $pdfHeight);
415
    }
416
417
    /**
418
     * Test PDF request saves file to
419
     * disk with correct format size.
420
     *
421
     * @access public
422
     * @return void
423
     */
424
    public function testPdfRequestSavesFileToDiskWithCorrectFormatSize()
425
    {
426
        $this->filename = 'test.pdf';
427
        $file = ($this->directory . '/' . $this->filename);
428
429
        $client = $this->getClient();
430
431
        $request  = $client->getMessageFactory()->createPdfRequest();
432
        $response = $client->getMessageFactory()->createResponse();
433
434
        $request->setMethod('GET');
435
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
436
        $request->setOutputFile($file);
437
        $request->setFormat('A4');
438
        $request->setMargin('0cm');
439
440
        $client->send($request, $response);
441
442
        $pdf = \ZendPdf\PdfDocument::load($file);
443
444
        $pdfWidth  = round(($pdf->pages[0]->getWidth() * 0.0352777778));
445
        $pdfHeight = round(($pdf->pages[0]->getHeight()  * 0.0352777778));
446
447
        $this->assertEquals(21, $pdfWidth);
448
        $this->assertEquals(30, $pdfHeight);
449
    }
450
451
    /**
452
     * Test PDF request saves file to
453
     * disk with correct orientation.
454
     *
455
     * @access public
456
     * @return void
457
     */
458
    public function testPdfRequestSavesFileToDiskWithCorrectOrientation()
459
    {
460
        $this->filename = 'test.pdf';
461
        $file = ($this->directory . '/' . $this->filename);
462
463
        $client = $this->getClient();
464
465
        $request  = $client->getMessageFactory()->createPdfRequest();
466
        $response = $client->getMessageFactory()->createResponse();
467
468
        $request->setMethod('GET');
469
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
470
        $request->setOutputFile($file);
471
        $request->setFormat('A4');
472
        $request->setOrientation('landscape');
473
        $request->setMargin('0cm');
474
475
        $client->send($request, $response);
476
477
        $pdf = \ZendPdf\PdfDocument::load($file);
478
479
        $pdfWidth  = round(($pdf->pages[0]->getWidth() * 0.0352777778));
480
        $pdfHeight = round(($pdf->pages[0]->getHeight()  * 0.0352777778));
481
482
        $this->assertEquals(30, $pdfWidth);
483
        $this->assertEquals(21, $pdfHeight);
484
    }
485
    
486
    /**
487
     * Test can set repeating header
488
     * for PDF request
489
     *
490
     * @access public
491
     * @return void
492
     */
493
    public function testCanSetRepeatingHeaderForPDFRequest()
494
    {
495
        $this->filename = 'test.pdf';
496
        $file = ($this->directory . '/' . $this->filename);
497
498
        $client = $this->getClient();
499
500
        $request  = $client->getMessageFactory()->createPdfRequest();
501
        $response = $client->getMessageFactory()->createResponse();
502
503
        $request->setMethod('GET');
504
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
505
        $request->setOutputFile($file);
506
        $request->setFormat('A4');
507
        $request->setOrientation('landscape');
508
        $request->setMargin('0cm');
509
        $request->setRepeatingHeader('<h1>Header <span style="float:right">%pageNum% / %pageTotal%</span></h1>', '2cm');
510
        $request->setRepeatingFooter('<footer>Footer <span style="float:right">%pageNum% / %pageTotal%</span></footer>', '2cm');
511
512
        $client->send($request, $response);
513
514
        $pdf = (new \Smalot\PdfParser\Parser())
515
            ->parseFile($file);
516
517
        $text = str_replace(' ', '', $pdf->getText());
518
519
        $this->assertContains('Header', $text);
520
    }
521
    
522
    /**
523
     * Test can set repeating footer
524
     * for PDF request
525
     *
526
     * @access public
527
     * @return void
528
     */
529
    public function testCanSetRepeatingFooterForPDFRequest()
530
    {
531
        $this->filename = 'test.pdf';
532
        $file = ($this->directory . '/' . $this->filename);
533
534
        $client = $this->getClient();
535
536
        $request  = $client->getMessageFactory()->createPdfRequest();
537
        $response = $client->getMessageFactory()->createResponse();
538
539
        $request->setMethod('GET');
540
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
541
        $request->setOutputFile($file);
542
        $request->setFormat('A4');
543
        $request->setOrientation('landscape');
544
        $request->setMargin('0cm');
545
        $request->setRepeatingHeader('<h1>Header <span style="float:right">%pageNum% / %pageTotal%</span></h1>', '2cm');
546
        $request->setRepeatingFooter('<footer>Footer <span style="float:right">%pageNum% / %pageTotal%</span></footer>', '2cm');
547
548
        $client->send($request, $response);
549
550
        $pdf = (new \Smalot\PdfParser\Parser())
551
            ->parseFile($file);
552
553
        $text = str_replace(' ', '', $pdf->getText());
554
555
        $this->assertContains('Footer', $text);
556
    }
557
558
    /**
559
     * Test set viewport size sets
560
     * size of viewport in default
561
     * request.
562
     *
563
     * @access public
564
     * @return void
565
     */
566
    public function testSetViewportSizeSetsSizeOfViewportInDefaultRequest()
567
    {
568
        $width  = 100;
569
        $height = 200;
570
571
        $client = $this->getClient();
572
573
        $request  = $client->getMessageFactory()->createRequest();
574
        $response = $client->getMessageFactory()->createResponse();
575
576
        $request->setMethod('GET');
577
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
578
        $request->setViewportsize($width, $height);
579
580
        $client->send($request, $response);
581
582
        $logs = explode("\n", $client->getLog());
583
584
        $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
585
586
        $this->assertTrue(($startIndex !== false));
587
    }
588
589
    /**
590
     * Test set viewport size sets
591
     * size of viewport in capture
592
     * request.
593
     *
594
     * @access public
595
     * @return void
596
     */
597
    public function testSetViewportSizeSetsSizeOfViewportInCaptureRequest()
598
    {
599
        $width  = 100;
600
        $height = 200;
601
602
        $client = $this->getClient();
603
604
        $request  = $client->getMessageFactory()->createCaptureRequest();
605
        $response = $client->getMessageFactory()->createResponse();
606
607
        $request->setMethod('GET');
608
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
609
        $request->setViewportsize($width, $height);
610
611
        $client->send($request, $response);
612
613
        $logs = explode("\n", $client->getLog());
614
615
        $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
616
617
        $this->assertTrue(($startIndex !== false));
618
    }
619
620
    /**
621
     * Test delay logs start time
622
     * in client for default request.
623
     *
624
     * @access public
625
     * @return void
626
     */
627
    public function testDelayLogsStartTimeInClientForDefaultRequest()
628
    {
629
        $delay = 1;
630
631
        $client = $this->getClient();
632
633
        $request  = $client->getMessageFactory()->createRequest();
634
        $response = $client->getMessageFactory()->createResponse();
635
636
        $request->setMethod('GET');
637
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
638
        $request->setDelay($delay);
639
640
        $client->send($request, $response);
641
642
        $logs = explode("\n", $client->getLog());
643
644
        $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
645
646
        $this->assertTrue(($startIndex !== false));
647
    }
648
649
    /**
650
     * Test delay logs end time
651
     * in client for default request.
652
     *
653
     * @access public
654
     * @return void
655
     */
656
    public function testDelayLogsEndTimeInClientForDefaultRequest()
657
    {
658
        $delay = 1;
659
660
        $client = $this->getClient();
661
662
        $request  = $client->getMessageFactory()->createRequest();
663
        $response = $client->getMessageFactory()->createResponse();
664
665
        $request->setMethod('GET');
666
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
667
        $request->setDelay($delay);
668
669
        $client->send($request, $response);
670
671
        $logs = explode("\n", $client->getLog());
672
673
        $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
674
675
        $this->assertTrue(($endIndex !== false));
676
    }
677
678
    /**
679
     * Test delay delays page render for
680
     * specified time for default request.
681
     *
682
     * @access public
683
     * @return void
684
     */
685
    public function testDelayDelaysPageRenderForSpecifiedTimeForDefaultRequest()
686
    {
687
        $delay = 1;
688
689
        $client = $this->getClient();
690
691
        $request  = $client->getMessageFactory()->createRequest();
692
        $response = $client->getMessageFactory()->createResponse();
693
694
        $request->setMethod('GET');
695
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
696
        $request->setDelay($delay);
697
698
        $client->send($request, $response);
699
700
        $logs = explode("\\n", $client->getLog());
701
702
        $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
703
        $endIndex   = $this->getLogEntryIndex($logs, 'Rendering page after');
704
705
        $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
706
        $endTime   = strtotime(substr($logs[$endIndex], 0 , 19));
707
708
        $this->assertSame(($startTime+$delay), $endTime);
709
    }
710
711
    /**
712
     * Test delay logs start time
713
     * in client for capture request.
714
     *
715
     * @access public
716
     * @return void
717
     */
718
    public function testDelayLogsStartTimeInClientForCaptureRequest()
719
    {
720
        $delay = 1;
721
722
        $client = $this->getClient();
723
724
        $request  = $client->getMessageFactory()->createCaptureRequest();
725
        $response = $client->getMessageFactory()->createResponse();
726
727
        $request->setMethod('GET');
728
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
729
        $request->setDelay($delay);
730
731
        $client->send($request, $response);
732
733
        $logs = explode("\\n", $client->getLog());
734
735
        $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
736
737
        $this->assertTrue(($startIndex !== false));
738
    }
739
740
    /**
741
     * Test delay logs end time
742
     * in client for capture request.
743
     *
744
     * @access public
745
     * @return void
746
     */
747
    public function testDelayLogsEndTimeInClientForCaptureRequest()
748
    {
749
        $delay = 1;
750
751
        $client = $this->getClient();
752
753
        $request  = $client->getMessageFactory()->createCaptureRequest();
754
        $response = $client->getMessageFactory()->createResponse();
755
756
        $request->setMethod('GET');
757
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
758
        $request->setDelay($delay);
759
760
        $client->send($request, $response);
761
762
        $logs = explode("\\n", $client->getLog());
763
764
        $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
765
766
        $this->assertTrue(($endIndex !== false));
767
    }
768
769
    /**
770
     * Test delay delays page render for
771
     * specified time for capture request.
772
     *
773
     * @access public
774
     * @return void
775
     */
776
    public function testDelayDelaysPageRenderForSpecifiedTimeForCaptureRequest()
777
    {
778
        $delay = 1;
779
780
        $client = $this->getClient();
781
782
        $request  = $client->getMessageFactory()->createCaptureRequest();
783
        $response = $client->getMessageFactory()->createResponse();
784
785
        $request->setMethod('GET');
786
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
787
        $request->setDelay($delay);
788
789
        $client->send($request, $response);
790
791
        $logs = explode("\\n", $client->getLog());
792
793
        $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
794
        $endIndex   = $this->getLogEntryIndex($logs, 'Rendering page after');
795
796
        $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
797
        $endTime   = strtotime(substr($logs[$endIndex], 0 , 19));
798
799
        $this->assertSame(($startTime+$delay), $endTime);
800
    }
801
802
    /**
803
     * Test debug logs debug info to
804
     * client log.
805
     *
806
     * @access public
807
     * @return void
808
     */
809
    public function testDebugLogsDebugInfoToClientLog()
810
    {
811
        $client = $this->getClient();
812
        $client->getEngine()->debug(true);
813
814
        $request  = $client->getMessageFactory()->createRequest();
815
        $response = $client->getMessageFactory()->createResponse();
816
817
        $request->setMethod('GET');
818
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
819
820
        $client->send($request, $response);
821
822
        $this->assertContains('[DEBUG]', $client->getLog());
823
    }
824
825
    /**
826
     * Test test can set page
827
     * background color
828
     *
829
     * @access public
830
     * @return void
831
     */
832
    public function testCanSetPageBackgroundColor()
833
    {
834
        $this->filename = 'test.jpg';
835
        $file = ($this->directory . '/' . $this->filename);
836
837
        $client = $this->getClient();
838
839
        $request  = $client->getMessageFactory()->createCaptureRequest();
840
        $response = $client->getMessageFactory()->createResponse();
841
842
        $request->setMethod('GET');
843
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
844
        $request->setBodyStyles(array('backgroundColor' => 'red'));
845
        $request->setOutputFile($file);
846
847
        $client->send($request, $response);
848
849
        $this->assertContains('body style="background-color: red;"', $response->getContent());
850
    }
851
852
/** +++++++++++++++++++++++++++++++++++ **/
853
/** ++++++++++ TEST ENTITIES ++++++++++ **/
854
/** +++++++++++++++++++++++++++++++++++ **/
855
856
    /**
857
     * Get client instance.
858
     *
859
     * @return \JonnyW\PhantomJs\Client
860
     */
861
    protected function getClient()
862
    {
863
        $serviceContainer = ServiceContainer::getInstance();
864
865
        $client = new Client(
866
            $serviceContainer->get('engine'),
0 ignored issues
show
Documentation introduced by
$serviceContainer->get('engine') is of type object|null, but the function expects a object<JonnyW\PhantomJs\Engine>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
867
            $serviceContainer->get('procedure_loader'),
0 ignored issues
show
Documentation introduced by
$serviceContainer->get('procedure_loader') is of type object|null, but the function expects a object<JonnyW\PhantomJs\...ocedureLoaderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
868
            $serviceContainer->get('procedure_compiler'),
0 ignored issues
show
Documentation introduced by
$serviceContainer->get('procedure_compiler') is of type object|null, but the function expects a object<JonnyW\PhantomJs\...edureCompilerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
869
            $serviceContainer->get('message_factory')
0 ignored issues
show
Documentation introduced by
$serviceContainer->get('message_factory') is of type object|null, but the function expects a object<JonnyW\PhantomJs\...essageFactoryInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
870
        );
871
872
        return $client;
873
    }
874
875
/** +++++++++++++++++++++++++++++++++++ **/
876
/** ++++++++++++ UTILITIES ++++++++++++ **/
877
/** +++++++++++++++++++++++++++++++++++ **/
878
879
    /**
880
     * Set up test environment.
881
     *
882
     * @access public
883
     * @return void
884
     */
885
    public function setUp()
886
    {
887
        $this->filename  = 'test.proc';
888
        $this->directory = sys_get_temp_dir();
889
890
        if (!is_writable($this->directory)) {
891
            throw new \RuntimeException(sprintf('Test directory must be writable: %s', $this->directory));
892
        }
893
    }
894
895
    /**
896
     * Tear down test environment.
897
     *
898
     * @access public
899
     * @return void
900
     */
901
    public function tearDown()
902
    {
903
        $filename = $this->getFilename();
904
905
        if (file_exists($filename)) {
906
            unlink($filename);
907
        }
908
    }
909
910
    /**
911
     * Get test filename.
912
     *
913
     * @access public
914
     * @return string
915
     */
916
    public function getFilename()
917
    {
918
        return sprintf('%1$s/%2$s', $this->directory, $this->filename);
919
    }
920
921
    /**
922
     * Write procedure body to file.
923
     *
924
     * @access public
925
     * @param  string $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
926
     * @return string
927
     */
928
    public function writeProcedure($procedure)
929
    {
930
        $filename = $this->getFilename();
931
932
        file_put_contents($filename, $procedure);
933
934
        return $filename;
935
    }
936
937
    /**
938
     * Get log entry index.
939
     *
940
     * @access public
941
     * @param  array     $logs
942
     * @param  string    $search
943
     * @return int|false
944
     */
945
    public function getLogEntryIndex(array $logs, $search)
946
    {
947
        foreach ($logs as $index => $log) {
948
949
            $pos = stripos($log, $search);
950
951
            if ($pos !== false) {
952
                return $index;
953
            }
954
        }
955
956
        return false;
957
    }
958
}
959