You are here

public function AcquiaLiftTest::testSaveAutoTargetingRule in Acquia Lift Connector 7

Test AcquiaLiftAPI->saveAutoTargetingRule()

File

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

Class

AcquiaLiftTest
@file Unit tests for Acquia Lift module.

Code

public function testSaveAutoTargetingRule() {
  $lift_api = $this
    ->getAcquiaLiftAPI();
  $agentName = 'some-agent-name';
  $autoFeatures = array(
    'first',
    'second',
    'third',
  );
  $lift_api
    ->saveAutoTargetingRule($agentName, $autoFeatures);
  $processedAutoFeatures = array();
  foreach ($autoFeatures as $key => $feature) {
    $processedAutoFeatures[$key] = '#' . $feature;
  }

  // Define the requests we expect to have been made to our dummy http
  // client for this operation.
  $requests = array(
    array(
      'type' => 'post',
      'uri' => "{$lift_api->getApiUrl()}/{$lift_api->getOwnerCode()}/transform-rule?apikey={$lift_api->getAdminKey()}",
      'headers' => array(
        'Content-Type' => 'application/json; charset=utf-8',
        'Accept' => 'application/json',
      ),
      'options' => array(),
      'body' => array(
        'code' => $agentName . '-auto-targeting',
        'status' => 1,
        'agents' => array(
          $agentName,
        ),
        'when' => array(),
        'apply' => array(
          'feature' => implode(',', $processedAutoFeatures),
        ),
      ),
    ),
  );

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::INFO,
      'message' => "The targeting rule for campaign {$agentName} was saved successfully",
    ),
  );
  $this
    ->assertLogs($logs);
  $lift_api = $this
    ->getAcquiaLiftAPI(TRUE);
  try {
    $lift_api
      ->saveAutoTargetingRule($agentName, $autoFeatures);
  } catch (Exception $e) {
    $this
      ->assertTrue($e instanceof AcquiaLiftException);
  }

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::ERROR,
      'message' => "The targeting rule could not be saved for campaign {$agentName}",
    ),
  );
  $this
    ->assertLogs($logs);
}