Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* @Given /^I send a request to google page speed for "([^"]*)" with "([^"]*)" strategy$/
*/
public function iSendARequestToGooglePageSpeedForWithStrategy($path, $strategy) {
if ($path === 'the homepage') {
$url = $this->getMinkParameter('base_url');
}
else {
$url = $this->getMinkParameter('base_url') . $path;
}
//Try google page speed up to 5 times
for ($i=0; $i<5; $i++) {
$this->restClient = new Client();
$this->restClient->response = $this->restClient->get('https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=' . $url . '&filter_third_party_resources=false&strategy=' . $strategy . '&fields=ruleGroups');
//Grab the response code from google
$response_code = !empty($this->restClient->response->code) ? $this->restClient->response->code : FALSE;
//Stop sending requests to google page speed if we get a valid response
if (!empty($response_code) && $response_code == 200) {
break;
}
}
// If google does not respond, throw a PendingException. This allows us to
// continue executing the remaining steps without failing the whole build
if (empty($response_code) || $response_code != 200) {
throw new PendingException('Google did respond as with a ' . $response_code . 'code');
}
}
@victorpavlov

This comment has been minimized.

Copy link

victorpavlov commented May 13, 2015

Thank you Matt! There is only one small thing. We should use:

$this->restClient->response->getStatusCode()

instead of:

$this->restClient->response->code

when we are defining $response_code variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.