function ContentEntityBaseUnitTest::commonSetUp
Sets up the entity under test.
Parameters
int $getCreateFieldItemListCallsCount: The number of expected calls to the FieldTypePluginManager::createFieldItemList() method.
int $getFieldDefinitionsCallsCount: The number of expected calls to the EntityFieldManagerInterface::getFieldDefinitions() method.
File
-
core/
tests/ Drupal/ Tests/ Core/ Entity/ ContentEntityBaseUnitTest.php, line 125
Class
Namespace
Drupal\Tests\Core\EntityCode
protected function commonSetUp(int $getCreateFieldItemListCallsCount, int $getFieldDefinitionsCallsCount) : void {
$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->createStub(EntityTypeInterface::class);
$this->entityType
->method('getKeys')
->willReturn([
'id' => 'id',
'uuid' => 'uuid',
]);
$this->entityType
->method('getKey')
->willReturnMap([
[
'default_langcode',
'default_langcode',
],
[
'id',
'id',
],
[
'langcode',
'langcode',
],
[
'revision',
'revision_id',
],
[
'uuid',
'uuid',
],
]);
$this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
$this->entityTypeManager
->expects($this->atLeastOnce())
->method('getDefinition')
->with($this->entityTypeId)
->willReturn($this->entityType);
$this->entityFieldManager = $this->createMock(EntityFieldManagerInterface::class);
$this->entityTypeBundleInfo = $this->createStub(EntityTypeBundleInfoInterface::class);
$this->uuid = $this->createStub(UuidInterface::class);
$this->typedDataManager = $this->createStub(TypedDataManagerInterface::class);
$this->typedDataManager
->method('getDefinition')
->willReturn([
'class' => '\\Drupal\\Core\\Entity\\Plugin\\DataType\\EntityAdapter',
]);
$this->languageManager = $this->createStub('\\Drupal\\Core\\Language\\LanguageManagerInterface');
$this->fieldTypePluginManager = $this->getMockBuilder('\\Drupal\\Core\\Field\\FieldTypePluginManager')
->disableOriginalConstructor()
->getMock();
$this->fieldTypePluginManager
->expects($this->atLeastOnce())
->method('getDefaultStorageSettings')
->willReturn([]);
$this->fieldTypePluginManager
->expects($this->atLeastOnce())
->method('getDefaultFieldSettings')
->willReturn([]);
$this->fieldTypePluginManager
->expects($this->exactly($getCreateFieldItemListCallsCount))
->method('createFieldItemList')
->willReturn($this->createStub(FieldItemListInterface::class));
$container = new ContainerBuilder();
$container->set('entity_field.manager', $this->entityFieldManager);
$container->set('entity_type.bundle.info', $this->entityTypeBundleInfo);
$container->set('entity_type.manager', $this->entityTypeManager);
$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->exactly($getFieldDefinitionsCallsCount))
->method('getFieldDefinitions')
->with($this->entityTypeId, $this->bundle)
->willReturn($this->fieldDefinitions);
$this->entity = new ContentEntityBaseMockableClass($values, $this->entityTypeId, $this->bundle);
$values['defaultLangcode'] = [
LanguageInterface::LANGCODE_DEFAULT => LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
$this->entityUnd = new ContentEntityBaseMockableClass($values, $this->entityTypeId, $this->bundle);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.