class DummyAcquiaLiftHttpClient in Acquia Lift Connector 7
Same name and namespace in other branches
- 7.2 tests/acquia_lift.test_classes.inc \DummyAcquiaLiftHttpClient
Classes used for testing.
Hierarchy
- class \DummyAcquiaLiftHttpClient implements AcquiaLiftDrupalHttpClientInterface
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.
*/
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 and the second
// will be the owner code, which we don't need here.
$path_parts = array_slice($path_parts, 2);
switch ($path_parts[0]) {
case 'list-agents':
return array(
'data' => array(
'agents' => isset($this->data['agents']) ? $this->data['agents'] : array(),
),
);
case 'transforms-options':
return array(
'data' => array(
'options' => isset($this->data['options']) ? $this->data['options'] : array(),
),
);
case '-':
// For some reason the endpoint for getting targeting values is '/-/potential-targeting'
if ($path_parts[1] != 'potential-targeting') {
return array();
}
if (isset($this->data['features'])) {
return array(
'data' => array(
'potential' => array(
'features' => $this->data['features'],
),
),
);
}
if (isset($this->data['reports'])) {
// Grab the agent name from the querystring.
$query_params = explode('&', $parsed['query']);
$first_param = explode('=', reset($query_params));
if ($first_param[0] !== 'agent') {
return array();
}
$agent_name = $first_param[1];
if (isset($this->data['reports'][$agent_name]['context-filters'])) {
return $this->data['reports'][$agent_name]['context-filters'];
}
}
return array(
'data' => array(
'potential' => array(
'features' => array(),
),
),
);
case 'agent-api':
$agent_name = $path_parts[1];
if (count($path_parts) == 2) {
return array(
'machine-name' => $agent_name,
'status' => PERSONALIZE_STATUS_NOT_STARTED,
);
}
switch ($path_parts[2]) {
case 'points':
// This request must be for the list of points for an agent, or the list of decisions
// for a point, or the list of choices for a decision. Use a regex to figure out which.
$matches = array();
$pattern = '/agent\\-api\\/[a-zA-Z0-9-_]+(\\/points\\/[a-zA-Z0-9-_]+\\/decisions(\\/[a-zA-Z0-9-_]+\\/choices)?)?/';
if (!preg_match($pattern, $parsed['path'], $matches)) {
return array();
}
switch (count($matches)) {
case 1:
// This is a request for points for an agent.
return isset($this->data['points'][$agent_name]) ? $this->data['points'][$agent_name] : array();
case 2:
// This is a request for decisions for a point.
$point_name = $path_parts[3];
return isset($this->data['decisions'][$agent_name][$point_name]) ? $this->data['decisions'][$agent_name][$point_name] : array();
case 3:
// This is a request for choices for a decision.
$point_name = $path_parts[3];
$decision_name = $path_parts[5];
return isset($this->data['choices'][$agent_name][$point_name][$decision_name]) ? $this->data['choices'][$agent_name][$point_name][$decision_name] : array();
}
return array();
case 'goals':
return isset($this->data['goals'][$agent_name]) ? $this->data['goals'][$agent_name] : array();
}
break;
}
// Reports (unfortunately not all of which have the same url pattern)
if (strpos($parsed['path'], 'report/status') !== FALSE) {
$query_params = explode('&', $parsed['query']);
$first = explode('=', reset($query_params));
if ($first[0] !== 'codes') {
return array();
}
$agent_names = explode(',', $first[1]);
$agent_name = reset($agent_names);
return isset($this->data['reports'][$agent_name]['agent-status']) ? $this->data['reports'][$agent_name]['agent-status'] : array();
}
if (isset($path_parts[1]) && $path_parts[1] == 'report') {
$matches = array();
$pattern = '/([a-zA-Z0-9-_]+)\\/report\\/(confidence|targeting\\-features|learning)/';
if (!preg_match($pattern, $parsed['path'], $matches)) {
return array();
}
$agent_name = $matches[1];
$report_type = $matches[2];
if ($report_type == 'confidence' && isset($headers['x-mpath-point'])) {
// We're getting a confidence report for a particular decision point.
if (isset($this->data['reports'][$agent_name]['confidence'])) {
$confidence_report = $this->data['reports'][$agent_name]['confidence'];
foreach ($confidence_report['data']['items'] as $i => $item) {
if ($item['point'] !== $headers['x-mpath-point']) {
unset($confidence_report['data']['items'][$i]);
}
}
$confidence_report['data']['items'] = array_values($confidence_report['data']['items']);
return $confidence_report;
}
}
return isset($this->data['reports'][$agent_name][$report_type]) ? $this->data['reports'][$agent_name][$report_type] : array();
}
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',
));
}
}