You are here

function AcquiaLiftTest::testUpdateAgentStatus in Acquia Lift Connector 7

File

tests/AcquiaLiftAPI.test, line 1910
Unit tests for Acquia Lift module.

Class

AcquiaLiftTest
@file Unit tests for Acquia Lift module.

Code

function testUpdateAgentStatus() {
  $lift_api = $this
    ->getAcquiaLiftAPI();
  DummyAcquiaLiftHttpClient::clearLoggedRequests();
  $agent_name = 'my-test-agent';

  // The updating the status to a non-existent status code string.
  try {
    $lift_api
      ->updateAgentStatus($agent_name, 'some-non-existent-status-code');
    $this
      ->fail('Exception not thrown as expected');
  } catch (Exception $e) {
    $this
      ->pass('Exception thrown as expected');
  }

  // Now try with a non-existent numeric code.
  try {
    $lift_api
      ->updateAgentStatus($agent_name, 33);
    $this
      ->fail('Exception not thrown as expected');
  } catch (Exception $e) {
    $this
      ->pass('Exception thrown as expected');
  }

  // Now try with a string code that exists in Lift.
  try {
    $lift_api
      ->updateAgentStatus($agent_name, AcquiaLiftAPI::PAUSED_STATUS);
  } catch (Exception $e) {
    $this
      ->fail('Exception thrown when none expected');
  }

  // Define the requests we expect to have been made to our dummy http
  // client for this operation.
  $requests = array(
    array(
      'type' => 'put',
      'uri' => "http://api.lift.acquia.com/{$this->liftOwnerCode}/agent-api/{$agent_name}?apikey={$this->liftAdminKey}",
      'headers' => array(
        'Content-Type' => 'application/json; charset=utf-8',
        'Accept' => 'application/json',
      ),
      'options' => array(),
      'body' => array(
        'status' => AcquiaLiftAPI::PAUSED_STATUS,
      ),
    ),
  );

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

  // Confirm the expected messages were logged.
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::INFO,
      'message' => "The new status of campaign {$agent_name} was pushed to Acquia Lift",
    ),
  );
  $this
    ->assertLogs($logs);
  $this->logger
    ->clearLogs();

  // Now try with a numeric code that exists in Personalize.
  try {
    $lift_api
      ->updateAgentStatus($agent_name, PERSONALIZE_STATUS_PAUSED);
  } catch (Exception $e) {
    $this
      ->fail('Exception thrown when none expected');
  }

  // The request made and the logs should be identical.
  $this
    ->assertAPIRequests($requests);
  $this
    ->assertLogs($logs);
  $this->logger
    ->clearLogs();

  // Now try with a broken http client.
  $lift_api = $this
    ->getAcquiaLiftAPI(TRUE);
  try {
    $lift_api
      ->updateAgentStatus($agent_name, PERSONALIZE_STATUS_PAUSED);
    $this
      ->fail('Exception not thrown as expected');
  } catch (Exception $e) {
    $this
      ->pass('Exception thrown as expected');
  }
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::ERROR,
      'message' => "The new status of campaign {$agent_name} could not be pushed to Acquia Lift",
    ),
  );
  $this
    ->assertLogs($logs);
  $this->logger
    ->clearLogs();
}