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

Allowed passing plugins to TypedDataManager

parent 3f269a5d
No related branches found
No related tags found
No related merge requests found
......@@ -28,18 +28,17 @@ class EntityStorageStubFactory extends UnitTestCase {
'loadByProperties',
'delete',
/** Custom helper functions for the stub: */
/** Attaches an entity type object to the stub. */
// Custom helper functions for the stub:
// Attaches an entity type object to the stub.
'setEntityType',
/** Generates a next entity id, emulating DB autoincrement behavior. */
// Generates a next entity id, emulating DB autoincrement behavior.
'stubGetNewEntityId',
/** Adds or replaces an entity to the static storage. */
// Adds or replaces an entity to the static storage.
'stubAddEntity',
/** Deletes an entity from the static storage. */
// Deletes an entity from the static storage.
'stubDeleteEntity',
]);
......
......@@ -166,19 +166,26 @@ class EntityStubFactory extends UnitTestCase {
}
/**
* Returns the Field Type Manager stub.
* Returns the FieldTypeManagerStub.
*/
public function getFieldTypeManagerStub() {
return $this->fieldTypeManagerStub;
}
/**
* Returns the Field Type Manager stub.
* Returns the FieldItemListStubFactory.
*/
public function getFieldItemListStubFactory() {
return $this->fieldItemListStubFactory;
}
/**
* Returns the TypedDataManagerStub.
*/
public function getTypedDataManagerStub() {
return $this->typedDataManagerStub;
}
/**
* Generates a new entity id, using auto increment like method.
*/
......
......@@ -18,15 +18,14 @@ class EntityTypeManagerStubFactory extends UnitTestCase {
$entityTypeManagerStub = $this->createPartialMock(EntityTypeManager::class, [
'findDefinitions',
/** Custom helper functions for the stub: */
/** Adds a definition to the static storage. */
// Custom helper functions for the stub:
// Adds a definition to the static storage.
'stubAddDefinition',
/** Adds or creates a handler. */
// Adds or creates a handler.
'stubGetOrCreateHandler',
/** Adds or creates a storage. */
// Adds or creates a storage.
'stubGetOrCreateStorage',
]);
......
......@@ -63,7 +63,9 @@ class FieldTypeManagerStub extends UnitTestCase {
$fieldTypePluginManager
->method('createFieldItem')
->willReturnCallback(function ($items, $index, $values) {
$itemClass = $items->getFieldDefinition()->getItemDefinition()->getClass();
if ($items->getFieldDefinition()->getItemDefinition()) {
$itemClass = $items->getFieldDefinition()->getItemDefinition()->getClass();
}
foreach ($this->fieldItemClassByListClassMap as $listClass => $itemClassCandidate) {
if ($items instanceof $listClass) {
$itemClass = $itemClassCandidate;
......
......@@ -16,20 +16,28 @@ class TypedDataManagerStubFactory extends UnitTestCase {
*/
public function createInstance() {
/** @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit\Framework\MockObject\MockObject $instance */
$instance = $this->createPartialMock(TypedDataManager::class, ['getDefinition']);
$instance = $this->createPartialMock(TypedDataManager::class, [
'getDefinition',
'stubAddPlugin',
]);
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;
return $this->stubPluginsDefinition[$plugin_id] ?? NULL;
},
$instance,
'getDefinition'
);
UnitTestHelpers::bindClosureToClassMethod(
function ($class) {
$definition = UnitTestHelpers::getPluginDefinition($class, 'TypedData');
$this->stubPluginsDefinition[$definition['id']] = $definition;
},
$instance,
'stubAddPlugin'
);
$instance->stubAddPlugin(StringData::class);
return $instance;
}
......
......@@ -3,7 +3,6 @@
namespace Drupal\test_helpers;
use Drupal\Component\Annotation\Doctrine\SimpleAnnotationReader;
use Drupal\Component\Plugin\Definition\PluginDefinition;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
......@@ -41,7 +40,7 @@ class UnitTestHelpers extends UnitTestCase {
$reader->addNamespace('Drupal\Core\Annotation');
$reader->addNamespace('Drupal\Core\\' . $plugin . '\Annotation');
// If no annotation name is passed, just getting the first anotatin;
// If no annotation name is passed, just getting the first anotation.
if (!$annotationName) {
$annotation = current($reader->getClassAnnotations($rc));
}
......
......@@ -34,6 +34,7 @@ class EntityStorageStubApiTest extends UnitTestCase {
$node1Values = [
'type' => 'article',
'title' => 'My cool article',
'empty_field' => NULL,
'body' => 'Very interesting article text.',
'field_sign' => 'Alice',
'field_tags' => [
......@@ -53,6 +54,9 @@ class EntityStorageStubApiTest extends UnitTestCase {
$this->assertEquals($node1Values['field_tags'], $node1Entity->field_tags->getValue());
$this->assertEquals($node1Values['field_tags'][1]['target_id'], $node1Entity->field_tags[1]->getValue()['target_id']);
$this->assertFalse($node1Entity->title->isEmpty());
$this->assertTrue($node1Entity->empty_field->isEmpty());
$node1Entity->save();
$node1EntityId = $node1Entity->id();
......
......@@ -14,6 +14,9 @@ use Drupal\Tests\UnitTestCase;
*/
class ExampleCodeTest extends UnitTestCase {
/**
* Tests the module Example code.
*/
public function testExampleCode() {
$entityStubFactory = new EntityStubFactory();
......
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