You are here

class DummyAcquiaLiftHttpClient in Acquia Lift Connector 7.2

Same name and namespace in other branches
  1. 7 tests/acquia_lift.test_classes.inc \DummyAcquiaLiftHttpClient

Classes used for testing.

Hierarchy

Expanded class hierarchy of DummyAcquiaLiftHttpClient

File

tests/acquia_lift.test_classes.inc, line 10
Provides test classes for Acquia Lift

View source
class DummyAcquiaLiftHttpClient implements AcquiaLiftDrupalHttpClientInterface {

  /**
   * Stores all requests that have been received.
   *
   * @var array
   *   An array of requests.
   */
  protected static $requests_received;

  /**
   * Whether or not this http client should return 500 errors.
   *
   * @var bool
   */
  protected $broken;

  /**
   * The type of breakage to simulate, i.e. client or server side.
   *
   * @var string
   */
  protected $breakageType;

  /**
   * An array of data simulating resources than can be returned.
   *
   * @var array
   */
  protected $data = array();

  /**
   * Generates a dummy response based on the passed in data.
   *
   * @param array $data
   *   An array of data for the response.
   * @return stdClass
   *   An object representing a response from the server.
   */
  protected function generateDummyResponse($data) {
    $response = new stdClass();
    $response->code = 200;
    $response->status_message = 'OK';
    if ($this->broken) {
      if ($this->breakageType == 'client') {
        $response->code = 400;
        $response->status_message = 'Bad request';
      }
      else {
        $response->code = 500;
        $response->status_message = 'Internal Server Error';
      }
    }
    $response->data = drupal_json_encode($data);
    return $response;
  }

  /**
   * Constructor
   *
   * @param bool $broken
   *   Whether or not this http client should just get 500 errors.
   * @param array $data
   *   An array of dummy data that can be returned in responses.
   * @param bool $simulate_client_side_breakage
   *   Do simulate client side breakage.
   */
  public function __construct($broken = FALSE, $data = array(), $simulate_client_side_breakage = FALSE) {
    $this->broken = $broken;
    $this->breakageType = $simulate_client_side_breakage ? 'client' : 'server';
    $this->data += $data;
  }

  /**
   * Logs the request internally.
   *
   * @param $type
   *   The type of request, e.g. 'get'
   * @param $uri
   *   The uri of the request.
   * @param $headers
   *   The array of headers.
   * @param $options
   *   An array of options
   * @param null $body
   *   (optional) The body of the request.
   */
  protected function logRequest($type, $uri, $headers, $options, $body = NULL) {
    self::$requests_received[] = array(
      'type' => $type,
      'uri' => $uri,
      'headers' => $headers,
      'options' => $options,
      'body' => $body,
    );
  }

  /**
   * Returns all requests that have been made to this client.
   *
   * @return array
   *   An array of requests
   */
  public static function getLoggedRequests() {
    return self::$requests_received;
  }
  public static function clearLoggedRequests() {
    self::$requests_received = array();
  }

  /**
   * Returns the expected data array for a given uri.
   *
   * @param string $uri
   *   An absolute url for an API endpoint, e.g. http://example.com/owner-code/list-agents
   * @return array
   *   An array of data to be returned in the response.
   */
  protected function getDataForURI($uri, $headers = array()) {
    $parsed = parse_url($uri);
    $path_parts = explode('/', $parsed['path']);

    // The first element of the $path_parts array will be an empty string.
    $path_parts = array_slice($path_parts, 1);
    switch ($path_parts[0]) {
      case 'campaigns':
        if (isset($this->data['campaigns'])) {
          return $this->data['campaigns'];
        }
        if (isset($this->data['error'])) {
          return $this->data['error'];
        }
        return array();
      case 'report':
        $query_params = explode('&', $parsed['query']);
        $first_param = explode('=', reset($query_params));
        if ($first_param[0] !== 'campaign_id') {
          return array();
        }
        $agent_name = $first_param[1];
        return isset($this->data['reports'][$agent_name]) ? $this->data['reports'][$agent_name] : array();
      case 'ping':
        return array(
          'message' => 'pong!',
        );
        break;
    }
    return array();
  }

  /**
   * Implements AcquiaLiftDrupalHttpClientInterface::get().
   */
  public function get($uri = null, $headers = null, array $options = array()) {
    $this
      ->logRequest('get', $uri, $headers, $options);
    $data = $this
      ->getDataForURI($uri, $headers);
    return $this
      ->generateDummyResponse($data);
  }

  /**
   * Implements AcquiaLiftDrupalHttpClientInterface::put().
   */
  public function put($uri = null, $headers = null, $body = null, array $options = array()) {
    $this
      ->logRequest('put', $uri, $headers, $options, $body);
    return $this
      ->generateDummyResponse(array(
      'status' => 'ok',
    ));
  }

  /**
   * Implements AcquiaLiftDrupalHttpClientInterface::post().
   */
  public function post($uri = null, $headers = null, $body = null, array $options = array()) {
    $this
      ->logRequest('post', $uri, $headers, $options, $body);
    return $this
      ->generateDummyResponse(array(
      'status' => 'ok',
    ));
  }

  /**
   * Implements AcquiaLiftDrupalHttpClientInterface::delete().
   */
  public function delete($uri = null, $headers = null, $body = null, array $options = array()) {
    $this
      ->logRequest('delete', $uri, $headers, $options, $body);
    return $this
      ->generateDummyResponse(array(
      'status' => 'ok',
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DummyAcquiaLiftHttpClient::$breakageType protected property The type of breakage to simulate, i.e. client or server side.
DummyAcquiaLiftHttpClient::$broken protected property Whether or not this http client should return 500 errors.
DummyAcquiaLiftHttpClient::$data protected property An array of data simulating resources than can be returned.
DummyAcquiaLiftHttpClient::$requests_received protected static property Stores all requests that have been received.
DummyAcquiaLiftHttpClient::clearLoggedRequests public static function
DummyAcquiaLiftHttpClient::delete public function Implements AcquiaLiftDrupalHttpClientInterface::delete(). Overrides AcquiaLiftDrupalHttpClientInterface::delete
DummyAcquiaLiftHttpClient::generateDummyResponse protected function Generates a dummy response based on the passed in data.
DummyAcquiaLiftHttpClient::get public function Implements AcquiaLiftDrupalHttpClientInterface::get(). Overrides AcquiaLiftDrupalHttpClientInterface::get 1
DummyAcquiaLiftHttpClient::getDataForURI protected function Returns the expected data array for a given uri.
DummyAcquiaLiftHttpClient::getLoggedRequests public static function Returns all requests that have been made to this client.
DummyAcquiaLiftHttpClient::logRequest protected function Logs the request internally.
DummyAcquiaLiftHttpClient::post public function Implements AcquiaLiftDrupalHttpClientInterface::post(). Overrides AcquiaLiftDrupalHttpClientInterface::post
DummyAcquiaLiftHttpClient::put public function Implements AcquiaLiftDrupalHttpClientInterface::put(). Overrides AcquiaLiftDrupalHttpClientInterface::put
DummyAcquiaLiftHttpClient::__construct public function Constructor