function RendererTestBase::setUp

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\RendererTestBase::setUp()
  2. 10 core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\RendererTestBase::setUp()
  3. 11.x core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\RendererTestBase::setUp()

Overrides UnitTestCase::setUp

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

File

core/tests/Drupal/Tests/Core/Render/RendererTestBase.php, line 118

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->createMock('Drupal\\Core\\Controller\\ControllerResolverInterface');
    $this->themeManager = $this->createMock('Drupal\\Core\\Theme\\ThemeManagerInterface');
    $this->elementInfo = $this->createMock('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->createMock('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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.