You are here

protected function FieldConfigEntityUnitTest::setUp in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Unit/FieldConfigEntityUnitTest.php \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest::setUp()

Overrides UnitTestCase::setUp

File

core/modules/field/tests/src/Unit/FieldConfigEntityUnitTest.php, line 76
Contains \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest.

Class

FieldConfigEntityUnitTest
@coversDefaultClass \Drupal\field\Entity\FieldConfig @group field

Namespace

Drupal\Tests\field\Unit

Code

protected function setUp() : void {
  $this->entityTypeId = $this
    ->randomMachineName();
  $this->entityType = $this
    ->createMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
  $this->entityTypeManager = $this
    ->createMock(EntityTypeManagerInterface::class);
  $this->entityFieldManager = $this
    ->createMock(EntityFieldManagerInterface::class);
  $this->uuid = $this
    ->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
  $this->fieldTypePluginManager = $this
    ->createMock('Drupal\\Core\\Field\\FieldTypePluginManagerInterface');
  $container = new ContainerBuilder();
  $container
    ->set('entity_field.manager', $this->entityFieldManager);
  $container
    ->set('entity_type.manager', $this->entityTypeManager);
  $container
    ->set('uuid', $this->uuid);
  $container
    ->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
  \Drupal::setContainer($container);

  // Create a mock FieldStorageConfig object.
  $this->fieldStorage = $this
    ->createMock('\\Drupal\\field\\FieldStorageConfigInterface');
  $this->fieldStorage
    ->expects($this
    ->any())
    ->method('getType')
    ->will($this
    ->returnValue('test_field'));
  $this->fieldStorage
    ->expects($this
    ->any())
    ->method('getName')
    ->will($this
    ->returnValue('field_test'));
  $this->fieldStorage
    ->expects($this
    ->any())
    ->method('getSettings')
    ->willReturn([]);

  // Place the field in the mocked entity field manager's field registry.
  $this->entityFieldManager
    ->expects($this
    ->any())
    ->method('getFieldStorageDefinitions')
    ->with('test_entity_type')
    ->will($this
    ->returnValue([
    $this->fieldStorage
      ->getName() => $this->fieldStorage,
  ]));
}