예제 #1
0
 public function testSetApiResponse()
 {
     $response = new Base();
     $myArray = array("foo" => "bar");
     $myObject = new StdClass();
     $myObject->foo = "bar";
     $response->setApiResponse(json_encode($myArray));
     $this->assertEquals($myObject, $response->getResponse());
 }
예제 #2
0
 /**
  * Checks if the Browserstack Screenshots Service is online and accessible
  * Can be used prior any requests to ensure that the API is working
  *
  * If anything is fine, returns an Array with index 'success'
  * If any error occured contains an index 'errors' with an error message
  *
  * @return Array
  */
 public function isBrowserstackAccessable()
 {
     $message = null;
     $headers = get_headers(self::API_BASE_URL);
     $response = new Base();
     if (!empty($headers) && isset($headers[0])) {
         $httpCode = $headers[0];
         preg_match('#HTTP\\/1\\.1\\s+(.*?)\\s+#', $httpCode, $matches);
         if (!empty($matches) && isset($matches[1])) {
             $httpCode = $matches[1];
             http_response_code($httpCode);
             switch (substr($httpCode, 0, 1)) {
                 case 2:
                     $response->setApiResponse(['success' => ['Service is available']]);
                     return $response->getResponse();
                 case 4:
                     if ($httpCode == 404) {
                         $message = 'API Endpoint not found';
                     }
                     if ($httpCode == 401) {
                         $message = 'User is not authorized';
                     }
                     if ($httpCode == 401) {
                         $message = 'Bad request';
                     }
                     break;
                 case 5:
                     $message = 'Bad request: ' . $httpCode;
                     break;
             }
         }
     } else {
         $message = 'Could not establish connection to Browserstacks';
     }
     $response->setApiResponse(['errors' => [$message]]);
     return $response->getResponse();
 }