Skip to content

Instantly share code, notes, and snippets.

@MKorostoff
Created May 12, 2015 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MKorostoff/8ee27d9cc517850a4e8e to your computer and use it in GitHub Desktop.
Save MKorostoff/8ee27d9cc517850a4e8e to your computer and use it in GitHub Desktop.
<?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
Copy link

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