Example #1
0
 /**
  * Returns a mocked response
  *
  * @param string $path
  * @param array $options
  *
  * @return mixed
  */
 public function get($path, array $options = [], array $headers = [], $array = false, $useProxy = true, $permanent = false, $force = false)
 {
     $path = ConnectorTrait::sanitizePath($path);
     $query = http_build_query($options);
     if ($query !== '') {
         $path .= "?{$query}";
     }
     if (isset($this->responses[$path])) {
         return $this->responses[$path];
     }
     $response = new \stdClass();
     $response->status = 'error';
     $response->message = "No mock route '{$path}' found in app/config/responses.yml";
     return $response;
 }
 /**
  * @param $url
  * @param array $options
  * @param array $headers
  * @param bool $array
  * @param bool $permanent
  * @param bool $force
  *
  * @return mixed
  */
 protected function getFromProxy($url, array $options = [], array $headers = [], $array = false, $permanent = false, $force = false)
 {
     $requestUrl = ConnectorTrait::bindUrlOptions($url, $options);
     $key = ['url' => $requestUrl];
     $requestHeaders = $this->buildHeaders($requestUrl, $headers);
     $now = new \DateTime();
     $storedCall = $this->proxy->find($key);
     if (null === $storedCall || $force) {
         $response = $this->getAbsolute($url, $options, $headers, false, false);
         if ($response) {
             $data = ['data' => $response, 'url' => $url, 'headers' => $this->getLastHeaders(), 'timestamp' => $now->getTimestamp(), 'ttl' => $permanent ? null : 86400, 'permanent' => $permanent, 'request_headers' => $requestHeaders];
             $this->proxy->save($key, $data);
         }
         return json_decode(json_encode($response), $array);
     }
     if ($this->recordHasExpired($storedCall)) {
         $this->proxy->delete($key);
         return $this->getFromProxy($url, $options, $requestHeaders, $array, $permanent, $force);
     }
     $this->lastHeaders = json_decode(json_encode($storedCall->headers), true);
     return json_decode(json_encode($storedCall->data), $array);
 }