You are here

public function RenderCacheBackendAdapterTest::setUp in Render cache 7.2

File

tests/src/Cache/RenderCacheBackendAdapterTest.php, line 35
Contains \Drupal\render_cache\Tests\Cache\RenderCacheBackendAdapterTest

Class

RenderCacheBackendAdapterTest
@coversDefaultClass \Drupal\render_cache\Cache\RenderCacheBackendAdapter @group cache

Namespace

Drupal\render_cache\Tests\Cache

Code

public function setUp() {

  // @todo We need to mock Drupal class, because RenderCache extends it.
  $drupal = Mockery::mock('alias:Drupal');
  $data_raw = array(
    'foo' => array(
      '#markup' => 'foo',
    ),
    'bar' => array(
      '#markup' => 'bar',
    ),
    'baz' => array(
      'lama' => array(
        '#markup' => 'baz',
      ),
    ),
  );
  $data = $data_raw;
  $data['#cache'] = array(
    'tags' => array(
      'bar:1',
    ),
  );
  $data['foo']['#cache'] = array(
    'tags' => array(
      'foo:1',
    ),
  );
  $data['baz']['lama']['#cache'] = array(
    'tags' => array(
      'baz:42',
      'lama:1',
    ),
  );
  $this->cacheHitData = (object) array(
    'data' => $data,
  );
  $properties = array(
    '#cache' => array(
      'tags' => array(
        'bar:1',
        'baz:42',
        'foo:1',
        'lama:1',
        'zar:1',
      ),
      'max-age' => array(
        600,
      ),
    ),
  );
  $preserved = array(
    'baz' => $data['baz'],
  );
  $this->cacheHitRendered = 'foobarbaz';
  $this->cacheHitRenderedOriginal = $this->cacheHitData->data;
  $this->cacheHitRenderedOriginal['#printed'] = TRUE;
  $this->cacheHitRenderedOriginal['foo']['#printed'] = TRUE;
  $this->cacheHitRenderedOriginal['bar']['#printed'] = TRUE;
  $this->cacheHitRenderedOriginal['baz']['#printed'] = TRUE;
  $this->cacheHitRenderedOriginal['baz']['lama']['#printed'] = TRUE;
  $this->cacheHitNoRender = $data_raw + $properties;
  $this->cacheHitLateRender = $data_raw + $properties + array(
    '#attached' => array(
      'render_cache' => $properties + $preserved,
    ),
  );
  $this->cacheHitLateRender['#cache']['cid'] = 'render:foo:late';
  $this->cacheHitRenderDirect = array(
    '#markup' => $this->cacheHitRendered,
    '#attached' => array(),
  ) + $properties + $preserved;

  // @todo This should use more mocked methods.
  $stack = Mockery::mock('\\Drupal\\render_cache\\Cache\\RenderStack[render,collectAttached]');

  // @todo Still need to implement those.
  $stack
    ->shouldReceive('render')
    ->andReturn(array(
    $this->cacheHitRendered,
    $this->cacheHitRenderedOriginal,
  ));
  $stack
    ->shouldReceive('collectAttached')
    ->andReturn($this->cacheHitLateRender['#attached']);
  $cache_bin = Mockery::mock('\\DrupalCacheInterface');

  // Cache hit
  $cache_bin
    ->shouldReceive('getMultiple')
    ->with(Mockery::on(function (&$cids) {
    $cid = reset($cids);
    if ($cid == 'render:foo:exists') {
      $cids = array();
      return TRUE;
    }
    return FALSE;
  }))
    ->andReturn(array(
    'render:foo:exists' => $this->cacheHitData,
  ));

  // Cache miss
  $cache_bin
    ->shouldReceive('getMultiple')
    ->with(Mockery::on(function (&$cids) {
    $cid = reset($cids);
    if ($cid == 'render:foo:not_exists') {
      return TRUE;
    }
    return FALSE;
  }))
    ->andReturn(array(
    // This is test only and will never happen with D7 backend.
    'render:foo:not_exists' => FALSE,
  ));
  $cache_bin
    ->shouldReceive('set')
    ->with('render:foo:bar', Mockery::on(function ($data) {
    return TRUE;
  }), 0);
  $cache_bin
    ->shouldReceive('set')
    ->with('render:foo:no', Mockery::on(function ($data) {
    return TRUE;
  }), 0);
  $cache_bin
    ->shouldReceive('set')
    ->with('render:foo:late', Mockery::on(function ($data) {
    return TRUE;
  }), 0);
  $cache_bin
    ->shouldReceive('set')
    ->with('render:foo:direct', Mockery::on(function ($data) {
    return TRUE;
  }), 0);
  $cache_bin
    ->shouldReceive('set')
    ->with('render:foo:direct', Mockery::on(function ($data) {
    return TRUE;
  }), -1);
  $cache = Mockery::mock('\\Drupal\\render_cache\\Cache\\RenderCacheBackendAdapter[cache]', array(
    $stack,
  ));
  $cache
    ->shouldReceive('cache')
    ->once()
    ->andReturn($cache_bin);
  $this->cache = $cache;
}