You are here

public function AcquiaLiftTest::testSaveGoal in Acquia Lift Connector 7

Same name and namespace in other branches
  1. 7.2 tests/AcquiaLiftAPI.test \AcquiaLiftTest::testSaveGoal()

Test AcquiaLiftAPI->saveGoal()

File

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

Class

AcquiaLiftTest
@file Unit tests for Acquia Lift module.

Code

public function testSaveGoal() {
  $lift_api = $this
    ->getAcquiaLiftAPI();
  $agentName = 'some-test-agent';
  $goalName = 'Some Test Point';
  $data = array(
    'key' => 'value',
  );
  $lift_api
    ->saveGoal($agentName, $goalName);

  // Define the requests we expect to have been made to our dummy http
  // client for this operation.
  $requests = array(
    array(
      'type' => 'put',
      'uri' => "{$lift_api->getApiUrl()}/{$lift_api->getOwnerCode()}/agent-api/{$agentName}/goals/{$goalName}?apikey={$lift_api->getAdminKey()}",
      'headers' => array(
        'Content-Type' => 'application/json; charset=utf-8',
        'Accept' => 'application/json',
      ),
      'options' => array(),
      'body' => NULL,
    ),
  );

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::INFO,
      'message' => "The goal {$goalName} was pushed to the Acquia Lift campaign {$agentName}",
    ),
  );
  $this
    ->assertLogs($logs);

  // Try to pass data structure
  $lift_api
    ->saveGoal($agentName, $goalName, $data);
  $requests = array(
    array(
      'type' => 'put',
      'uri' => "{$lift_api->getApiUrl()}/{$lift_api->getOwnerCode()}/agent-api/{$agentName}/goals/{$goalName}?apikey={$lift_api->getAdminKey()}",
      'headers' => array(
        'Content-Type' => 'application/json; charset=utf-8',
        'Accept' => 'application/json',
      ),
      'options' => array(),
      'body' => $data,
    ),
  );

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::INFO,
      'message' => "The goal {$goalName} was pushed to the Acquia Lift campaign {$agentName}",
    ),
  );
  $this
    ->assertLogs($logs);
  $lift_api = $this
    ->getAcquiaLiftAPI(TRUE);
  try {
    $lift_api
      ->saveGoal($agentName, $goalName, $data);
  } catch (Exception $e) {
    $this
      ->assertTrue($e instanceof AcquiaLiftException);
  }

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::ERROR,
      'message' => "Could not save the goal {$goalName} to the Acquia Lift campaign {$agentName}",
    ),
  );
  $this
    ->assertLogs($logs);
}