function FieldDefinitionTest::setUp

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php \Drupal\Tests\Core\Entity\FieldDefinitionTest::setUp()
  2. 10 core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php \Drupal\Tests\Core\Entity\FieldDefinitionTest::setUp()
  3. 11.x core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php \Drupal\Tests\Core\Entity\FieldDefinitionTest::setUp()

Overrides UnitTestCase::setUp

File

core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php, line 53

Class

FieldDefinitionTest
Unit test for the FieldDefinition class.

Namespace

Drupal\Tests\Core\Entity

Code

protected function setUp() : void {
    $this->fieldType = $this->randomMachineName();
    $this->fieldTypeDefinition = [
        'id' => $this->fieldType,
        'field_settings' => [
            'some_instance_setting' => 'value 2',
        ],
        'storage_settings' => [
            'some_storage_setting' => 'some value',
        ],
    ];
    $field_type_manager = $this->prophesize(FieldTypePluginManagerInterface::class);
    $field_type_manager->getDefinitions()
        ->willReturn([
        $this->fieldType => $this->fieldTypeDefinition,
    ]);
    $field_type_manager->getDefinition()
        ->willReturn($this->fieldTypeDefinition);
    $field_type_manager->getDefaultFieldSettings($this->fieldType)
        ->willReturn($this->fieldTypeDefinition['field_settings']);
    $field_type_manager->getDefaultStorageSettings($this->fieldType)
        ->willReturn($this->fieldTypeDefinition['storage_settings']);
    $storage_definition = $this->prophesize(FieldStorageDefinitionInterface::class);
    $storage_definition->getMainPropertyName()
        ->willReturn('value');
    $storage_definition->getType()
        ->willReturn($this->fieldType);
    $storage_definition->getName()
        ->willReturn('test_field_name');
    $storage_supports_translation =& $this->storageSupportsTranslation;
    $storage_definition->isTranslatable()
        ->will(function () use (&$storage_supports_translation) {
        return $storage_supports_translation;
    });
    $storage_definition->getSettings()
        ->willReturn($this->fieldTypeDefinition['storage_settings']);
    $storage_definition->getSetting('some_storage_setting')
        ->willReturn($this->fieldTypeDefinition['storage_settings']['some_storage_setting']);
    $this->storageDefinition = $storage_definition->reveal();
    $entity_field_manager = $this->prophesize(EntityFieldManagerInterface::class);
    $entity_field_manager->getFieldStorageDefinitions('entity_test')
        ->willReturn([
        'foo' => $storage_definition->reveal(),
    ]);
    $typed_data_manager = $this->prophesize(TypedDataManager::class);
    $container = new ContainerBuilder();
    $container->set('plugin.manager.field.field_type', $field_type_manager->reveal());
    $container->set('entity_field.manager', $entity_field_manager->reveal());
    $container->set('typed_data_manager', $typed_data_manager->reveal());
    \Drupal::setContainer($container);
}

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