You are here

public function AuthcacheP13nTestObjectFactoryCase::testFactoryResolve in Authenticated User Page Caching (Authcache) 7.2

Covers AuthcacheP13nObjectFactory::resolveReferences().

File

modules/authcache_p13n/tests/authcache_p13n.object-factory.test, line 76
Define test cases for object factory.

Class

AuthcacheP13nTestObjectFactoryCase
Tests for object factory.

Code

public function testFactoryResolve() {
  $resources = array();
  $factory = new AuthcacheP13nObjectFactory($resources);
  $result = $factory
    ->resolveReferences(42);
  $this
    ->assertEqual(42, $result, t('Should return the same value when there are no references'));
  $result = $factory
    ->resolveReferences(3.14);
  $this
    ->assertEqual(3.14, $result, t('Should return the same value when there are no references'));
  $result = $factory
    ->resolveReferences(TRUE);
  $this
    ->assertEqual(TRUE, $result, t('Should return the same value when there are no references'));
  $result = $factory
    ->resolveReferences('a quick brown fox');
  $this
    ->assertEqual('a quick brown fox', $result, t('Should return the same value when there are no references'));
  $result = $factory
    ->resolveReferences(array());
  $this
    ->assertEqual(array(), $result, t('Should return the same value when there are no references'));
  $result = $factory
    ->resolveReferences((object) array());
  $this
    ->assertEqual((object) array(), $result, t('Should return the same value when there are no references'));
  $result = $factory
    ->resolveReferences('@@escaped-at');
  $this
    ->assertEqual('@escaped-at', $result, t('Should unescape a string beginning with a double-at'));
  $result = $factory
    ->resolveReferences(array(
    'nested' => '@@resources',
  ));
  $this
    ->assertEqual(array(
    'nested' => '@resources',
  ), $result, t('Should unescape a string beginning with a double-at nested within an array'));
  try {
    $result = $factory
      ->resolveReferences('@missing-resource');
    $this
      ->fail(t('Should throw an exception when resource is missing'));
  } catch (AuthcacheP13nObjectFactoryException $e) {
    unset($e);
    $this
      ->pass(t('Should throw an exception when resource is missing'));
  }
  $resources = array(
    'a value' => array(
      '#type' => 'value',
      '#value' => 42,
    ),
    'a class' => array(
      '#type' => 'class',
      '#class' => 'AuthcacheP13nTestObjectFactoryDummyClass',
    ),
  );
  $factory = new AuthcacheP13nObjectFactory($resources);
  $result = $factory
    ->resolveReferences('@a value');
  $this
    ->assertIdentical(42, $result, t('Should return proper ressource'));
  $expect_instance = new AuthcacheP13nTestObjectFactoryDummyClass();
  $result = $factory
    ->resolveReferences('@a class');
  $this
    ->assertEqual($expect_instance, $result, t('Should return proper ressource'));
  $array_value = array(
    'a plain value' => 'unchanged',
    'a value reference' => '@a value',
    'nested' => array(
      'object reference' => '@a class',
    ),
  );
  $expect_value = array(
    'a plain value' => 'unchanged',
    'a value reference' => 42,
    'nested' => array(
      'object reference' => $expect_instance,
    ),
  );
  $result = $factory
    ->resolveReferences($array_value);
  $this
    ->assertEqual($expect_value, $result, t('Should resolve ressource referencese nested within an array.'));
  $processors = array(
    'process' => '_authcache_p13n_object_factory_test_processor',
  );
  $factory = new AuthcacheP13nObjectFactory($resources, $processors);
  $expect_result = array(
    42,
    NULL,
    'a value',
    $factory,
  );
  $result = $factory
    ->resolveReferences('@a value[process]');
  $this
    ->assertEqual($expect_result, $result, t('Should execute processor'));
  $result = $factory
    ->resolveReferences('@a value[process()]');
  $this
    ->assertEqual($expect_result, $result, t('Should execute processor with empty argument list'));
  $expect_result = array(
    42,
    'arg',
    'a value',
    $factory,
  );
  $result = $factory
    ->resolveReferences('@a value[process(arg)]');
  $this
    ->assertEqual($expect_result, $result, t('Should execute processor with argument'));

  // Missing class.
  $resources = array(
    'missing class' => array(
      '#type' => 'class',
      '#class' => $this
        ->randomName(8),
    ),
  );
  $factory = new AuthcacheP13nObjectFactory($resources);
  try {
    $factory
      ->resolveReferences('@missing class');
    $this
      ->fail(t('Should throw an exception when trying to instantiate a non-existing class'));
  } catch (AuthcacheP13nObjectFactoryException $e) {
    $this
      ->pass(t('Should throw an exception when trying to instantiate a non-existing class'));
  }
}