Skip to content

Commit

Permalink
Adding body styles partial allowing CSS styles to be set on the body …
Browse files Browse the repository at this point in the history
…tag of the requested page - #92
  • Loading branch information
jonnnnyw committed May 2, 2016
1 parent 5ba4950 commit 1e09977
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/JonnyW/PhantomJs/Http/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ abstract class AbstractRequest
*/
protected $viewportHeight;

/**
* Body styles.
*
* @var int
* @access protected
*/
protected $bodyStyles;

/**
* Internal constructor
*
Expand All @@ -97,6 +105,7 @@ public function __construct($url = null, $method = RequestInterface::METHOD_GET,
{
$this->headers = array();
$this->data = array();
$this->bodyStyles = array();
$this->delay = 0;
$this->viewportWidth = 0;
$this->viewportHeight = 0;
Expand Down Expand Up @@ -377,6 +386,36 @@ public function getHeaders($format = 'default')
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
* dimensional array
Expand Down
16 changes: 16 additions & 0 deletions src/JonnyW/PhantomJs/Http/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,20 @@ public function addHeaders(array $headers);
* @return array
*/
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ phantom.onError = function(msg, trace) {
* Open page
*/
page.open ('{{ input.getUrl() }}', '{{ input.getMethod() }}', '{{ input.getBody() }}', function (status) {
[[ engine.load('page_body_styles') ]]
[[ engine.load('page_open') ]]
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

page.evaluate(function() {

var styles = {{ input.getBodyStyles('json') }};

for(var property in styles) {
document.body.style[property] = styles[property];
}
});
31 changes: 29 additions & 2 deletions src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function testResponseContainsHeaders()

$this->assertNotEmpty($response->getHeaders());
}

/**
* Test redirect URL is set in response
* if request is redirected.
Expand Down Expand Up @@ -310,7 +310,7 @@ public function testCaptureRequestSavesFileToLocalDisk()
$response = $client->getMessageFactory()->createResponse();

$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);

$client->send($request, $response);
Expand Down Expand Up @@ -750,6 +750,33 @@ public function testDebugLogsDebugInfoToClientLog()
$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 ++++++++++ **/
/** +++++++++++++++++++++++++++++++++++ **/
Expand Down

0 comments on commit 1e09977

Please sign in to comment.