Explorar o código

Adding body styles partial allowing CSS styles to be set on the body tag of the requested page - https://github.com/jonnnnyw/php-phantomjs/issues/92

Jonny Wenmoth %!s(int64=9) %!d(string=hai) anos
pai
achega
1e09977d6f

+ 39 - 0
src/JonnyW/PhantomJs/Http/AbstractRequest.php

@@ -85,6 +85,14 @@ abstract class AbstractRequest
      */
      */
     protected $viewportHeight;
     protected $viewportHeight;
 
 
+    /**
+     * Body styles.
+     *
+     * @var int
+     * @access protected
+     */
+    protected $bodyStyles;
+
     /**
     /**
      * Internal constructor
      * Internal constructor
      *
      *
@@ -97,6 +105,7 @@ abstract class AbstractRequest
     {
     {
         $this->headers        = array();
         $this->headers        = array();
         $this->data           = array();
         $this->data           = array();
+        $this->bodyStyles     = array();
         $this->delay          = 0;
         $this->delay          = 0;
         $this->viewportWidth  = 0;
         $this->viewportWidth  = 0;
         $this->viewportHeight = 0;
         $this->viewportHeight = 0;
@@ -377,6 +386,36 @@ abstract class AbstractRequest
         return $this->headers;
         return $this->headers;
     }
     }
 
 
+    /**
+     * Set body styles
+     *
+     * @access public
+     * @param  array                                  $styles
+     * @return \JonnyW\PhantomJs\Http\AbstractRequest
+     */
+    public function setBodyStyles(array $styles)
+    {
+        $this->bodyStyles = $styles;
+
+        return $this;
+    }
+
+    /**
+     * Get body styles
+     *
+     * @access public
+     * @param string $format (default: 'default')
+     * @return array
+     */
+    public function getBodyStyles($format = 'default')
+    {
+        if ($format === 'json') {
+            return json_encode($this->bodyStyles);
+        }
+
+        return $this->bodyStyles;
+    }
+
     /**
     /**
      * Flatten data into single
      * Flatten data into single
      * dimensional array
      * dimensional array

+ 16 - 0
src/JonnyW/PhantomJs/Http/RequestInterface.php

@@ -184,4 +184,20 @@ interface RequestInterface
      * @return array
      * @return array
      */
      */
     public function getHeaders($format = 'default');
     public function getHeaders($format = 'default');
+
+    /**
+     * Set body styles
+     *
+     * @access public
+     * @param array $styles
+     */
+    public function setBodyStyles(array $styles);
+
+    /**
+     * Get body styles
+     *
+     * @access public
+     * @return array
+     */
+    public function getBodyStyles();
 }
 }

+ 1 - 0
src/JonnyW/PhantomJs/Resources/procedures/http_default.proc

@@ -75,6 +75,7 @@ phantom.onError = function(msg, trace) {
  * Open page
  * Open page
  */
  */
 page.open ('{{ input.getUrl() }}', '{{ input.getMethod() }}', '{{ input.getBody() }}', function (status) {
 page.open ('{{ input.getUrl() }}', '{{ input.getMethod() }}', '{{ input.getBody() }}', function (status) {
+    [[ engine.load('page_body_styles') ]]
     [[ engine.load('page_open') ]]
     [[ engine.load('page_open') ]]
 });
 });
 
 

+ 9 - 0
src/JonnyW/PhantomJs/Resources/procedures/page_body_styles.partial

@@ -0,0 +1,9 @@
+
+page.evaluate(function() {
+    
+    var styles = {{ input.getBodyStyles('json') }};
+    
+    for(var property in styles) {
+        document.body.style[property] = styles[property];
+    }
+});

+ 29 - 2
src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php

@@ -243,7 +243,7 @@ EOF;
 
 
         $this->assertNotEmpty($response->getHeaders());
         $this->assertNotEmpty($response->getHeaders());
     }
     }
-    
+
     /**
     /**
      * Test redirect URL is set in response
      * Test redirect URL is set in response
      * if request is redirected.
      * if request is redirected.
@@ -310,7 +310,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
         $response = $client->getMessageFactory()->createResponse();
 
 
         $request->setMethod('GET');
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
+        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
         $request->setOutputFile($file);
         $request->setOutputFile($file);
 
 
         $client->send($request, $response);
         $client->send($request, $response);
@@ -750,6 +750,33 @@ EOF;
         $this->assertContains('[DEBUG]', $client->getLog());
         $this->assertContains('[DEBUG]', $client->getLog());
     }
     }
 
 
+    /**
+     * Test test can set page 
+     * background color
+     *
+     * @access public
+     * @return void
+     */
+    public function testCanSetPageBackgroundColor()
+    {
+        $this->filename = 'test.jpg';
+        $file = ($this->directory . '/' . $this->filename);
+
+        $client = $this->getClient();
+
+        $request  = $client->getMessageFactory()->createCaptureRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $request->setMethod('GET');
+        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setBodyStyles(array('backgroundColor' => 'red'));
+        $request->setOutputFile($file);
+
+        $client->send($request, $response);
+
+        $this->assertContains('body style="background-color: red;"', $response->getContent());
+    }
+
 /** +++++++++++++++++++++++++++++++++++ **/
 /** +++++++++++++++++++++++++++++++++++ **/
 /** ++++++++++ TEST ENTITIES ++++++++++ **/
 /** ++++++++++ TEST ENTITIES ++++++++++ **/
 /** +++++++++++++++++++++++++++++++++++ **/
 /** +++++++++++++++++++++++++++++++++++ **/