You are here

function AcquiaLiftTest::testSaveAgent in Acquia Lift Connector 7

File

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

Class

AcquiaLiftTest
@file Unit tests for Acquia Lift module.

Code

function testSaveAgent() {
  $lift_api = $this
    ->getAcquiaLiftAPI();
  $agentName = 'some-test-agent';
  $lift_api
    ->saveAgent($agentName, 'Some Test Agent', 'adaptive', 'enabled');

  // 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/{$agentName}?apikey={$this->liftAdminKey}",
      'headers' => array(
        'Content-Type' => 'application/json; charset=utf-8',
        'Accept' => 'application/json',
      ),
      'options' => array(),
      'body' => array(
        'name' => 'Some Test Agent',
        'selection-mode' => 'adaptive',
        'status' => 'enabled',
        'control-rate' => 0.1,
        'explore-rate' => 0.2,
        'decision-stickiness' => 'session',
      ),
    ),
  );

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

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

  // Make sure we can use a Personalize status code.
  $this
    ->assertAPIRequests(array());
  $lift_api
    ->saveAgent($agentName, 'Some Test Agent', 'adaptive', PERSONALIZE_STATUS_RUNNING);

  // The requests and logs should be the same.
  $this
    ->assertAPIRequests($requests);
  $this
    ->assertLogs($logs);
  $this->logger
    ->clearLogs();
  $this
    ->assertAPIRequests(array());
  $lift_api
    ->saveAgent($agentName, 'Some Test Agent', 'adaptive', 'enabled', 0.1, 0.4);

  // 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/{$agentName}?apikey={$this->liftAdminKey}",
      'headers' => array(
        'Content-Type' => 'application/json; charset=utf-8',
        'Accept' => 'application/json',
      ),
      'options' => array(),
      'body' => array(
        'name' => 'Some Test Agent',
        'selection-mode' => 'adaptive',
        'status' => 'enabled',
        'control-rate' => 0.1,
        'explore-rate' => 0.4,
        'decision-stickiness' => 'session',
      ),
    ),
  );

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

  // Save an agent without sticky decisions
  $lift_api
    ->saveAgent($agentName, 'Some Test Agent', 'adaptive', 'enabled', 0.1, 0.4, 0);
  $requests[0]['body']['decision-stickiness'] = 'none';

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

  // Try with something nonsensical for the decision mode.
  $lift_api
    ->saveAgent($agentName, 'Some Test Agent', 'narf', 'enabled', 0.1, 0.4, 0);

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

  // Now try with a broken http client.
  $lift_api = $this
    ->getAcquiaLiftAPI(TRUE);
  try {
    $lift_api
      ->saveAgent('some-test-agent', 'Some Test Agent', 'adaptive', 'enabled', 0.1, 0.4, 0);
  } catch (Exception $e) {
    $this
      ->assertTrue($e instanceof AcquiaLiftException);
  }

  // The same requests should be made.
  $this
    ->assertAPIRequests($requests);

  // Confirm the expected error message was logged.
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::ERROR,
      'message' => "The campaign {$agentName} could not be pushed to Acquia Lift",
    ),
  );
  $this
    ->assertLogs($logs);
  $this->logger
    ->clearLogs();
}