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

Issue #3350342 by Murz: Allow initialization of core services

parent d06d4dd4
No related branches found
No related tags found
No related merge requests found
Showing
with 6641 additions and 858 deletions
<?php
namespace Drupal\test_helpers_example\Plugin\Field;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
/**
* A computed field calculating node age.
*/
class NodeAgeComputedFieldItemList extends FieldItemList {
use ComputedItemListTrait;
/**
* {@inheritdoc}
*/
protected function computeValue() {
$node = $this->getEntity(); /** @var \Drupal\node\NodeInterface $node */
$node_age = \Drupal::time()->getCurrentTime() - $node->getCreatedTime();
$this->list[0] = $this->createItem(0, $node_age);
}
}
<?php
namespace Drupal\Tests\test_helpers_example\Unit;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\node\Entity\Node;
use Drupal\Tests\UnitTestCase;
use Drupal\test_helpers\TestHelpers;
use Drupal\test_helpers_example\Plugin\Field\NodeAgeComputedFieldItemList;
/**
* @coversDefaultClass \Drupal\test_helpers_example\ArticlesManagerService
* @group test_helpers_example
*/
class NodeAgeComputedFieldItemListTest extends UnitTestCase {
/**
* Tests Test Helpers API, related to entities.
*/
public function testComputeValue() {
$timeCreated = 500;
$currentTime = 1000;
TestHelpers::service('datetime.time', NULL, NULL, ['getCurrentTime'])
->method('getCurrentTime')->willReturnCallback(
function () use (&$currentTime) {
return $currentTime;
}
);
$node = TestHelpers::createEntity(Node::class, ['created' => $timeCreated]);
$fieldItemList = TestHelpers::createPartialMockWithConstructor(
NodeAgeComputedFieldItemList::class,
['createItem'],
[
$this->createMock(DataDefinitionInterface::class),
'field_node_age',
EntityAdapter::createFromEntity($node),
],
);
$fieldItemList->method('createItem')->willReturnCallback(
function ($offset = 0, $value = NULL) use (&$currentTime, &$timeCreated) {
$this->assertEquals($currentTime - $timeCreated, $value);
}
);
TestHelpers::callPrivateMethod($fieldItemList, 'computeValue');
$currentTime = 2000;
TestHelpers::callPrivateMethod($fieldItemList, 'computeValue');
}
}
......@@ -2,12 +2,10 @@
namespace Drupal\Tests\test_helpers_example\Unit;
use Drupal\node\Entity\Node;
use Drupal\test_helpers\UnitTestCaseWrapper;
use Drupal\test_helpers\TestHelpers;
use Drupal\test_helpers_example\Controller\TestHelpersExampleController;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\User;
/**
* Tests TestHelpersExampleController with Test Helpers API to check conditions.
......@@ -25,12 +23,12 @@ class TestHelpersExampleControllerModernConditionsTest extends UnitTestCase {
public function testArticlesList() {
TestHelpers::service('config.factory')->stubSetConfig('test_helpers_example.settings', ['articles_to_display' => 2]);
TestHelpers::service('date.formatter')->stubSetFormat('medium', 'Medium', 'd.m.Y');
TestHelpers::saveEntity(User::class, ['name' => 'Alice']);
TestHelpers::saveEntity('user', ['name' => 'Alice']);
// Putting coding standards ignore flag to suppress warnings until the
// https://www.drupal.org/project/coder/issues/3185082 is fixed.
// @codingStandardsIgnoreStart
TestHelpers::saveEntity(Node::class, ['title' => 'A1', 'uid' => 1, 'created' => 1672574400]);
TestHelpers::saveEntity(Node::class, ['title' => 'A2', 'uid' => 1, 'created' => 1672660800]);
TestHelpers::saveEntity('node', ['title' => 'A1', 'uid' => 1, 'created' => 1672574400]);
TestHelpers::saveEntity('node', ['title' => 'A2', 'uid' => 1, 'created' => 1672660800]);
// @codingStandardsIgnoreEnd
TestHelpers::getServiceStub('entity.query.sql')->stubSetExecuteHandler(function () {
......
......@@ -2,11 +2,9 @@
namespace Drupal\Tests\test_helpers_example\Unit;
use Drupal\node\Entity\Node;
use Drupal\test_helpers\TestHelpers;
use Drupal\test_helpers_example\Controller\TestHelpersExampleController;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\User;
/**
* Tests TestHelpersExampleController with Test Helpers API to check the result.
......@@ -24,14 +22,14 @@ class TestHelpersExampleControllerModernResultTest extends UnitTestCase {
public function testArticlesList() {
TestHelpers::service('config.factory')->stubSetConfig('test_helpers_example.settings', ['articles_to_display' => 1]);
TestHelpers::service('date.formatter')->stubSetFormat('medium', 'Medium', 'd.m.Y');
TestHelpers::saveEntity(User::class, ['name' => 'Alice']);
TestHelpers::saveEntity('user', ['name' => 'Alice']);
// Putting coding standards ignore flag to suppress warnings until the
// https://www.drupal.org/project/coder/issues/3185082 is fixed.
// @codingStandardsIgnoreStart
TestHelpers::saveEntity(Node::class, ['type' => 'article', 'title' => 'A1', 'status' => 1, 'uid' => 1, 'created' => 1672574400]);
TestHelpers::saveEntity(Node::class, ['type' => 'article', 'title' => 'A2', 'status' => 1, 'uid' => 1, 'created' => 1672660800]);
TestHelpers::saveEntity(Node::class, ['type' => 'page', 'title' => 'P1', 'status' => 1, 'uid' => 1, 'created' => 1672747200]);
TestHelpers::saveEntity(Node::class, ['type' => 'article', 'title' => 'A3', 'status' => 0, 'uid' => 1, 'created' => 1672833600]);
TestHelpers::saveEntity('node', ['type' => 'article', 'title' => 'A1', 'status' => 1, 'uid' => 1, 'created' => 1672574400]);
TestHelpers::saveEntity('node', ['type' => 'article', 'title' => 'A2', 'status' => 1, 'uid' => 1, 'created' => 1672660800]);
TestHelpers::saveEntity('node', ['type' => 'page', 'title' => 'P1', 'status' => 1, 'uid' => 1, 'created' => 1672747200]);
TestHelpers::saveEntity('node', ['type' => 'article', 'title' => 'A3', 'status' => 0, 'uid' => 1, 'created' => 1672833600]);
// @codingStandardsIgnoreEnd
$result = TestHelpers::createClass(TestHelpersExampleController::class)->articlesList();
......
#!/usr/bin/env php
<?php
/**
* @file
* Generates list of services and entities from the current Drupal Core.
*
* Requres a clean installation of Drupal, without any contrib modules.
*/
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Drupal\test_helpers\TestHelpers;
use Symfony\Component\HttpFoundation\Request;
// @codingStandardsIgnoreLine
const ONELINER = '
export DRUPAL_VERSION=9.3; rm -rf ./drupal_$DRUPAL_VERSION && composer create-project drupal/recommended-project:~$DRUPAL_VERSION.0 drupal_$DRUPAL_VERSION && cd drupal_$DRUPAL_VERSION && composer require drush/drush && composer require drupal/test_helpers:@alpha --prefer-source && ./vendor/bin/drush si --db-url=sqlite://db.sqlite -y
./web/modules/contrib/test_helpers/scripts/generateCoreFeaturesMap.php
';
$contents = <<<EOT
<?php
/**
* @file
* Pre-generated list of the services from a clean Drupal installation.
*
* This list can be regenerated on a clean Drupal installation using the command
* line script `scripts/generateCoreFeaturesMap.php`.
*/
// @codingStandardsIgnoreFile
EOT;
require_once __DIR__ . '/../src/TestHelpers.php';
$drupalRoot = TestHelpers::getDrupalRoot();
chdir($drupalRoot);
$autoloader = include_once $drupalRoot . '/autoload.php';
require_once $drupalRoot . '/core/includes/bootstrap.inc';
$request = Request::createFromGlobals();
Settings::initialize(dirname(__DIR__, 2), DrupalKernel::findSitePath($request), $autoloader);
DrupalKernel::createFromRequest($request, $autoloader, 'prod')->boot();
\Drupal::service('request_stack')->push($request);
$container = \Drupal::getContainer();
$drupalVersionArray = explode('.', \Drupal::VERSION);
$drupalVersionMinor = $drupalVersionArray[0] . '.' . $drupalVersionArray[1];
$filename = dirname(__DIR__) . '/src/includes/CoreFeaturesMaps/CoreFeaturesMap.' . $drupalVersionMinor . '.php';
// Generating services map.
$contents .= <<<EOT
const TEST_HELPERS_DRUPAL_CORE_SERVICE_MAP = [
EOT;
foreach ($container->getServiceIds() as $serviceId) {
$service = $container->get($serviceId);
if (!is_object($service)) {
continue;
}
$class = get_class($service);
$info = TestHelpers::getServiceInfoFromClass($class);
if (isset($info['arguments'])) {
$arguments = ", 'arguments' => ['" . implode("', '", $info['arguments'] ?? []) . "']";
}
else {
$arguments = '';
}
$contents .= " '$serviceId' => ['class' => '\\$class'$arguments],\n";
}
$contents .= <<<EOT
];
EOT;
// Generating storage map.
$contents .= <<<EOT
const TEST_HELPERS_DRUPAL_CORE_STORAGE_MAP = [
EOT;
$entityTypeManager = \Drupal::service('entity_type.manager');
foreach ($entityTypeManager->getDefinitions() as $type => $definition) {
$class = "'\\" . $definition->getClass() . "'";
$contents .= " '$type' => $class,\n";
}
$contents .= <<<EOT
];
EOT;
if (!file_put_contents($filename, $contents)) {
throw new \Exception("Error creating file $filename");
}
echo "Generated services file: $filename" . PHP_EOL;
......@@ -93,7 +93,7 @@ class EntityStorageStubFactory {
$moduleDirectory = dirname(dirname(dirname($entityFile)));
$moduleName = basename($moduleDirectory);
$moduleFile = "$moduleDirectory/$moduleName.module";
file_exists($moduleFile) && require_once $moduleFile;
file_exists($moduleFile) && include_once $moduleFile;
$entityTypeStorageClass = $entityTypeDefinition->getStorageClass();
self::$entityDataStorage ??= [
......@@ -171,10 +171,12 @@ class EntityStorageStubFactory {
break;
}
$addMethods = array_unique([
...($storageOptions['addMethods'] ?? []),
'stubGetAllLatestRevision',
]);
$addMethods = array_unique(
[
...($storageOptions['addMethods'] ?? []),
'stubGetAllLatestRevision',
]
);
$mockMethods = array_unique(array_merge($overridedMethods, $storageOptions['mockMethods'] ?? []));
......@@ -204,24 +206,28 @@ class EntityStorageStubFactory {
],
);
TestHelpers::setMockedClassMethod($entityStorage, 'stubInit', function () use ($entityTypeDefinition) {
$this->entityType = $entityTypeDefinition;
$this->entityTypeId = $this->entityType->id();
TestHelpers::setMockedClassMethod(
$entityStorage, 'stubInit', function () use ($entityTypeDefinition) {
$this->entityType = $entityTypeDefinition;
$this->entityTypeId = $this->entityType->id();
$this->baseEntityClass = $this->entityType->getClass();
$this->entityTypeBundleInfo = TestHelpers::service('entity_type.bundle.info');
$this->baseEntityClass = $this->entityType->getClass();
$this->entityTypeBundleInfo = TestHelpers::service('entity_type.bundle.info');
$this->database = TestHelpers::service('database');
$this->memoryCache = TestHelpers::service('cache.backend.memory')->get('entity_storage_stub.memory_cache.' . $this->entityTypeId);
$this->cacheBackend = TestHelpers::service('cache.backend.memory')->get('entity_storage_stub.cache.' . $this->entityTypeId);
$this->database = TestHelpers::service('database');
$this->memoryCache = TestHelpers::service('cache.backend.memory')->get('entity_storage_stub.memory_cache.' . $this->entityTypeId);
$this->cacheBackend = TestHelpers::service('cache.backend.memory')->get('entity_storage_stub.cache.' . $this->entityTypeId);
}, $entityStorage, 'stubInit');
}, $entityStorage, 'stubInit'
);
$entityStorage->stubInit();
}
$saveFunction = function (EntityInterface $entity, array $names = []) use (&$entitiesStorage, &$entitiesMaxIdStorage, &$entitiesMaxRevisionIdStorage) {
/** @var \Drupal\test_helpers\StubFactory\EntityStubInterface $this */
/**
* @var \Drupal\test_helpers\StubFactory\EntityStubInterface $this
*/
$idProperty = $this->entityType->getKey('id') ?? NULL;
if ($idProperty) {
// The `id` value for even integer autoincrement is stored as string in
......@@ -323,62 +329,70 @@ class EntityStorageStubFactory {
}
if (in_array('delete', $overridedMethods)) {
TestHelpers::setMockedClassMethod($entityStorage, 'delete', function (array $entities) use (&$entitiesStorage) {
foreach ($entities as $entity) {
$id = $entity->id();
if (isset($entitiesStorage['byId'][$id])) {
unset($entitiesStorage['byId'][$id]);
TestHelpers::setMockedClassMethod(
$entityStorage, 'delete', function (array $entities) use (&$entitiesStorage) {
foreach ($entities as $entity) {
$id = $entity->id();
if (isset($entitiesStorage['byId'][$id])) {
unset($entitiesStorage['byId'][$id]);
}
}
}
});
);
}
if (in_array('loadMultiple', $overridedMethods)) {
TestHelpers::setMockedClassMethod($entityStorage, 'loadMultiple', function (array $ids = NULL) use (&$entitiesStorage) {
if ($ids === NULL) {
$entitiesValues = $entitiesStorage['byId'] ?? [];
}
else {
$entitiesValues = [];
foreach ($ids as $id) {
if (isset($entitiesStorage['byId'][$id])) {
$entitiesValues[] = $entitiesStorage['byId'][$id];
TestHelpers::setMockedClassMethod(
$entityStorage, 'loadMultiple', function (array $ids = NULL) use (&$entitiesStorage) {
if ($ids === NULL) {
$entitiesValues = $entitiesStorage['byId'] ?? [];
}
else {
$entitiesValues = [];
foreach ($ids as $id) {
if (isset($entitiesStorage['byId'][$id])) {
$entitiesValues[] = $entitiesStorage['byId'][$id];
}
}
}
$entities = [];
foreach ($entitiesValues as $values) {
$entity = EntityStorageStubFactory::valuesToEntity($this->entityType, $values);
if (($this->entityType instanceof ContentEntityTypeInterface) && $this->entityType->isRevisionable()) {
$entity->updateLoadedRevisionId();
}
$entities[$entity->id()] = $entity;
}
return $entities;
}
);
}
$entities = [];
foreach ($entitiesValues as $values) {
if (in_array('loadRevision', $overridedMethods)) {
TestHelpers::setMockedClassMethod(
$entityStorage, 'loadRevision', function ($id) use (&$entitiesStorage) {
if (!$values = $entitiesStorage['byRevisionId'][$id] ?? NULL) {
return NULL;
}
$entity = EntityStorageStubFactory::valuesToEntity($this->entityType, $values);
if (($this->entityType instanceof ContentEntityTypeInterface) && $this->entityType->isRevisionable()) {
$entity->updateLoadedRevisionId();
}
$entities[$entity->id()] = $entity;
return $entity;
}
return $entities;
});
);
}
if (in_array('loadRevision', $overridedMethods)) {
TestHelpers::setMockedClassMethod($entityStorage, 'loadRevision', function ($id) use (&$entitiesStorage) {
if (!$values = $entitiesStorage['byRevisionId'][$id] ?? NULL) {
return NULL;
}
$entity = EntityStorageStubFactory::valuesToEntity($this->entityType, $values);
if (($this->entityType instanceof ContentEntityTypeInterface) && $this->entityType->isRevisionable()) {
$entity->updateLoadedRevisionId();
TestHelpers::setMockedClassMethod(
$entityStorage, 'stubGetAllLatestRevision', function () use (&$entitiesStorage) {
$entities = [];
foreach ($entitiesStorage['byIdLatestRevision'] ?? [] as $values) {
$entities[] = EntityStorageStubFactory::valuesToEntity($this->entityType, $values);
}
return $entity;
});
}
TestHelpers::setMockedClassMethod($entityStorage, 'stubGetAllLatestRevision', function () use (&$entitiesStorage) {
$entities = [];
foreach ($entitiesStorage['byIdLatestRevision'] ?? [] as $values) {
$entities[] = EntityStorageStubFactory::valuesToEntity($this->entityType, $values);
return $entities;
}
return $entities;
});
);
// Crunches for known specific Core entity types.
switch ($entityTypeId) {
......
......@@ -25,8 +25,9 @@ class EntityStubFactory {
/**
* Creates an entity stub with field values.
*
* @param string $entityTypeClassName
* The entity type class.
* @param string $entityTypeNameOrClass
* A full path to an entity type class, or an entity type id for Drupal
* Core entities like `node`, `taxonomy_term`, etc.
* @param array $values
* A list of values to set in the created entity.
* @param array $translations
......@@ -37,40 +38,57 @@ class EntityStubFactory {
* - addMethods: list of additional methods.
* - skipEntityConstructor: a flag to skip calling the entity constructor.
* - fields: a list of custom field options by field name.
* Applies only on the first initialization of this field.
* Field options supportable formats:
* - A string, indicating field type, like 'integer', 'string',
* 'entity_reference', only core field types are supported.
* - An array with field type and settings, like this:
* [
* '#type' => 'entity_reference',
* '#settings' => ['target_type' => 'node']
* ].
* - A field definition object, that will be applied to the field.
* Applies only on the first initialization of this field.
* Field options supportable formats:
* - A string, indicating field type, like 'integer', 'string',
* 'entity_reference', only core field types are supported.
* - An array with field type and settings, like this:
* [
* '#type' => 'entity_reference',
* '#settings' => ['target_type' => 'node']
* ].
* - A field definition object, that will be applied to the field.
* @param array $storageOptions
* A list of options to pass to the storage initialization. Acts only once
* if the storage is not initialized yet.
* - skipPrePostSave: a flag to use direct save on the storage without
* calling preSave and postSave functions. Can be useful if that functions
* have dependencies which hard to mock.
* calling preSave and postSave functions. Can be useful if that functions
* have dependencies which hard to mock.
* - constructorArguments: additional arguments to the constructor.
*
* @return \Drupal\Core\Entity\EntityInterface|\Drupal\test_helpers\StubFactory\EntityStubInterface
* The stub object for the entity.
*/
public static function create(string $entityTypeClassName, array $values = NULL, array $translations = NULL, array $options = NULL, array $storageOptions = NULL) {
public static function create(
string $entityTypeNameOrClass,
array $values = NULL,
array $translations = NULL,
array $options = NULL,
array $storageOptions = NULL
) {
$values ??= [];
$options ??= [];
TestHelpers::requireCoreFeaturesMap();
$entityTypeClass = ltrim(TEST_HELPERS_DRUPAL_CORE_STORAGE_MAP[$entityTypeNameOrClass] ?? $entityTypeNameOrClass, '\\');
if (is_array($options['methods'] ?? NULL)) {
@trigger_error('The storage option "methods" is deprecated in test_helpers:1.0.0-beta9 and is removed from test_helpers:1.0.0-rc1. Use "mockMethods" instead. See https://www.drupal.org/project/test_helpers/issues/3347857', E_USER_DEPRECATED);
$options['mockMethods'] = array_unique(array_merge($options['mockMethods'] ?? [], $options['methods']));
}
// Creating a new entity storage stub instance, if not exists.
/** @var \Drupal\test_helpers\Stub\EntityTypeManagerStub $entityTypeManager */
/**
* @var \Drupal\test_helpers\Stub\EntityTypeManagerStub $entityTypeManager
*/
$entityTypeManager = TestHelpers::service('entity_type.manager');
/** @var \Drupal\test_helpers\Stub\EntityFieldManagerStub $entityFieldManager */
/**
* @var \Drupal\test_helpers\Stub\EntityFieldManagerStub $entityFieldManager
*/
$entityTypeBundleInfo = TestHelpers::service('entity_type.bundle.info');
$storage = $entityTypeManager->stubGetOrCreateStorage($entityTypeClassName, NULL, FALSE, $storageOptions);
/**
* @var \Drupal\Core\Entity\EntityStorageInterface $storage
*/
$storage = $entityTypeManager->stubGetOrCreateStorage($entityTypeClass, NULL, FALSE, $storageOptions);
$entityTypeDefinition = $storage->getEntityType();
$entityTypeId = $storage->getEntityTypeId();
$bundleKey = $entityTypeDefinition->getKey('bundle');
......@@ -82,10 +100,12 @@ class EntityStubFactory {
if (!$bundleEntity = $bundleStorage->load($bundle)) {
$idKey = $bundleStorage->getEntityType()->getKey('id');
$labelKey = $bundleStorage->getEntityType()->getKey('label');
$bundleEntity = $bundleStorage->create([
$idKey => $values[$bundleKey],
$labelKey => $values[$bundleKey],
]);
$bundleEntity = $bundleStorage->create(
[
$idKey => $values[$bundleKey],
$labelKey => $values[$bundleKey],
]
);
$bundleEntity->save();
}
$entityTypeBundleInfo->stubSetBundleInfo($entityTypeId, $bundle, $bundleEntity);
......@@ -102,7 +122,7 @@ class EntityStubFactory {
// @todo Remove this crunch.
// $entityClass instanceOf ContentEntityBase doesn't work.
if (in_array(ContentEntityBase::class, class_parents($entityTypeClassName))) {
if (in_array(ContentEntityBase::class, class_parents($entityTypeClass))) {
$methodsToMock[] = 'updateOriginalValues';
}
$valuesForConstructor = [];
......@@ -116,15 +136,18 @@ class EntityStubFactory {
'stubSetFieldObject',
...($options['addMethods'] ?? []),
];
/**
* @var \Drupal\test_helpers\StubFactory\EntityStubInterface|\Drupal\Core\Entity\EntityInterface|\PHPUnit\Framework\MockObject\MockObject $entity
*/
if ($options['skipEntityConstructor'] ?? NULL) {
$entity = TestHelpers::createPartialMock(
$entityTypeClassName,
$entityTypeClass,
[...$methodsToMock, ...$addMethods]
);
}
else {
$entity = TestHelpers::createPartialMockWithConstructor(
$entityTypeClassName,
$entityTypeClass,
$methodsToMock,
[
$valuesForConstructor,
......@@ -162,7 +185,9 @@ class EntityStubFactory {
if ($options['skipEntityConstructor'] ?? NULL) {
// If we skipped the original constructor, we must define some
// crucial things manually.
/** @var \Drupal\test_helpers\StubFactory\EntityStubInterface $this */
/**
* @var \Drupal\test_helpers\StubFactory\EntityStubInterface|\Drupal\Core\Entity\EntityInterface $this
*/
$this->entityTypeId = $entityTypeId;
$this->entityKeys['bundle'] = $bundle ? $bundle : $this->entityTypeId;
foreach ($this->getEntityType()->getKeys() as $key => $field) {
......@@ -287,37 +312,47 @@ class EntityStubFactory {
}
$entity->enforceIsNew();
TestHelpers::setMockedClassMethod($entity, 'stubSetFieldObject', function ($fieldName, $fieldObject, $langCode = NULL) {
$this->fieldDefinitions[$fieldName] = $fieldObject;
$langCode ??= $this->activeLangCode;
$this->fields[$fieldName][$langCode] = $fieldObject;
});
TestHelpers::setMockedClassMethod(
$entity, 'stubSetFieldObject', function ($fieldName, $fieldObject, $langCode = NULL) {
/**
* @var \Drupal\test_helpers\StubFactory\EntityStubInterface|\Drupal\Core\Entity\EntityInterface|\PHPUnit\Framework\MockObject\MockObject $this
*/
$this->fieldDefinitions[$fieldName] = $fieldObject;
$langCode ??= $this->activeLangCode;
$this->fields[$fieldName][$langCode] = $fieldObject;
}
);
if (array_search('updateOriginalValues', $methodsToMock)) {
TestHelpers::setMockedClassMethod($entity, 'updateOriginalValues', function (): void {
if (!$this->fields) {
// Phpcs shows an error here: Function return type is not void, but
// function is returning void here.
// Suppressing it.
// @codingStandardsIgnoreStart
return;
// @codingStandardsIgnoreEnd
}
foreach ($this->getFieldDefinitions() as $name => $definition) {
if (!$definition->isComputed() && !empty($this->fields[$name])) {
foreach ($this->fields[$name] as $langcode => $item) {
$item->filterEmptyItems();
// @todo Remove these crunches and use original function.
// Crunches start.
if (isset($this->values[$name]) && !is_array($this->values[$name])) {
$this->values[$name] = [];
TestHelpers::setMockedClassMethod(
$entity, 'updateOriginalValues', function (): void {
/**
* @var \Drupal\test_helpers\StubFactory\EntityStubInterface|\Drupal\Core\Entity\EntityInterface|\PHPUnit\Framework\MockObject\MockObject $this
*/
if (!$this->fields) {
// Phpcs shows an error here: Function return type is not void, but
// function is returning void here.
// Suppressing it.
// @codingStandardsIgnoreStart
return;
// @codingStandardsIgnoreEnd
}
foreach ($this->getFieldDefinitions() as $name => $definition) {
if (!$definition->isComputed() && !empty($this->fields[$name])) {
foreach ($this->fields[$name] as $langcode => $item) {
$item->filterEmptyItems();
// @todo Remove these crunches and use original function.
// Crunches start.
if (isset($this->values[$name]) && !is_array($this->values[$name])) {
$this->values[$name] = [];
}
// Crunches end.
$this->values[$name][$langcode] = $item->getValue();
}
// Crunches end.
$this->values[$name][$langcode] = $item->getValue();
}
}
}
});
);
}
return $entity;
}
......
This diff is collapsed.
......@@ -91,7 +91,7 @@ class UnitTestCaseWrapper extends UnitTestCase {
* @return \PHPUnit\Framework\MockObject\MockObject
* The mocked object
*/
public function createPartialMockWithConstructor(string $originalClassName, array $methods = [], array $constructorArgs = [], array $addMethods = NULL): MockObject {
public function createPartialMockWithConstructor(string $originalClassName, array $methods = NULL, array $constructorArgs = NULL, array $addMethods = NULL): MockObject {
$mockBuilder = $this->getMockBuilder($originalClassName)
->setConstructorArgs($constructorArgs)
->disableOriginalClone()
......@@ -120,18 +120,20 @@ class UnitTestCaseWrapper extends UnitTestCase {
* @return \PHPUnit\Framework\MockObject\MockObject
* The mocked object
*/
public function createPartialMockWithCustomMethods(string $originalClassName, array $methods = [], array $addMethods = []): MockObject {
public function createPartialMockWithCustomMethods(string $originalClassName, array $methods = NULL, array $addMethods = NULL): MockObject {
$mockBuilder = $this->getMockBuilder($originalClassName)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->allowMockingUnknownTypes()
// ->enableProxyingToOriginalMethods()
->onlyMethods($methods);
->allowMockingUnknownTypes();
if (!empty($methods)) {
$mockBuilder->onlyMethods($methods);
}
if (!empty($addMethods)) {
$mockBuilder->addMethods($addMethods);
}
// @todo Try to add enableProxyingToOriginalMethods() function.
return $mockBuilder->getMock();
}
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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