Browse Source

Adding ability to set viewport size for request - https://github.com/jonnnnyw/php-phantomjs/pull/14

Jonny Wenmoth 11 years ago
parent
commit
5fce2f79be

+ 0 - 4
doc/debugging.rst

@@ -1,4 +0,0 @@
-Debugging
-=============
-
-This documentation will be up in a couple of days.

+ 54 - 0
src/JonnyW/PhantomJs/Message/AbstractRequest.php

@@ -67,6 +67,22 @@ abstract class AbstractRequest implements RequestInterface
      */
     protected $delay;
 
+    /**
+     * Viewport width.
+     *
+     * @var int
+     * @access protected
+     */
+    protected $viewportWidth;
+
+    /**
+     * Viewport height.
+     *
+     * @var int
+     * @access protected
+     */
+    protected $viewportHeight;
+
     /**
      * Internal constructor
      *
@@ -81,6 +97,8 @@ abstract class AbstractRequest implements RequestInterface
         $this->headers        = array();
         $this->data           = array();
         $this->delay          = 0;
+        $this->viewportWidth  = 0;
+        $this->viewportHeight = 0;
 
         $this->setMethod($method);
         $this->setTimeout($timeout);
@@ -173,6 +191,42 @@ abstract class AbstractRequest implements RequestInterface
         return (int) $this->delay;
     }
 
+    /**
+     * Set viewport size.
+     *
+     * @access public
+     * @param  int  $width
+     * @param  int  $height
+     * @return void
+     */
+    public function setViewportSize($width, $height)
+    {
+        $this->viewportWidth  = (int) $width;
+        $this->viewportHeight = (int) $height;
+    }
+
+    /**
+     * Get viewport width.
+     *
+     * @access public
+     * @return int
+     */
+    public function getViewportWidth()
+    {
+        return (int) $this->viewportWidth;
+    }
+
+    /**
+     * Get viewport height.
+     *
+     * @access public
+     * @return int
+     */
+    public function getViewportHeight()
+    {
+        return (int) $this->viewportHeight;
+    }
+
     /**
      * Set request URL
      *

+ 16 - 1
src/JonnyW/PhantomJs/Resources/procedures/capture.proc

@@ -11,13 +11,15 @@ var page       = require('webpage').create(),
     left       = {{ request.getRectLeft() }},
     width      = {{ request.getRectWidth() }},
     height     = {{ request.getRectHeight() }},
+    vpWidth    = {{ request.getViewportWidth() }},
+    vpHeight   = {{ request.getViewportHeight() }},
     debug      = [],
     logs       = [],
     procedure  = {};
 
 
 /**
- * Define viewport width & height for capture.
+ * Define width & height of screenshot.
  */
 if(width && height) {
     
@@ -31,6 +33,19 @@ if(width && height) {
     };
 }
 
+/**
+ * Set viewport size.
+ */
+if(vpWidth && vpHeight) {
+    
+    debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Set viewport size ~ width: ' + vpWidth + ' height: ' + vpHeight);
+    
+    page.viewportSize = {
+        width: vpWidth,
+        height: vpHeight
+    };
+}
+
 /**
  * Define custom headers.
  */

+ 15 - 0
src/JonnyW/PhantomJs/Resources/procedures/default.proc

@@ -6,11 +6,26 @@ var page       = require('webpage').create(),
     system     = require('system'),
     headers    = {{ request.getHeaders('json') }},
     delay      = {{ request.getDelay() }},
+    vpWidth    = {{ request.getViewportWidth() }},
+    vpHeight   = {{ request.getViewportHeight() }},
     response   = {},
     debug      = [],
     logs       = [],
     procedure  = {};
 
+/**
+ * Set viewport size.
+ */
+if(vpWidth && vpHeight) {
+    
+    debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Set viewport size ~ width: ' + vpWidth + ' height: ' + vpHeight);
+    
+    page.viewportSize = {
+        width: vpWidth,
+        height: vpHeight
+    };
+}
+
 /**
  * Define custom headers.
  */

+ 62 - 0
src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php

@@ -311,6 +311,68 @@ EOF;
         $this->assertEquals($height, $imageInfo[1]);
     }
 
+    /**
+     * Test set viewport size sets
+     * size of viewport in default
+     * request.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetViewportSizeSetsSizeOfViewportInDefaultRequest()
+    {
+        $width  = 100;
+        $height = 200;
+
+        $client = $this->getClient();
+
+        $request  = $client->getMessageFactory()->createRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $request->setMethod('GET');
+        $request->setUrl('http://jonnnnyw.github.io/php-phantomjs/tests/test-default.html');
+        $request->setViewportsize($width, $height);
+
+        $client->send($request, $response);
+
+        $logs = explode("\n", $client->getLog());
+
+        $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
+
+        $this->assertTrue(($startIndex !== false));
+    }
+
+    /**
+     * Test set viewport size sets
+     * size of viewport in capture
+     * request.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetViewportSizeSetsSizeOfViewportInCaptureRequest()
+    {
+        $width  = 100;
+        $height = 200;
+
+        $client = $this->getClient();
+
+        $request  = $client->getMessageFactory()->createCaptureRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $request->setMethod('GET');
+        $request->setUrl('http://jonnnnyw.github.io/php-phantomjs/tests/test-default.html');
+        $request->setViewportsize($width, $height);
+
+        $client->send($request, $response);
+
+        $logs = explode("\n", $client->getLog());
+
+        $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
+
+        $this->assertTrue(($startIndex !== false));
+    }
+
     /**
      * Test delay logs start time
      * in client for default request.

+ 5 - 0
src/JonnyW/PhantomJs/Tests/Unit/ClientTest.php

@@ -19,6 +19,11 @@ use JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface;
  */
 class ClientTest extends \PHPUnit_Framework_TestCase
 {
+
+/** +++++++++++++++++++++++++++++++++++ **/
+/** ++++++++++++++ TESTS ++++++++++++++ **/
+/** +++++++++++++++++++++++++++++++++++ **/
+
     /**
      * Test get instance returns instance
      * of client.

+ 34 - 0
src/JonnyW/PhantomJs/Tests/Unit/Message/CaptureRequestTest.php

@@ -521,6 +521,40 @@ class CaptureRequestTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($captureFile, $captureRequest->getCaptureFile());
     }
 
+    /**
+     * Test set viewport size sets viewport width.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetViewportSizeSetsViewportWidth()
+    {
+        $width  = 100;
+        $height = 200;
+
+        $caputreRequest = $this->getCaptureRequest();
+        $caputreRequest->setViewportSize($width, $height);
+
+        $this->assertSame($width, $caputreRequest->getViewportWidth());
+    }
+
+    /**
+     * Test set viewport size sets viewport height.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetViewportSizeSetsViewportHeight()
+    {
+        $width  = 100;
+        $height = 200;
+
+        $caputreRequest = $this->getCaptureRequest();
+        $caputreRequest->setViewportSize($width, $height);
+
+        $this->assertSame($height, $caputreRequest->getViewportHeight());
+    }
+
 /** +++++++++++++++++++++++++++++++++++ **/
 /** ++++++++++ TEST ENTITIES ++++++++++ **/
 /** +++++++++++++++++++++++++++++++++++ **/

+ 34 - 0
src/JonnyW/PhantomJs/Tests/Unit/Message/RequestTest.php

@@ -430,6 +430,40 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($headers, $request->getHeaders('default'));
     }
 
+    /**
+     * Test set viewport size sets viewport width.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetViewportSizeSetsViewportWidth()
+    {
+        $width  = 100;
+        $height = 200;
+
+        $request = $this->getRequest();
+        $request->setViewportSize($width, $height);
+
+        $this->assertSame($width, $request->getViewportWidth());
+    }
+
+    /**
+     * Test set viewport size sets viewport height.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetViewportSizeSetsViewportHeight()
+    {
+        $width  = 100;
+        $height = 200;
+
+        $request = $this->getRequest();
+        $request->setViewportSize($width, $height);
+
+        $this->assertSame($height, $request->getViewportHeight());
+    }
+
 /** +++++++++++++++++++++++++++++++++++ **/
 /** ++++++++++ TEST ENTITIES ++++++++++ **/
 /** +++++++++++++++++++++++++++++++++++ **/