You are here

function ALProfilesAPITest::testGetSegements in Acquia Lift Connector 7

File

acquia_lift_profiles/tests/acquia_lift_profiles_unit.test, line 112
Unit tests for Acquia Lift Profiles module.

Class

ALProfilesAPITest
@file Unit tests for Acquia Lift Profiles module.

Code

function testGetSegements() {
  $acquia_lift_profiles_api = $this
    ->getALProfilesAPI();
  $acquia_lift_profiles_api
    ->getSegments();

  // Define the requests we expect to have been made to our dummy http
  // client for this operation.
  $canonicalRequest = "GET\naccept:application/json\n/dashboard/rest/{$acquia_lift_profiles_api->getAccountName()}/segments";
  $hmac = base64_encode(hash_hmac('sha1', $canonicalRequest, $this->secretKey, TRUE));
  $auth_header = 'HMAC ' . $this->accessKey . ':' . $hmac;
  $requests = array(
    array(
      'type' => 'get',
      'uri' => "{$acquia_lift_profiles_api->getApiUrl()}/dashboard/rest/{$acquia_lift_profiles_api->getAccountName()}/segments",
      'headers' => array(
        'Authorization' => $auth_header,
        'Accept' => 'application/json',
      ),
      'options' => array(),
      'body' => NULL,
    ),
  );

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array();
  $this
    ->assertLogs($logs);

  // Now try with a broken http client (simulating a bad response).
  $acquia_lift_profiles_api = $this
    ->getALProfilesAPI(TRUE);
  try {
    $acquia_lift_profiles_api
      ->getSegments();
    $this
      ->fail('Exception should have been thrown');
  } catch (Exception $e) {
  }

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => 'error',
      'message' => 'Could not get segments from Acquia Lift Profiles "500 Internal Server Error"',
    ),
  );
  $this
    ->assertLogs($logs);
  ALProfilesAPI::reset();
}