protected function EntityFieldExportTest::setUp in REST Views 8
Same name and namespace in other branches
- 2.0.x tests/src/Unit/EntityFieldExportTest.php \Drupal\Tests\rest_views\Unit\EntityFieldExportTest::setUp()
Overrides UnitTestCase::setUp
File
- tests/
src/ Unit/ EntityFieldExportTest.php, line 50
Class
- EntityFieldExportTest
- Test the EntityFieldExport views field plugin.
Namespace
Drupal\Tests\rest_views\UnitCode
protected function setUp() {
parent::setUp();
// Create our field handler, mocking the required services.
$entityManager = $this
->getMock(EntityManagerInterface::class);
$formatterPluginManager = $this
->getMockBuilder(FormatterPluginManager::class)
->disableOriginalConstructor()
->getMock();
$fieldTypePluginManager = $this
->getMock(FieldTypePluginManagerInterface::class);
$languageManager = $this
->getMock(LanguageManagerInterface::class);
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this
->getMock(RendererInterface::class);
// For the t() function to work, mock the translation service.
$container = new ContainerBuilder();
$translation = $this
->getMock(TranslationInterface::class);
$translation
->method('translateString')
->willReturnCallback(static function (TranslatableMarkup $string) {
return $string
->getUntranslatedString();
});
$container
->set('string_translation', $translation);
$container
->set('entity.repository', $entityManager);
$container
->set('entity_field.manager', $entityManager);
Drupal::setContainer($container);
$this->handler = $this
->getMockBuilder(EntityFieldExport::class)
->setConstructorArgs([
[],
NULL,
[
'entity_type' => 'node',
'field_name' => 'title',
],
$entityManager,
$formatterPluginManager,
$fieldTypePluginManager,
$languageManager,
$renderer,
])
->setMethods([
'getFieldDefinition',
])
->getMock();
// Mock the field definition.
$fieldDefinition = $this
->getMock(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);
$entityManager
->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(),
]);
}