Skip to content
Snippets Groups Projects
Commit 418d710c authored by Alexey Korepov's avatar Alexey Korepov
Browse files

Implemented typed data manager stub

parent a8feefb7
No related branches found
No related tags found
No related merge requests found
......@@ -18,26 +18,21 @@ class EntityStubFactory extends UnitTestCase {
* Constructs a new EntityStubFactory.
*/
public function __construct() {
$this->fieldTypeManagerStub = new FieldTypeManagerStub();
// Reusing a string field type definition as default one.
$stringItemDefinition = UnitTestHelpers::getPluginDefinition(StringItem::class, 'Field', '\Drupal\Core\Field\Annotation\FieldType');
$this->fieldTypeManagerStub->addDefinition('string', $stringItemDefinition);
$this->fieldItemListStubFactory = new FieldItemListStubFactory($this->fieldTypeManagerStub);
$this->entitiesStorageById = [];
$this->entitiesStorageByUuid = [];
$entityTypeManagerStubFactory = new EntityTypeManagerStubFactory();
$this->entityTypeManager = $entityTypeManagerStubFactory->create();
$this->entityTypeManager = (new EntityTypeManagerStubFactory())->create();
$this->typedDataManagerStub = (new TypedDataManagerStubFactory())->createInstance();
UnitTestHelpers::addToContainer('entity.repository', $this->createMock(EntityRepositoryInterface::class));
UnitTestHelpers::addToContainer('entity_type.manager', $this->entityTypeManager);
UnitTestHelpers::addToContainer('entity_field.manager', $this->createMock(EntityFieldManagerInterface::class));
UnitTestHelpers::addToContainer('entity_type.manager', $this->entityTypeManager);
UnitTestHelpers::addToContainer('typed_data_manager', $this->typedDataManagerStub);
UnitTestHelpers::addToContainer('uuid', new PhpUuid());
// Reusing a string field type definition as default one.
$stringItemDefinition = UnitTestHelpers::getPluginDefinition(StringItem::class, 'Field', '\Drupal\Core\Field\Annotation\FieldType');
$this->fieldTypeManagerStub->addDefinition('string', $stringItemDefinition);
/** @var \Drupal\Core\Entity\EntityRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject $entityRepository */
$entityRepository = \Drupal::service('entity.repository');
$entityRepository
......@@ -74,7 +69,7 @@ class EntityStubFactory extends UnitTestCase {
* @param array $options
* The array of options:
* - methods: the list of additional methods to allow mocking of them.
* - definition: the list of custom field definitions for needed fields.
* - definitions: the list of custom field definitions for needed fields.
* If not passed - the default one (`StringItem`) will be used.
*
* @return \Drupal\Core\Entity\ContentEntityInterface|\PHPUnit\Framework\MockObject\MockObject
......@@ -109,16 +104,18 @@ class EntityStubFactory extends UnitTestCase {
// Filling values to the entity array.
$fieldItemListStubFactory = $this->fieldItemListStubFactory;
UnitTestHelpers::bindClosureToClassMethod(
function (array $values) use ($fieldItemListStubFactory) {
function (array $values) use ($fieldItemListStubFactory, $options) {
// Filling common values.
$this->translations[LanguageInterface::LANGCODE_DEFAULT] = ['status' => TRUE];
// Filling values to the entity array.
foreach ($values as $name => $value) {
$definition = $options['definitions'][$name] ?? NULL;
if (isset($options['definitions'][$name])) {
$definition = $options['definitions'][$name];
}
// @todo Convert entity to TypedDataInterface and pass to the
// item list initialization as a third argument $parent.
$field = $fieldItemListStubFactory->create($name, $value, $definition);
$field = $fieldItemListStubFactory->create($name, $value, $definition ?? NULL);
$this->fieldDefinitions[$name] = $field->getFieldDefinition();
$this->fields[$name][LanguageInterface::LANGCODE_DEFAULT] = $field;
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\test_helpers;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemList;
......@@ -40,16 +39,21 @@ class FieldItemListStubFactory extends TestCase {
* Field name.
* @param string $type
* Type of the creating field.
* @param string $class
* The class name to use for item definition.
*
* @return \Drupal\Core\Field\FieldDefinitionInterface
* A field field definition stub.
*/
private function createFieldItemDefinitionStub(string $name, string $type): FieldDefinitionInterface {
public function createFieldItemDefinitionStub(string $name, string $type, string $class = NULL): FieldDefinitionInterface {
if (!$class) {
$class = StringItem::class;
}
// @todo Now it's a quick initialization of BaseFieldDefinition,
// will be good to add support for other field types.
$field_definition = BaseFieldDefinition::create($type);
$field_definition->setName($name);
$field_definition->getItemDefinition()->setClass(StringItem::class);
$field_definition->getItemDefinition()->setClass($class);
return $field_definition;
}
......@@ -73,7 +77,7 @@ class FieldItemListStubFactory extends TestCase {
// @todo Now it's a hard-coded type, will be good to add support for
// custom types.
$type = 'string';
$definition = $this->createFieldItemDefinitionStub($name, $type);
$definition = $this->createFieldItemDefinitionStub($name, $type, StringItem::class);
}
$field = new FieldItemList($definition, $name, $parent);
$field->setValue($values);
......
......@@ -6,7 +6,6 @@ use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\TypedData\FieldItemDataDefinitionInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\entity_test\Plugin\Field\FieldType\FieldTestItem;
use Drupal\Tests\UnitTestCase;
/**
......@@ -64,8 +63,7 @@ class FieldTypeManagerStub extends UnitTestCase {
$fieldTypePluginManager
->method('createFieldItem')
->willReturnCallback(function ($items, $index, $values) {
// By default reusing the class from entity_test module.
$itemClass = FieldTestItem::class;
$itemClass = $items->getFieldDefinition()->getItemDefinition()->getClass();
foreach ($this->fieldItemClassByListClassMap as $listClass => $itemClassCandidate) {
if ($items instanceof $listClass) {
$itemClass = $itemClassCandidate;
......
<?php
namespace Drupal\test_helpers;
use Drupal\Core\TypedData\Plugin\DataType\StringData;
use Drupal\Core\TypedData\TypedDataManager;
use Drupal\Tests\UnitTestCase;
/**
* The Entity Storage Stub class.
*/
class TypedDataManagerStubFactory extends UnitTestCase {
/**
* Creates an entity type stub and defines a static storage for it.
*/
public function createInstance() {
/** @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit\Framework\MockObject\MockObject $instance */
$instance = $this->createPartialMock(TypedDataManager::class, ['getDefinition']);
UnitTestHelpers::bindClosureToClassMethod(
function ($plugin_id, $exception_on_invalid = TRUE) {
// @todo Add support for other plugins.
if ($plugin_id == 'string') {
$definition = UnitTestHelpers::getPluginDefinition(StringData::class, 'TypedData');
return $definition;
}
return NULL;
},
$instance,
'getDefinition'
);
return $instance;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment