You are here

function AcquiaLiftTest::testSaveCampaign in Acquia Lift Connector 7.2

Tests the saveCampaign method.

@todo Add tests for other settings being passed.

File

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

Class

AcquiaLiftTest
@file Unit tests for Acquia Lift module.

Code

function testSaveCampaign() {
  $lift_api = $this
    ->getAcquiaLiftAPI();
  $agent_name = 'some-test-agent';
  $agent_title = 'Some Test Agent';
  $lift_api
    ->saveCampaign($agent_name, $agent_title, 'some-decision', array(
    'some-goal',
  ));
  $logged_requests = DummyAcquiaLiftHttpClient::getLoggedRequests();
  $expected_uri = "{$lift_api->getApiUrl()}/campaigns?client_id={$lift_api->getPublicKey()}";
  $expected_body = json_encode(array(
    'id' => $agent_name,
    'title' => $agent_title,
    'algorithm' => 'mab',
    'decision_sets' => array(
      'some-decision',
    ),
    'goals' => array(
      'some-goal',
    ),
    'traffic_fraction' => 1,
    'explore_fraction' => 1,
  ));

  // Confirm the request URI and body are as expected.
  $this
    ->assertAPIRequestURI($expected_uri, $logged_requests[0]['uri']);
  $this
    ->assertEqual($expected_body, $logged_requests[0]['body']);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::INFO,
      'message' => "The personalization {$agent_name} was pushed to Acquia Lift",
    ),
  );
  $this
    ->assertLogs($logs);
  DummyAcquiaLiftHttpClient::clearLoggedRequests();
  $this->logger
    ->clearLogs();

  // Now specify different options for the test.
  $agent_name = 'new-test-agent';
  $agent_title = 'New Test Agent';
  $lift_api
    ->saveCampaign($agent_name, $agent_title, 'some-decision', array(
    'some-goal',
  ), TRUE, 0, 0.4);
  $logged_requests = DummyAcquiaLiftHttpClient::getLoggedRequests();
  $expected_uri = "{$lift_api->getApiUrl()}/campaigns?client_id={$lift_api->getPublicKey()}";
  $expected_body = json_encode(array(
    'id' => $agent_name,
    'title' => $agent_title,
    'algorithm' => 'mab',
    'decision_sets' => array(
      'some-decision',
    ),
    'goals' => array(
      'some-goal',
    ),
    'traffic_fraction' => 1,
    'explore_fraction' => 0.4,
  ));

  // Confirm the request URI and body are as expected.
  $this
    ->assertAPIRequestURI($expected_uri, $logged_requests[0]['uri']);
  $this
    ->assertEqual($expected_body, $logged_requests[0]['body']);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::INFO,
      'message' => "The personalization {$agent_name} was pushed to Acquia Lift",
    ),
  );
  $this
    ->assertLogs($logs);
  DummyAcquiaLiftHttpClient::clearLoggedRequests();
  $this->logger
    ->clearLogs();

  // Now try with a broken http client.
  $lift_api = $this
    ->getAcquiaLiftAPI(TRUE);
  try {
    $lift_api
      ->saveCampaign($agent_name, $agent_title, 'some-decision', array(
      'some-goal',
    ));
  } catch (Exception $e) {
    $this
      ->assertTrue($e instanceof AcquiaLiftException);
  }

  // The same requests should be made.
  $this
    ->assertAPIRequestURI($expected_uri, $logged_requests[0]['uri']);

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