You are here

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

Covers $factory->get().

File

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

Class

AuthcacheP13nTestObjectFactoryCase
Tests for object factory.

Code

public function testFactorySimpleGet() {
  $resources = array();
  $factory = new AuthcacheP13nObjectFactory($resources);
  try {
    $result = $factory
      ->get('missing ressource');
    $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(
    'fourty two' => array(
      '#type' => 'value',
      '#value' => 42,
    ),
    'dummy instance' => array(
      '#type' => 'class',
      '#class' => 'AuthcacheP13nTestObjectFactoryDummyClass',
      '#arguments' => array(
        '@fourty two',
        '@strlen of four',
      ),
    ),
    'strlen of four' => array(
      '#type' => 'func',
      '#func' => 'strlen',
      '#arguments' => array(
        'four',
      ),
    ),
    'nested dummy' => array(
      '#type' => 'class',
      '#class' => 'AuthcacheP13nTestObjectFactoryDummyClass',
      '#arguments' => array(
        '@dummy instance',
      ),
    ),
  );
  $factory = new AuthcacheP13nObjectFactory($resources);
  $result = $factory
    ->get('fourty two');
  $this
    ->assertIdentical(42, $result, t('Should return value for value-resources'));
  $factory = new AuthcacheP13nObjectFactory($resources);
  $expect_instance = new AuthcacheP13nTestObjectFactoryDummyClass(42, 4);
  $result = $factory
    ->get('dummy instance');
  $this
    ->assertEqual($expect_instance, $result, t('Should return instance for class-resources'));
  $second_result = $factory
    ->get('dummy instance');
  $this
    ->assertIdentical($second_result, $result, t('Should return identical instance on subsequent calls'));
  $factory = new AuthcacheP13nObjectFactory($resources);
  $result = $factory
    ->get('strlen of four');
  $this
    ->assertIdentical(4, $result, t('Should return result of calling the function on func-resources'));
  $factory = new AuthcacheP13nObjectFactory($resources);
  $expect_inner = new AuthcacheP13nTestObjectFactoryDummyClass(42, 4);
  $expect_instance = new AuthcacheP13nTestObjectFactoryDummyClass($expect_inner);
  $result = $factory
    ->get('nested dummy');
  $this
    ->assertEqual($expect_instance, $result, t('Should resolve arguments of resource definitions before creating instances'));
}