You are here

public function ViewEventTest::testPostRenderEvent in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/views_event_dispatcher/tests/src/Unit/Views/ViewEventTest.php \Drupal\Tests\views_event_dispatcher\Unit\Views\ViewEventTest::testPostRenderEvent()

Post render event.

File

modules/views_event_dispatcher/tests/src/Unit/Views/ViewEventTest.php, line 150

Class

ViewEventTest
Class ViewEventTest.

Namespace

Drupal\Tests\views_event_dispatcher\Unit\Views

Code

public function testPostRenderEvent() : void {

  /** @var \Drupal\views\ViewExecutable $view */
  $view = $this
    ->createMock(ViewExecutable::class);
  $output = $expectedOutput = [
    '#theme' => [
      'views_view',
    ],
    '#attached' => [
      'library' => [
        'views/views.module',
      ],
    ],
  ];

  /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
  $cache = $this
    ->createMock(CachePluginBase::class);
  $cache->options['results_lifespan'] = 0;
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::VIEWS_POST_RENDER => static function (ViewsPostRenderEvent $event) {
      $output =& $event
        ->getOutput();
      $output['#attached']['library'][] = 'test';
      $cache = $event
        ->getCache();
      $cache->options['results_lifespan'] = 10;
    },
  ]);
  views_event_dispatcher_views_post_render($view, $output, $cache);
  $expectedOutput['#attached']['library'][] = 'test';

  /** @var \Drupal\views_event_dispatcher\Event\Views\ViewsPostRenderEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_POST_RENDER);
  self::assertSame($expectedOutput, $event
    ->getOutput());
  self::assertSame($expectedOutput, $output);
  self::assertSame($cache, $event
    ->getCache());
  self::assertSame(10, $cache->options['results_lifespan']);
}