You are here

protected function RendererTestBase::setUp in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\RendererTestBase::setUp()

Overrides UnitTestCase::setUp

2 calls to RendererTestBase::setUp()
RendererBubblingTest::setUp in core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php
RendererPlaceholdersTest::setUp in core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php
2 methods override RendererTestBase::setUp()
RendererBubblingTest::setUp in core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php
RendererPlaceholdersTest::setUp in core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php

File

core/tests/Drupal/Tests/Core/Render/RendererTestBase.php, line 117
Contains \Drupal\Tests\Core\Render\RendererTestBase.

Class

RendererTestBase
Base class for the actual unit tests testing \Drupal\Core\Render\Renderer.

Namespace

Drupal\Tests\Core\Render

Code

protected function setUp() {
  parent::setUp();
  $this->controllerResolver = $this
    ->getMock('Drupal\\Core\\Controller\\ControllerResolverInterface');
  $this->themeManager = $this
    ->getMock('Drupal\\Core\\Theme\\ThemeManagerInterface');
  $this->elementInfo = $this
    ->getMock('Drupal\\Core\\Render\\ElementInfoManagerInterface');
  $this->elementInfo
    ->expects($this
    ->any())
    ->method('getInfo')
    ->willReturnCallback(function ($type) {
    switch ($type) {
      case 'details':
        $info = [
          '#theme_wrappers' => [
            'details',
          ],
        ];
        break;
      case 'link':
        $info = [
          '#theme' => 'link',
        ];
        break;
      default:
        $info = [];
    }
    $info['#defaults_loaded'] = TRUE;
    return $info;
  });
  $this->requestStack = new RequestStack();
  $request = new Request();
  $request->server
    ->set('REQUEST_TIME', $_SERVER['REQUEST_TIME']);
  $this->requestStack
    ->push($request);
  $this->cacheFactory = $this
    ->getMock('Drupal\\Core\\Cache\\CacheFactoryInterface');
  $this->cacheContextsManager = $this
    ->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
    ->disableOriginalConstructor()
    ->getMock();
  $this->cacheContextsManager
    ->method('assertValidTokens')
    ->willReturn(TRUE);
  $current_user_role =& $this->currentUserRole;
  $this->cacheContextsManager
    ->expects($this
    ->any())
    ->method('convertTokensToKeys')
    ->willReturnCallback(function ($context_tokens) use (&$current_user_role) {
    $keys = [];
    foreach ($context_tokens as $context_id) {
      switch ($context_id) {
        case 'user.roles':
          $keys[] = 'r.' . $current_user_role;
          break;
        case 'languages:language_interface':
          $keys[] = 'en';
          break;
        case 'theme':
          $keys[] = 'stark';
          break;
        default:
          $keys[] = $context_id;
      }
    }
    return new ContextCacheKeys($keys, new CacheableMetadata());
  });
  $this->placeholderGenerator = new PlaceholderGenerator($this->rendererConfig);
  $this->renderCache = new PlaceholderingRenderCache($this->requestStack, $this->cacheFactory, $this->cacheContextsManager, $this->placeholderGenerator);
  $this->renderer = new Renderer($this->controllerResolver, $this->themeManager, $this->elementInfo, $this->placeholderGenerator, $this->renderCache, $this->requestStack, $this->rendererConfig);
  $container = new ContainerBuilder();
  $container
    ->set('cache_contexts_manager', $this->cacheContextsManager);
  $container
    ->set('render_cache', $this->renderCache);
  $container
    ->set('renderer', $this->renderer);
  \Drupal::setContainer($container);
}