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 ( 5ba495...1e0997 )
by Jonny
03:29
created

ClientTest   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 867
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 10
Bugs 0 Features 2
Metric Value
wmc 36
c 10
b 0
f 2
lcom 1
cbo 9
dl 0
loc 867
rs 8.8

32 Methods

Rating   Name   Duplication   Size   Complexity  
B testAdditionalProceduresCanBeLoadedThroughChainLoader() 0 25 1
B testSyntaxExceptionIsThrownIfRequestProcedureContainsSyntaxError() 0 24 1
A testResponseContains200StatusCodeIfPageIsSuccessfullyLoaded() 0 14 1
A testResponseContains200StatusCodeIfRequestUrlContainsReservedCharacters() 0 18 1
A testResponseContainsValidBodyIfPageIsSuccessfullyLoaded() 0 14 1
A testResponseContainsConsoleErrorIfAJavascriptErrorExistsOnThePage() 0 17 1
A testResponseContainsConsoleTraceIfAJavascriptErrorExistsOnThePage() 0 16 1
A testResponseContainsHeaders() 0 14 1
A testRedirectUrlIsSetInResponseIfRequestIsRedirected() 0 14 1
A testPostRequestSendsRequestData() 0 19 1
A testCaptureRequestSavesFileToLocalDisk() 0 18 1
B testCaptureRequestSavesFileToDiskWithCorrectCaptureDimensions() 0 25 1
A testPdfRequestSavesPdfToLocalDisk() 0 18 1
B testPdfRequestSavesFileToDiskWithCorrectPaperSize() 0 29 1
B testPdfRequestSavesFileToDiskWithCorrectFormatSize() 0 26 1
B testPdfRequestSavesFileToDiskWithCorrectOrientation() 0 27 1
A testSetViewportSizeSetsSizeOfViewportInDefaultRequest() 0 22 1
A testSetViewportSizeSetsSizeOfViewportInCaptureRequest() 0 22 1
A testDelayLogsStartTimeInClientForDefaultRequest() 0 21 1
A testDelayLogsEndTimeInClientForDefaultRequest() 0 21 1
B testDelayDelaysPageRenderForSpecifiedTimeForDefaultRequest() 0 25 1
A testDelayLogsStartTimeInClientForCaptureRequest() 0 21 1
A testDelayLogsEndTimeInClientForCaptureRequest() 0 21 1
B testDelayDelaysPageRenderForSpecifiedTimeForCaptureRequest() 0 25 1
A testDebugLogsDebugInfoToClientLog() 0 15 1
A testCanSetPageBackgroundColor() 0 19 1
A getClient() 0 13 1
A setUp() 0 9 2
A tearDown() 0 8 2
A getFilename() 0 4 1
A writeProcedure() 0 8 1
A getLogEntryIndex() 0 13 3
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 capture 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 capture 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 capture 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 set viewport size sets
488
     * size of viewport in default
489
     * request.
490
     *
491
     * @access public
492
     * @return void
493
     */
494
    public function testSetViewportSizeSetsSizeOfViewportInDefaultRequest()
495
    {
496
        $width  = 100;
497
        $height = 200;
498
499
        $client = $this->getClient();
500
501
        $request  = $client->getMessageFactory()->createRequest();
502
        $response = $client->getMessageFactory()->createResponse();
503
504
        $request->setMethod('GET');
505
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
506
        $request->setViewportsize($width, $height);
507
508
        $client->send($request, $response);
509
510
        $logs = explode("\n", $client->getLog());
511
512
        $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
513
514
        $this->assertTrue(($startIndex !== false));
515
    }
516
517
    /**
518
     * Test set viewport size sets
519
     * size of viewport in capture
520
     * request.
521
     *
522
     * @access public
523
     * @return void
524
     */
525
    public function testSetViewportSizeSetsSizeOfViewportInCaptureRequest()
526
    {
527
        $width  = 100;
528
        $height = 200;
529
530
        $client = $this->getClient();
531
532
        $request  = $client->getMessageFactory()->createCaptureRequest();
533
        $response = $client->getMessageFactory()->createResponse();
534
535
        $request->setMethod('GET');
536
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
537
        $request->setViewportsize($width, $height);
538
539
        $client->send($request, $response);
540
541
        $logs = explode("\n", $client->getLog());
542
543
        $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
544
545
        $this->assertTrue(($startIndex !== false));
546
    }
547
548
    /**
549
     * Test delay logs start time
550
     * in client for default request.
551
     *
552
     * @access public
553
     * @return void
554
     */
555
    public function testDelayLogsStartTimeInClientForDefaultRequest()
556
    {
557
        $delay = 1;
558
559
        $client = $this->getClient();
560
561
        $request  = $client->getMessageFactory()->createRequest();
562
        $response = $client->getMessageFactory()->createResponse();
563
564
        $request->setMethod('GET');
565
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
566
        $request->setDelay($delay);
567
568
        $client->send($request, $response);
569
570
        $logs = explode("\n", $client->getLog());
571
572
        $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
573
574
        $this->assertTrue(($startIndex !== false));
575
    }
576
577
    /**
578
     * Test delay logs end time
579
     * in client for default request.
580
     *
581
     * @access public
582
     * @return void
583
     */
584
    public function testDelayLogsEndTimeInClientForDefaultRequest()
585
    {
586
        $delay = 1;
587
588
        $client = $this->getClient();
589
590
        $request  = $client->getMessageFactory()->createRequest();
591
        $response = $client->getMessageFactory()->createResponse();
592
593
        $request->setMethod('GET');
594
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
595
        $request->setDelay($delay);
596
597
        $client->send($request, $response);
598
599
        $logs = explode("\n", $client->getLog());
600
601
        $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
602
603
        $this->assertTrue(($endIndex !== false));
604
    }
605
606
    /**
607
     * Test delay delays page render for
608
     * specified time for default request.
609
     *
610
     * @access public
611
     * @return void
612
     */
613
    public function testDelayDelaysPageRenderForSpecifiedTimeForDefaultRequest()
614
    {
615
        $delay = 1;
616
617
        $client = $this->getClient();
618
619
        $request  = $client->getMessageFactory()->createRequest();
620
        $response = $client->getMessageFactory()->createResponse();
621
622
        $request->setMethod('GET');
623
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
624
        $request->setDelay($delay);
625
626
        $client->send($request, $response);
627
628
        $logs = explode("\\n", $client->getLog());
629
630
        $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
631
        $endIndex   = $this->getLogEntryIndex($logs, 'Rendering page after');
632
633
        $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
634
        $endTime   = strtotime(substr($logs[$endIndex], 0 , 19));
635
636
        $this->assertSame(($startTime+$delay), $endTime);
637
    }
638
639
    /**
640
     * Test delay logs start time
641
     * in client for capture request.
642
     *
643
     * @access public
644
     * @return void
645
     */
646
    public function testDelayLogsStartTimeInClientForCaptureRequest()
647
    {
648
        $delay = 1;
649
650
        $client = $this->getClient();
651
652
        $request  = $client->getMessageFactory()->createCaptureRequest();
653
        $response = $client->getMessageFactory()->createResponse();
654
655
        $request->setMethod('GET');
656
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
657
        $request->setDelay($delay);
658
659
        $client->send($request, $response);
660
661
        $logs = explode("\\n", $client->getLog());
662
663
        $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
664
665
        $this->assertTrue(($startIndex !== false));
666
    }
667
668
    /**
669
     * Test delay logs end time
670
     * in client for capture request.
671
     *
672
     * @access public
673
     * @return void
674
     */
675
    public function testDelayLogsEndTimeInClientForCaptureRequest()
676
    {
677
        $delay = 1;
678
679
        $client = $this->getClient();
680
681
        $request  = $client->getMessageFactory()->createCaptureRequest();
682
        $response = $client->getMessageFactory()->createResponse();
683
684
        $request->setMethod('GET');
685
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
686
        $request->setDelay($delay);
687
688
        $client->send($request, $response);
689
690
        $logs = explode("\\n", $client->getLog());
691
692
        $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
693
694
        $this->assertTrue(($endIndex !== false));
695
    }
696
697
    /**
698
     * Test delay delays page render for
699
     * specified time for capture request.
700
     *
701
     * @access public
702
     * @return void
703
     */
704
    public function testDelayDelaysPageRenderForSpecifiedTimeForCaptureRequest()
705
    {
706
        $delay = 1;
707
708
        $client = $this->getClient();
709
710
        $request  = $client->getMessageFactory()->createCaptureRequest();
711
        $response = $client->getMessageFactory()->createResponse();
712
713
        $request->setMethod('GET');
714
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
715
        $request->setDelay($delay);
716
717
        $client->send($request, $response);
718
719
        $logs = explode("\\n", $client->getLog());
720
721
        $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
722
        $endIndex   = $this->getLogEntryIndex($logs, 'Rendering page after');
723
724
        $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
725
        $endTime   = strtotime(substr($logs[$endIndex], 0 , 19));
726
727
        $this->assertSame(($startTime+$delay), $endTime);
728
    }
729
730
    /**
731
     * Test debug logs debug info to
732
     * client log.
733
     *
734
     * @access public
735
     * @return void
736
     */
737
    public function testDebugLogsDebugInfoToClientLog()
738
    {
739
        $client = $this->getClient();
740
        $client->getEngine()->debug(true);
741
742
        $request  = $client->getMessageFactory()->createRequest();
743
        $response = $client->getMessageFactory()->createResponse();
744
745
        $request->setMethod('GET');
746
        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
747
748
        $client->send($request, $response);
749
750
        $this->assertContains('[DEBUG]', $client->getLog());
751
    }
752
753
    /**
754
     * Test test can set page 
755
     * background color
756
     *
757
     * @access public
758
     * @return void
759
     */
760
    public function testCanSetPageBackgroundColor()
761
    {
762
        $this->filename = 'test.jpg';
763
        $file = ($this->directory . '/' . $this->filename);
764
765
        $client = $this->getClient();
766
767
        $request  = $client->getMessageFactory()->createCaptureRequest();
768
        $response = $client->getMessageFactory()->createResponse();
769
770
        $request->setMethod('GET');
771
        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
772
        $request->setBodyStyles(array('backgroundColor' => 'red'));
773
        $request->setOutputFile($file);
774
775
        $client->send($request, $response);
776
777
        $this->assertContains('body style="background-color: red;"', $response->getContent());
778
    }
779
780
/** +++++++++++++++++++++++++++++++++++ **/
781
/** ++++++++++ TEST ENTITIES ++++++++++ **/
782
/** +++++++++++++++++++++++++++++++++++ **/
783
784
    /**
785
     * Get client instance.
786
     *
787
     * @return \JonnyW\PhantomJs\Client
788
     */
789
    protected function getClient()
790
    {
791
        $serviceContainer = ServiceContainer::getInstance();
792
793
        $client = new Client(
794
            $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...
795
            $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...
796
            $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...
797
            $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...
798
        );
799
800
        return $client;
801
    }
802
803
/** +++++++++++++++++++++++++++++++++++ **/
804
/** ++++++++++++ UTILITIES ++++++++++++ **/
805
/** +++++++++++++++++++++++++++++++++++ **/
806
807
    /**
808
     * Set up test environment.
809
     *
810
     * @access public
811
     * @return void
812
     */
813
    public function setUp()
814
    {
815
        $this->filename  = 'test.proc';
816
        $this->directory = sys_get_temp_dir();
817
818
        if (!is_writable($this->directory)) {
819
            throw new \RuntimeException(sprintf('Test directory must be writable: %s', $this->directory));
820
        }
821
    }
822
823
    /**
824
     * Tear down test environment.
825
     *
826
     * @access public
827
     * @return void
828
     */
829
    public function tearDown()
830
    {
831
        $filename = $this->getFilename();
832
833
        if (file_exists($filename)) {
834
            unlink($filename);
835
        }
836
    }
837
838
    /**
839
     * Get test filename.
840
     *
841
     * @access public
842
     * @return string
843
     */
844
    public function getFilename()
845
    {
846
        return sprintf('%1$s/%2$s', $this->directory, $this->filename);
847
    }
848
849
    /**
850
     * Write procedure body to file.
851
     *
852
     * @access public
853
     * @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...
854
     * @return string
855
     */
856
    public function writeProcedure($procedure)
857
    {
858
        $filename = $this->getFilename();
859
860
        file_put_contents($filename, $procedure);
861
862
        return $filename;
863
    }
864
865
    /**
866
     * Get log entry index.
867
     *
868
     * @access public
869
     * @param  array     $logs
870
     * @param  string    $search
871
     * @return int|false
872
     */
873
    public function getLogEntryIndex(array $logs, $search)
874
    {
875
        foreach ($logs as $index => $log) {
876
877
            $pos = stripos($log, $search);
878
879
            if ($pos !== false) {
880
                return $index;
881
            }
882
        }
883
884
        return false;
885
    }
886
}
887