You are here

public function AcquiaLiftTest::testSaveChoice in Acquia Lift Connector 7

Test AcquiaLiftAPI->saveChoice()

File

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

Class

AcquiaLiftTest
@file Unit tests for Acquia Lift module.

Code

public function testSaveChoice() {
  $lift_api = $this
    ->getAcquiaLiftAPI();
  $agentName = 'some-test-agent';
  $pointName = 'Some Test Point';
  $decisionName = 'Some Test Decision';
  $choiceName = 'Some Test Choice';
  $lift_api
    ->saveChoice($agentName, $pointName, $decisionName, $choiceName);

  // 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}/points/{$pointName}/decisions/{$decisionName}/choices/{$choiceName}?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 decision choice {$decisionName}: {$choiceName} for point {$pointName} was pushed to the Acquia Lift campaign {$agentName}",
    ),
  );
  $this
    ->assertLogs($logs);
  $this->logger
    ->clearLogs();
  $lift_api
    ->saveChoice($agentName, $pointName, $decisionName, $choiceName, array(
    'key' => 'data',
  ));

  // 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}/points/{$pointName}/decisions/{$decisionName}/choices/{$choiceName}?apikey={$lift_api->getAdminKey()}",
      'headers' => array(
        'Content-Type' => 'application/json; charset=utf-8',
        'Accept' => 'application/json',
      ),
      'options' => array(),
      'body' => array(
        'key' => 'data',
      ),
    ),
  );

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::INFO,
      'message' => "The decision choice {$decisionName}: {$choiceName} for point {$pointName} was pushed to the Acquia Lift campaign {$agentName}",
    ),
  );
  $this
    ->assertLogs($logs);
  $this->logger
    ->clearLogs();
  $lift_api = $this
    ->getAcquiaLiftAPI(TRUE);
  try {
    $lift_api
      ->saveChoice($agentName, $pointName, $decisionName, $choiceName, array(
      'key' => '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 decision choice {$decisionName}: {$choiceName} for point {$pointName} to the Acquia Lift campaign {$agentName}",
    ),
  );
  $this
    ->assertLogs($logs);
  $this->logger
    ->clearLogs();
}