function EntityAdapterUnitTest::setUp

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php \Drupal\Tests\Core\Entity\TypedData\EntityAdapterUnitTest::setUp()
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php \Drupal\Tests\Core\Entity\TypedData\EntityAdapterUnitTest::setUp()
  3. 10 core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php \Drupal\Tests\Core\Entity\TypedData\EntityAdapterUnitTest::setUp()

Overrides UnitTestCase::setUp

File

core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php, line 125

Class

EntityAdapterUnitTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Plugin%21DataType%21EntityAdapter.php/class/EntityAdapter/11.x" title="Defines the &quot;entity&quot; data type." class="local">\Drupal\Core\Entity\Plugin\DataType\EntityAdapter</a> @group Entity @group TypedData

Namespace

Drupal\Tests\Core\Entity\TypedData

Code

protected function setUp() : void {
    parent::setUp();
    $this->id = 1;
    $values = [
        'id' => $this->id,
        'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a',
        'defaultLangcode' => [
            LanguageInterface::LANGCODE_DEFAULT => 'en',
        ],
    ];
    $this->entityTypeId = $this->randomMachineName();
    $this->bundle = $this->randomMachineName();
    $this->entityType = $this->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $this->entityType
        ->expects($this->any())
        ->method('getKeys')
        ->willReturn([
        'id' => 'id',
        'uuid' => 'uuid',
    ]);
    $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
    $this->entityTypeManager
        ->expects($this->any())
        ->method('getDefinition')
        ->with($this->entityTypeId)
        ->willReturn($this->entityType);
    $this->uuid = $this->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
    $this->typedDataManager = $this->createMock(TypedDataManagerInterface::class);
    $this->typedDataManager
        ->expects($this->any())
        ->method('getDefinition')
        ->with('entity')
        ->willReturn([
        'class' => '\\Drupal\\Core\\Entity\\Plugin\\DataType\\EntityAdapter',
    ]);
    $this->typedDataManager
        ->expects($this->any())
        ->method('getDefaultConstraints')
        ->willReturn([]);
    $validation_constraint_manager = $this->getMockBuilder('\\Drupal\\Core\\Validation\\ConstraintManager')
        ->disableOriginalConstructor()
        ->getMock();
    $validation_constraint_manager->expects($this->any())
        ->method('create')
        ->willReturn([]);
    $this->typedDataManager
        ->expects($this->any())
        ->method('getValidationConstraintManager')
        ->willReturn($validation_constraint_manager);
    $not_specified = new Language([
        'id' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'locked' => TRUE,
    ]);
    $this->languageManager = $this->createMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
    $this->languageManager
        ->expects($this->any())
        ->method('getLanguages')
        ->willReturn([
        LanguageInterface::LANGCODE_NOT_SPECIFIED => $not_specified,
    ]);
    $this->languageManager
        ->expects($this->any())
        ->method('getLanguage')
        ->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
        ->willReturn($not_specified);
    $this->fieldTypePluginManager = $this->getMockBuilder('\\Drupal\\Core\\Field\\FieldTypePluginManager')
        ->disableOriginalConstructor()
        ->getMock();
    $this->fieldTypePluginManager
        ->expects($this->any())
        ->method('getDefaultStorageSettings')
        ->willReturn([]);
    $this->fieldTypePluginManager
        ->expects($this->any())
        ->method('getDefaultFieldSettings')
        ->willReturn([]);
    $this->fieldItemList = $this->createMock('\\Drupal\\Core\\Field\\FieldItemListInterface');
    $this->fieldTypePluginManager
        ->expects($this->any())
        ->method('createFieldItemList')
        ->willReturn($this->fieldItemList);
    $this->entityFieldManager = $this->createMock(EntityFieldManagerInterface::class);
    $container = new ContainerBuilder();
    $container->set('entity_type.manager', $this->entityTypeManager);
    $container->set('entity_field.manager', $this->entityFieldManager);
    $container->set('uuid', $this->uuid);
    $container->set('typed_data_manager', $this->typedDataManager);
    $container->set('language_manager', $this->languageManager);
    $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
    \Drupal::setContainer($container);
    $this->fieldDefinitions = [
        'id' => BaseFieldDefinition::create('integer'),
        'revision_id' => BaseFieldDefinition::create('integer'),
    ];
    $this->entityFieldManager
        ->expects($this->any())
        ->method('getFieldDefinitions')
        ->with($this->entityTypeId, $this->bundle)
        ->willReturn($this->fieldDefinitions);
    $this->entity = $this->getMockBuilder(ContentEntityBaseMockableClass::class)
        ->setConstructorArgs([
        $values,
        $this->entityTypeId,
        $this->bundle,
    ])
        ->onlyMethods([])
        ->getMock();
    $this->entityAdapter = EntityAdapter::createFromEntity($this->entity);
}

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