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

Merge branch '1.0.x' of git.drupal.org:project/test_helpers into 1.0.x

parents c3467a23 03ed87ba
No related branches found
Tags 1.0.0-alpha2
No related merge requests found
......@@ -27,6 +27,7 @@ class EntityStorageStubFactory extends UnitTestCase {
'loadMultiple',
'loadByProperties',
'delete',
'invokeHook',
// Custom helper functions for the stub:
// Attaches an entity type object to the stub.
......@@ -48,10 +49,17 @@ class EntityStorageStubFactory extends UnitTestCase {
// we've got an error "Indirect modification of overloaded property
// Mock_SqlContentEntityStorage_6202ec22::$stubStorage has no effect" if
// try to use a new own property for this.
$propertyToStoreEntities = 'tableMapping';
$propertyToStoreEntities = 'database';
$entityStorageStub->stubEntityStorageById = [];
UnitTestHelpers::bindClosureToClassMethod(
function () {
},
$entityStorageStub,
'invokeHook'
);
UnitTestHelpers::bindClosureToClassMethod(
function (EntityTypeInterface $entityType) {
$this->entityType = $entityType;
......
......@@ -5,6 +5,7 @@ namespace Drupal\test_helpers;
use Drupal\Component\Uuid\Php as PhpUuid;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Field\Plugin\Field\FieldType\StringItem;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\UnitTestCase;
......@@ -82,11 +83,13 @@ class EntityStubFactory extends UnitTestCase {
$entityTypeId = $storageNew->getEntityTypeId();
$storage = $this->entityTypeManager->stubGetOrCreateStorage($entityTypeId, $storageNew);
$bundle = $values[$entityTypeDefinition->getKey('bundle')] ?? $entityTypeId;
// Creating a stub of the entity.
// @todo Try to init with a real constructor.
/** @var \Drupal\Core\Entity\ContentEntityInterface|\PHPUnit\Framework\MockObject\MockObject $entity */
$entity = $this->createPartialMock($entityClass, [
'getEntityTypeId',
// 'getEntityTypeId',
// 'getFieldDefinitions',
'save',
'delete',
......@@ -104,9 +107,24 @@ class EntityStubFactory extends UnitTestCase {
// Filling values to the entity array.
$fieldItemListStubFactory = $this->fieldItemListStubFactory;
UnitTestHelpers::bindClosureToClassMethod(
function (array $values) use ($fieldItemListStubFactory, $options) {
function (array $values) use ($fieldItemListStubFactory, $options, $entityTypeId, $bundle) {
// Pre-filling entity keys.
$this->entityTypeId = $entityTypeId;
$this->entityKeys['bundle'] = $bundle ? $bundle : $this->entityTypeId;
foreach ($this->getEntityType()->getKeys() as $key => $field) {
if (isset($values[$field])) {
$this->entityKeys[$key] = $values[$field];
}
}
$this->langcodeKey = $this->getEntityType()->getKey('langcode');
$this->defaultLangcodeKey = $this->getEntityType()->getKey('default_langcode');
$this->revisionTranslationAffectedKey = $this->getEntityType()->getKey('revision_translation_affected');
// Filling common values.
$this->translations[LanguageInterface::LANGCODE_DEFAULT] = ['status' => TRUE];
$this->translations[LanguageInterface::LANGCODE_DEFAULT] = [
'status' => TRUE,
'entity' => $this,
];
// Filling values to the entity array.
foreach ($values as $name => $value) {
......@@ -115,7 +133,9 @@ class EntityStubFactory extends UnitTestCase {
}
// @todo Convert entity to TypedDataInterface and pass to the
// item list initialization as a third argument $parent.
$field = $fieldItemListStubFactory->create($name, $value, $definition ?? NULL);
// $parent = EntityAdapter::createFromEntity($this);
$parent = NULL;
$field = $fieldItemListStubFactory->create($name, $value, $definition ?? NULL, $parent);
$this->fieldDefinitions[$name] = $field->getFieldDefinition();
$this->fields[$name][LanguageInterface::LANGCODE_DEFAULT] = $field;
}
......@@ -126,14 +146,6 @@ class EntityStubFactory extends UnitTestCase {
);
$entity->stubInitValues($values);
UnitTestHelpers::bindClosureToClassMethod(
function () use ($entityTypeId) {
return $entityTypeId;
},
$entity,
'getEntityTypeId'
);
UnitTestHelpers::bindClosureToClassMethod(
function () use ($storage) {
$idProperty = $this->getEntityType()->getKey('id') ?? NULL;
......
......@@ -30,6 +30,7 @@ class FieldItemListStubFactory extends TestCase {
*/
public function __construct(FieldTypeManagerStub $fieldTypeManagerStub) {
$this->fieldTypeManagerStub = $fieldTypeManagerStub;
$this->unitTestHelpers = new UnitTestHelpers();
}
/**
......@@ -80,6 +81,23 @@ class FieldItemListStubFactory extends TestCase {
$definition = $this->createFieldItemDefinitionStub($name, $type, StringItem::class);
}
$field = new FieldItemList($definition, $name, $parent);
$field = $this->unitTestHelpers->createPartialMockWithCostructor(FieldItemList::class,
[
'applyDefaultValue',
],
[$definition, $name, $parent]
);
// We have no information about default values because of missing configs,
// so just return the same object.
UnitTestHelpers::bindClosureToClassMethod(
function ($notify = TRUE) {
return $this;
},
$field,
'applyDefaultValue'
);
$field->setValue($values);
return $field;
......
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