function ViewAjaxControllerTest::setUp

Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Unit/Controller/ViewAjaxControllerTest.php \Drupal\Tests\views\Unit\Controller\ViewAjaxControllerTest::setUp()
  2. 10 core/modules/views/tests/src/Unit/Controller/ViewAjaxControllerTest.php \Drupal\Tests\views\Unit\Controller\ViewAjaxControllerTest::setUp()
  3. 9 core/modules/views/tests/src/Unit/Controller/ViewAjaxControllerTest.php \Drupal\Tests\views\Unit\Controller\ViewAjaxControllerTest::setUp()
  4. 8.9.x core/modules/views/tests/src/Unit/Controller/ViewAjaxControllerTest.php \Drupal\Tests\views\Unit\Controller\ViewAjaxControllerTest::setUp()

Overrides UnitTestCase::setUp

File

core/modules/views/tests/src/Unit/Controller/ViewAjaxControllerTest.php, line 98

Class

ViewAjaxControllerTest
Tests Drupal\views\Controller\ViewAjaxController.

Namespace

Drupal\Tests\views\Unit\Controller

Code

protected function setUp() : void {
  parent::setUp();
  $this->viewStorage = $this->createStub(EntityStorageInterface::class);
  $this->executableFactory = $this->createStub(ViewExecutableFactory::class);
  $this->renderer = $this->createStub(RendererInterface::class);
  $this->renderer
    ->method('renderRoot')
    ->willReturnCallback(function (array &$elements) {
    $elements['#attached'] = [];
    return $elements['#markup'] ?? '';
  });
  $this->renderer
    ->method('executeInRenderContext')
    ->willReturnCallback(function (RenderContext $context, callable $callable) {
    return $callable();
  });
  $this->currentPath = $this->createStub(CurrentPathStack::class);
  $this->redirectDestination = $this->createStub(RedirectDestinationInterface::class);
  $this->pathValidator = $this->createStub(PathValidatorInterface::class);
  $this->pathValidator
    ->method('getUrlIfValid')
    ->willReturnCallback(function ($path) {
    if ($path === '/invalid-test-page') {
      return FALSE;
    }
    else {
      return Url::fromUserInput('/foo');
    }
  });
  $unroutedUrlAssembler = $this->createStub(UnroutedUrlAssemblerInterface::class);
  $unroutedUrlAssembler->method('assemble')
    ->willReturn('/foo');
  $this->viewAjaxController = new ViewAjaxController($this->viewStorage, $this->executableFactory, $this->renderer, $this->currentPath, $this->redirectDestination, $this->pathValidator);
  $element_info_manager = $this->createMock(ElementInfoManagerInterface::class);
  $element_info_manager->method('getInfo')
    ->with('status_messages')
    ->willReturn([]);
  $request_stack = new RequestStack();
  $request_stack->push(new Request());
  $this->renderer = new Renderer($this->createStub(CallableResolver::class), $this->createStub(ThemeManagerInterface::class), $element_info_manager, $this->createStub(PlaceholderGeneratorInterface::class), $this->createStub(RenderCacheInterface::class), $request_stack, [
    'required_cache_contexts' => [
      'languages:language_interface',
      'theme',
    ],
  ]);
  $container = new ContainerBuilder();
  $container->set('renderer', $this->renderer);
  $container->set('path.validator', $this->pathValidator);
  $container->set('unrouted_url_assembler', $unroutedUrlAssembler);
  \Drupal::setContainer($container);
  // Not initialized in Unit tests.
  $GLOBALS['base_path'] = '/';
}

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