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

Issue #3347857 by Murz: Rename "methods" option to "mockMethods" to be more clear

parent ae4084f4
No related branches found
Tags 8.x-3.1
No related merge requests found
......@@ -123,17 +123,12 @@ class EntityTypeManagerStub extends EntityTypeManager implements EntityTypeManag
/**
* {@inheritDoc}
*/
public function stubGetOrCreateStorage(string $entityClass, $storageInstanceOrAnnotation = NULL, bool $forceOverride = FALSE, $storageOptions = NULL) {
public function stubGetOrCreateStorage(string $entityClass, $storageInstance = NULL, bool $forceOverride = FALSE, $storageOptions = NULL) {
if (!$forceOverride && isset($this->stubEntityStoragesByClass[$entityClass])) {
return $this->stubEntityStoragesByClass[$entityClass];
}
elseif (is_object($storageInstanceOrAnnotation)) {
$storage = $storageInstanceOrAnnotation;
}
// In this case the annotation is passed, so we should manually initiate
// the storage instance.
elseif (is_string($storageInstanceOrAnnotation)) {
$storage = EntityStorageStubFactory::create($entityClass, $storageInstanceOrAnnotation, $storageOptions);
elseif (is_object($storageInstance)) {
$storage = $storageInstance;
}
else {
$storage = EntityStorageStubFactory::create($entityClass, NULL, $storageOptions);
......@@ -143,7 +138,7 @@ class EntityTypeManagerStub extends EntityTypeManager implements EntityTypeManag
$this->handlers['storage'][$entityTypeId] = $storage;
$this->definitions[$entityTypeId] = $storage->getEntityType();
if ($bundleEntityType = $this->definitions[$entityTypeId]->getBundleEntityType()) {
if ($this->definitions[$entityTypeId] && $bundleEntityType = $this->definitions[$entityTypeId]->getBundleEntityType()) {
// @todo Invent a better way to load the bundle entity type.
$bundleEntityClassName = (new CamelCaseToSnakeCaseNameConverter(NULL, FALSE))->denormalize($bundleEntityType);
$entityNamespace = substr($entityClass, 0, strrpos($entityClass, '\\'));
......
......@@ -42,7 +42,7 @@ class EntityStorageStubFactory {
* @param array|null $storageOptions
* The array of options:
* - constructorArguments: additional arguments to the constructor.
* - methods: list of methods to make mockable.
* - mockMethods: list of methods to make mockable.
* - addMethods: list of additional methods.
* - skipPrePostSave: a flag to use direct save on the storage without
* calling preSave and postSave functions. Can be useful if that functions
......@@ -56,6 +56,10 @@ class EntityStorageStubFactory {
*/
public static function create(string $entityTypeClass, string $annotation = NULL, array $storageOptions = NULL) {
$storageOptions ??= [];
if (is_array($storageOptions['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 XXX', E_USER_DEPRECATED);
$storageOptions['mockMethods'] = array_unique(array_merge($storageOptions['mockMethods'] ?? [], $storageOptions['methods']));
}
switch ($annotation) {
case 'ContentEntityType':
case 'ConfigEntityType':
......@@ -66,9 +70,11 @@ class EntityStorageStubFactory {
$entityTypeDefinition = TestHelpers::getPluginDefinition($entityTypeClass, 'Entity', $annotation);
}
else {
// Starting with the Content Entity type at first.
$annotation = '\Drupal\Core\Entity\Annotation\ContentEntityType';
$entityTypeDefinition = TestHelpers::getPluginDefinition($entityTypeClass, 'Entity', $annotation);
if ($entityTypeDefinition == NULL) {
// If it fails - use Config Entity type.
$annotation = '\Drupal\Core\Entity\Annotation\ConfigEntityType';
$entityTypeDefinition = TestHelpers::getPluginDefinition($entityTypeClass, 'Entity', $annotation);
}
......@@ -164,17 +170,21 @@ class EntityStorageStubFactory {
}
break;
}
$overridedMethods = array_unique(array_merge($overridedMethods, $storageOptions['methods'] ?? []));
$addMethods = array_unique([
...($storageOptions['addMethods'] ?? []),
'stubGetAllLatestRevision',
]);
$mockMethods = array_unique(array_merge($overridedMethods, $storageOptions['mockMethods'] ?? []));
// Removing requested mocked methods from mocking by the current class.
$overridedMethods = array_diff($overridedMethods, [...$storageOptions['mockMethods'] ?? [], ...$addMethods]);
if ($constructArguments) {
$entityStorage = TestHelpers::createPartialMockWithConstructor(
$entityTypeStorageClass,
$overridedMethods,
$mockMethods,
$constructArguments,
$addMethods,
);
......@@ -185,7 +195,7 @@ class EntityStorageStubFactory {
$entityStorage = TestHelpers::createPartialMock(
$entityTypeStorageClass,
[
...$overridedMethods,
...$mockMethods,
...$addMethods,
'stubInit',
],
......
......@@ -33,7 +33,7 @@ class EntityStubFactory {
* A list of translations to add to the created entity.
* @param array $options
* A list of options to entity stub creation:
* - methods: list of methods to make mockable.
* - mockMethods: list of methods to make mockable.
* - addMethods: list of additional methods.
* - skipEntityConstructor: a flag to skip calling the entity constructor.
* - fields: a list of custom field options by field name.
......@@ -61,6 +61,10 @@ class EntityStubFactory {
public static function create(string $entityTypeClassName, array $values = NULL, array $translations = NULL, array $options = NULL, array $storageOptions = NULL) {
$values ??= [];
$options ??= [];
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 XXX', 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 */
$entityTypeManager = TestHelpers::service('entity_type.manager');
......@@ -91,7 +95,7 @@ class EntityStubFactory {
$entityTypeBundleInfo->stubSetBundleInfo($entityTypeId, $bundle);
}
$methodsToMock = $options['methods'] ?? [];
$methodsToMock = $options['mockMethods'] ?? [];
if ($bundleKey) {
$methodsToMock[] = 'bundleFieldDefinitions';
}
......
......@@ -457,7 +457,7 @@ class TestHelpers {
return $container->get($serviceName);
}
elseif ($class === NULL) {
$class = self::getServiceStub($serviceName, $mockableMethods, $addMockableMethods);
$class = self::getServiceStubClass($serviceName, $mockableMethods, $addMockableMethods);
}
elseif (is_string($class)) {
$class = self::createMock($class);
......@@ -566,7 +566,7 @@ class TestHelpers {
* A list of translations to add to the created entity.
* @param array $options
* A list of options to entity stub creation:
* - methods: list of methods to make mockable.
* - mockMethods: list of methods to make mockable.
* - addMethods: list of additional methods.
* - skipPrePostSave: a flag to use direct save on the storage without
* calling preSave and postSave functions. Can be useful if that functions
......@@ -609,7 +609,7 @@ class TestHelpers {
* A list of translations to add to the created entity.
* @param array $options
* A list of options to entity stub creation:
* - methods: list of methods to make mockable.
* - mockMethods: list of methods to make mockable.
* - addMethods: list of additional methods.
* - skipPrePostSave: a flag to use direct save on the storage without
* calling preSave and postSave functions. Can be useful if that functions
......@@ -642,8 +642,8 @@ class TestHelpers {
*
* @param string $entityTypeClassName
* The entity class.
* @param string|object $annotation
* The annotation class.
* @param \Drupal\Core\Entity\EntityStorageInterface $storageInstance
* An already initialized instance of a storage, NULL to create a new one.
* @param bool $forceOverride
* Forces creation of the new clear storage, if exists.
* @param array $storageOptions
......@@ -653,12 +653,14 @@ class TestHelpers {
* calling preSave and postSave functions. Can be useful if that functions
* have dependencies which hard to mock.
* - constructorArguments: additional arguments to the constructor.
* - mockMethods: a list of storage methods to mock.
* - addMethods: a list of storage methods to add.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* The initialized stub of Entity Storage.
*/
public static function getEntityStorage(string $entityTypeClassName, $annotation = NULL, bool $forceOverride = FALSE, array $storageOptions = NULL): EntityStorageInterface {
return self::getServiceStub('entity_type.manager')->stubGetOrCreateStorage($entityTypeClassName, $annotation, $forceOverride, $storageOptions);
public static function getEntityStorage(string $entityTypeClassName, EntityStorageInterface $storageInstance = NULL, bool $forceOverride = FALSE, array $storageOptions = NULL): EntityStorageInterface {
return self::getServiceStub('entity_type.manager')->stubGetOrCreateStorage($entityTypeClassName, $storageInstance, $forceOverride, $storageOptions);
}
/**
......
......@@ -10,13 +10,16 @@ use Drupal\test_helpers\TestHelpers;
/**
* Tests CreateEntityStub API function.
*
* @coversDefaultClass Drupal\test_helpers\TestHelpers
* @coversDefaultClass \Drupal\test_helpers\TestHelpers
* @group test_helpers
*/
class CreateEntityStubTest extends UnitTestCase {
/**
* Tests creating Entity Stubs.
*
* @covers ::createEntity
* @covers \Drupal\test_helpers\StubFactory\EntityStubFactory::create
*/
public function testCreateEntityStub() {
// Creating mocked entities to test the results.
......@@ -118,6 +121,9 @@ class CreateEntityStubTest extends UnitTestCase {
/**
* Tests creating and saving entitites.
*
* @covers ::createEntity
* @covers \Drupal\test_helpers\StubFactory\EntityStubFactory::create
*/
public function testSaveEntityStub() {
$node = TestHelpers::saveEntity(Node::class);
......@@ -128,6 +134,9 @@ class CreateEntityStubTest extends UnitTestCase {
/**
* Tests creating configuration Entities.
*
* @covers ::createEntity
* @covers \Drupal\test_helpers\StubFactory\EntityStubFactory::create
*/
public function testEntityStorageStubWithConfigurationEntities() {
$values = [
......
<?php
namespace Drupal\Tests\test_helpers\Unit\UnitTestHelpersApi;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\UnitTestCase;
use Drupal\test_helpers\TestHelpers;
/**
* Tests Query helper functions.
*
* @coversDefaultClass \Drupal\test_helpers\TestHelpers
* @group test_helpers
*/
class GetEntityStorageTest extends UnitTestCase {
/**
* @covers ::getEntityStorage
* @covers \Drupal\test_helpers\StubFactory\EntityStorageStubFactory::create
* @covers \Drupal\test_helpers\Stub\EntityTypeManagerStub::stubGetOrCreateStorage
*/
public function testGetEntityStorage() {
$storage1 = TestHelpers::getEntityStorage(Term::class, NULL, FALSE);
$this->assertSame([], $storage1->loadMultiple());
$storage2 = TestHelpers::getEntityStorage(Term::class, NULL, FALSE, [
'mockMethods' => ['loadMultiple'],
]);
$this->assertSame([], $storage2->loadMultiple());
$storage3 = TestHelpers::getEntityStorage(Term::class, NULL, TRUE, [
'mockMethods' => ['loadMultiple'],
]);
$this->assertSame(NULL, $storage3->loadMultiple());
$storage4 = TestHelpers::getEntityStorage(Term::class, NULL, TRUE, [
'addMethods' => ['newMethod1', 'newMethod2'],
]);
$storage4->method('newMethod2')->willReturn('foo');
$this->assertSame(NULL, $storage4->newMethod1());
$this->assertSame('foo', $storage4->newMethod2());
}
}
......@@ -8,7 +8,7 @@ use Drupal\Tests\UnitTestCase;
/**
* Tests IsNestedArraySubsetOfTest API function.
*
* @coversDefaultClass Drupal\test_helpers\TestHelpers
* @coversDefaultClass \Drupal\test_helpers\TestHelpers
* @group test_helpers
*/
class IsNestedArraySubsetOfTest extends UnitTestCase {
......
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