You are here

protected function EntityFieldExportTest::setUp in REST Views 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Unit/EntityFieldExportTest.php \Drupal\Tests\rest_views\Unit\EntityFieldExportTest::setUp()

Overrides UnitTestCase::setUp

File

tests/src/Unit/EntityFieldExportTest.php, line 51

Class

EntityFieldExportTest
Test the EntityFieldExport views field plugin.

Namespace

Drupal\Tests\rest_views\Unit

Code

protected function setUp() : void {
  parent::setUp();

  // Create our field handler, mocking the required services.
  $entityTypeManager = $this
    ->createMock(EntityTypeManagerInterface::class);
  $entityFieldManager = $this
    ->createMock(EntityFieldManagerInterface::class);
  $entityRepository = $this
    ->createMock(EntityRepositoryInterface::class);
  $formatterPluginManager = $this
    ->getMockBuilder(FormatterPluginManager::class)
    ->disableOriginalConstructor()
    ->getMock();
  $fieldTypePluginManager = $this
    ->createMock(FieldTypePluginManagerInterface::class);
  $languageManager = $this
    ->createMock(LanguageManagerInterface::class);

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this
    ->createMock(RendererInterface::class);

  // For the t() function to work, mock the translation service.
  $container = new ContainerBuilder();
  $translation = $this
    ->createMock(TranslationInterface::class);
  $translation
    ->method('translateString')
    ->willReturnCallback(static function (TranslatableMarkup $string) {
    return $string
      ->getUntranslatedString();
  });
  $container
    ->set('string_translation', $translation);
  \Drupal::setContainer($container);
  $this->handler = $this
    ->getMockBuilder(EntityFieldExport::class)
    ->setConstructorArgs([
    [],
    NULL,
    [
      'entity_type' => 'node',
      'field_name' => 'title',
    ],
    $entityTypeManager,
    $formatterPluginManager,
    $fieldTypePluginManager,
    $languageManager,
    $renderer,
    $entityRepository,
    $entityFieldManager,
  ])
    ->setMethods([
    'getFieldDefinition',
  ])
    ->getMock();

  // Mock the field definition.
  $fieldDefinition = $this
    ->createMock(BaseFieldDefinition::class);
  $fieldDefinition
    ->method('getFieldStorageDefinition')
    ->willReturn($fieldDefinition);
  $fieldDefinition
    ->method('getColumns')
    ->willReturn([]);

  // The handler accesses it through itself, and through the entity manager.
  $this->handler
    ->method('getFieldDefinition')
    ->willReturn($fieldDefinition);
  $entityFieldManager
    ->method('getFieldStorageDefinitions')
    ->with('node')
    ->willReturn([
    'title' => $fieldDefinition,
  ]);

  // Initialize the handler, using a mocked view and display plugin.

  /** @var \Drupal\views\ViewExecutable $view */
  $view = $this
    ->getMockBuilder(ViewExecutable::class)
    ->disableOriginalConstructor()
    ->getMock();
  $view->display_handler = $this
    ->getMockBuilder(DisplayPluginBase::class)
    ->disableOriginalConstructor()
    ->getMock();
  $this->handler
    ->init($view, $view->display_handler);
  $this->serializer = new Serializer([
    new DataNormalizer(),
    new RenderNormalizer($renderer),
  ], [
    new JsonEncoder(),
  ]);
}