You are here

protected function RouteSubscriberTest::setupMocks in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Unit/EventSubscriber/RouteSubscriberTest.php \Drupal\Tests\views\Unit\EventSubscriber\RouteSubscriberTest::setupMocks()

Sets up mocks of Views objects needed for testing.

Return value

\Drupal\views\Plugin\views\display\DisplayRouterInterface[]|\PHPUnit\Framework\MockObject\MockObject[] An array of two mocked view displays.

2 calls to RouteSubscriberTest::setupMocks()
RouteSubscriberTest::testOnAlterRoutes in core/modules/views/tests/src/Unit/EventSubscriber/RouteSubscriberTest.php
Tests the onAlterRoutes method.
RouteSubscriberTest::testRouteRebuildFinished in core/modules/views/tests/src/Unit/EventSubscriber/RouteSubscriberTest.php
@covers ::routeRebuildFinished

File

core/modules/views/tests/src/Unit/EventSubscriber/RouteSubscriberTest.php, line 146
Contains \Drupal\Tests\views\Unit\EventSubscriber\RouteSubscriberTest.

Class

RouteSubscriberTest
@coversDefaultClass \Drupal\views\EventSubscriber\RouteSubscriber @group views

Namespace

Drupal\Tests\views\Unit\EventSubscriber

Code

protected function setupMocks() {
  $executable = $this
    ->getMockBuilder('Drupal\\views\\ViewExecutable')
    ->disableOriginalConstructor()
    ->getMock();
  $view = $this
    ->getMockBuilder('Drupal\\views\\Entity\\View')
    ->disableOriginalConstructor()
    ->getMock();
  $this->viewStorage
    ->expects($this
    ->any())
    ->method('load')
    ->will($this
    ->returnValue($view));
  $view
    ->expects($this
    ->any())
    ->method('getExecutable')
    ->will($this
    ->returnValue($executable));
  $view
    ->expects($this
    ->any())
    ->method('id')
    ->will($this
    ->returnValue('test_id'));
  $executable->storage = $view;
  $executable
    ->expects($this
    ->any())
    ->method('setDisplay')
    ->willReturnMap([
    [
      'page_1',
      TRUE,
    ],
    [
      'page_2',
      TRUE,
    ],
    [
      'page_3',
      FALSE,
    ],
  ]);

  // Ensure that only the first two displays are actually called.
  $display_1 = $this
    ->createMock('Drupal\\views\\Plugin\\views\\display\\DisplayRouterInterface');
  $display_2 = $this
    ->createMock('Drupal\\views\\Plugin\\views\\display\\DisplayRouterInterface');
  $display_collection = $this
    ->getMockBuilder('Drupal\\views\\DisplayPluginCollection')
    ->disableOriginalConstructor()
    ->getMock();
  $display_collection
    ->expects($this
    ->any())
    ->method('get')
    ->willReturnMap([
    [
      'page_1',
      $display_1,
    ],
    [
      'page_2',
      $display_2,
    ],
  ]);
  $executable->displayHandlers = $display_collection;
  $this->routeSubscriber->applicableViews = [];
  $this->routeSubscriber->applicableViews[] = [
    'test_id',
    'page_1',
  ];
  $this->routeSubscriber->applicableViews[] = [
    'test_id',
    'page_2',
  ];
  $this->routeSubscriber->applicableViews[] = [
    'test_id',
    'page_3',
  ];
  return [
    $display_1,
    $display_2,
  ];
}