class EntityTypeManagerTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Entity/EntityTypeManagerTest.php \Drupal\Tests\Core\Entity\EntityTypeManagerTest
- 10 core/tests/Drupal/Tests/Core/Entity/EntityTypeManagerTest.php \Drupal\Tests\Core\Entity\EntityTypeManagerTest
- 11.x core/tests/Drupal/Tests/Core/Entity/EntityTypeManagerTest.php \Drupal\Tests\Core\Entity\EntityTypeManagerTest
@coversDefaultClass \Drupal\Core\Entity\EntityTypeManager @group Entity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpunitCompatibilityTrait
- class \Drupal\Tests\Core\Entity\EntityTypeManagerTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of EntityTypeManagerTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityTypeManagerTest.php, line 33
Namespace
Drupal\Tests\Core\EntityView source
class EntityTypeManagerTest extends UnitTestCase {
/**
* The entity type manager under test.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* The translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $translationManager;
/**
* The plugin discovery.
*
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $discovery;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $moduleHandler;
/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $cacheBackend;
/**
* The entity last installed schema repository.
*
* @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $entityLastInstalledSchemaRepository;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
$this->moduleHandler
->getImplementations('entity_type_build')
->willReturn([]);
$this->moduleHandler
->alter('entity_type', Argument::type('array'))
->willReturn(NULL);
$this->cacheBackend = $this->prophesize(CacheBackendInterface::class);
$this->translationManager = $this->prophesize(TranslationInterface::class);
$this->entityLastInstalledSchemaRepository = $this->prophesize(EntityLastInstalledSchemaRepositoryInterface::class);
$this->entityTypeManager = new TestEntityTypeManager(new \ArrayObject(), $this->moduleHandler
->reveal(), $this->cacheBackend
->reveal(), $this->translationManager
->reveal(), $this->getClassResolverStub(), $this->entityLastInstalledSchemaRepository
->reveal());
$this->discovery = $this->prophesize(DiscoveryInterface::class);
$this->entityTypeManager
->setDiscovery($this->discovery
->reveal());
}
/**
* Sets up the entity type manager to be tested.
*
* @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions
* (optional) An array of entity type definitions.
*/
protected function setUpEntityTypeDefinitions($definitions = []) {
$class = $this->getMockClass(EntityInterface::class);
foreach ($definitions as $key => $entity_type) {
// \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called
// by \Drupal\Core\Entity\EntityManager::processDefinition() so it must
// always be mocked.
$entity_type->getLinkTemplates()
->willReturn([]);
// Give the entity type a legitimate class to return.
$entity_type->getClass()
->willReturn($class);
$entity_type->setClass($class)
->willReturn($entity_type->reveal());
$definitions[$key] = $entity_type->reveal();
}
$this->discovery
->getDefinition(Argument::cetera())
->will(function ($args) use ($definitions) {
$entity_type_id = $args[0];
$exception_on_invalid = $args[1];
if (isset($definitions[$entity_type_id])) {
return $definitions[$entity_type_id];
}
elseif (!$exception_on_invalid) {
return NULL;
}
else {
throw new PluginNotFoundException($entity_type_id);
}
});
$this->discovery
->getDefinitions()
->willReturn($definitions);
}
/**
* Tests the hasHandler() method.
*
* @covers ::hasHandler
*
* @dataProvider providerTestHasHandler
*/
public function testHasHandler($entity_type_id, $expected) {
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->hasHandlerClass('storage')
->willReturn(TRUE);
$banana = $this->prophesize(EntityTypeInterface::class);
$banana->hasHandlerClass('storage')
->willReturn(FALSE);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$entity_type = $this->entityTypeManager
->hasHandler($entity_type_id, 'storage');
$this->assertSame($expected, $entity_type);
}
/**
* Provides test data for testHasHandler().
*
* @return array
* Test data.
*/
public function providerTestHasHandler() {
return [
[
'apple',
TRUE,
],
[
'banana',
FALSE,
],
[
'pear',
FALSE,
],
];
}
/**
* Tests the getStorage() method.
*
* @covers ::getStorage
*/
public function testGetStorage() {
$class = $this->getTestHandlerClass();
$entity = $this->prophesize(EntityTypeInterface::class);
$entity->getHandlerClass('storage')
->willReturn($class);
$this->setUpEntityTypeDefinitions([
'test_entity_type' => $entity,
]);
$this->assertInstanceOf($class, $this->entityTypeManager
->getStorage('test_entity_type'));
}
/**
* Tests the getListBuilder() method.
*
* @covers ::getListBuilder
*/
public function testGetListBuilder() {
$class = $this->getTestHandlerClass();
$entity = $this->prophesize(EntityTypeInterface::class);
$entity->getHandlerClass('list_builder')
->willReturn($class);
$this->setUpEntityTypeDefinitions([
'test_entity_type' => $entity,
]);
$this->assertInstanceOf($class, $this->entityTypeManager
->getListBuilder('test_entity_type'));
}
/**
* Tests the getViewBuilder() method.
*
* @covers ::getViewBuilder
*/
public function testGetViewBuilder() {
$class = $this->getTestHandlerClass();
$entity = $this->prophesize(EntityTypeInterface::class);
$entity->getHandlerClass('view_builder')
->willReturn($class);
$this->setUpEntityTypeDefinitions([
'test_entity_type' => $entity,
]);
$this->assertInstanceOf($class, $this->entityTypeManager
->getViewBuilder('test_entity_type'));
}
/**
* Tests the getAccessControlHandler() method.
*
* @covers ::getAccessControlHandler
*/
public function testGetAccessControlHandler() {
$class = $this->getTestHandlerClass();
$entity = $this->prophesize(EntityTypeInterface::class);
$entity->getHandlerClass('access')
->willReturn($class);
$this->setUpEntityTypeDefinitions([
'test_entity_type' => $entity,
]);
$this->assertInstanceOf($class, $this->entityTypeManager
->getAccessControlHandler('test_entity_type'));
}
/**
* Tests the getFormObject() method.
*
* @covers ::getFormObject
*/
public function testGetFormObject() {
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getFormClass('default')
->willReturn(TestEntityForm::class);
$banana = $this->prophesize(EntityTypeInterface::class);
$banana->getFormClass('default')
->willReturn(TestEntityFormInjected::class);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$apple_form = $this->entityTypeManager
->getFormObject('apple', 'default');
$this->assertInstanceOf(TestEntityForm::class, $apple_form);
$this->assertAttributeInstanceOf(ModuleHandlerInterface::class, 'moduleHandler', $apple_form);
$this->assertAttributeInstanceOf(TranslationInterface::class, 'stringTranslation', $apple_form);
$banana_form = $this->entityTypeManager
->getFormObject('banana', 'default');
$this->assertInstanceOf(TestEntityFormInjected::class, $banana_form);
$this->assertAttributeEquals('yellow', 'color', $banana_form);
}
/**
* Tests the getFormObject() method with an invalid operation.
*
* @covers ::getFormObject
*/
public function testGetFormObjectInvalidOperation() {
$entity = $this->prophesize(EntityTypeInterface::class);
$entity->getFormClass('edit')
->willReturn('');
$this->setUpEntityTypeDefinitions([
'test_entity_type' => $entity,
]);
$this->expectException(InvalidPluginDefinitionException::class);
$this->entityTypeManager
->getFormObject('test_entity_type', 'edit');
}
/**
* Tests the getHandler() method.
*
* @covers ::getHandler
*/
public function testGetHandler() {
$class = $this->getTestHandlerClass();
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getHandlerClass('storage')
->willReturn($class);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
]);
$apple_controller = $this->entityTypeManager
->getHandler('apple', 'storage');
$this->assertInstanceOf($class, $apple_controller);
$this->assertAttributeInstanceOf(ModuleHandlerInterface::class, 'moduleHandler', $apple_controller);
$this->assertAttributeInstanceOf(TranslationInterface::class, 'stringTranslation', $apple_controller);
}
/**
* Tests the getHandler() method when no controller is defined.
*
* @covers ::getHandler
*/
public function testGetHandlerMissingHandler() {
$entity = $this->prophesize(EntityTypeInterface::class);
$entity->getHandlerClass('storage')
->willReturn('');
$this->setUpEntityTypeDefinitions([
'test_entity_type' => $entity,
]);
$this->expectException(InvalidPluginDefinitionException::class);
$this->entityTypeManager
->getHandler('test_entity_type', 'storage');
}
/**
* @covers ::getRouteProviders
*/
public function testGetRouteProviders() {
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getRouteProviderClasses()
->willReturn([
'default' => TestRouteProvider::class,
]);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
]);
$apple_route_provider = $this->entityTypeManager
->getRouteProviders('apple');
$this->assertInstanceOf(TestRouteProvider::class, $apple_route_provider['default']);
$this->assertAttributeInstanceOf(ModuleHandlerInterface::class, 'moduleHandler', $apple_route_provider['default']);
$this->assertAttributeInstanceOf(TranslationInterface::class, 'stringTranslation', $apple_route_provider['default']);
}
/**
* Tests the processDefinition() method.
*
* @covers ::processDefinition
*/
public function testProcessDefinition() {
$apple = $this->prophesize(EntityTypeInterface::class);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
]);
$apple->getLinkTemplates()
->willReturn([
'canonical' => 'path/to/apple',
]);
$definition = $apple->reveal();
$this->expectException(InvalidLinkTemplateException::class);
$this->expectExceptionMessage("Link template 'canonical' for entity type 'apple' must start with a leading slash, the current link template is 'path/to/apple'");
$this->entityTypeManager
->processDefinition($definition, 'apple');
}
/**
* Tests the getDefinition() method.
*
* @covers ::getDefinition
*
* @dataProvider providerTestGetDefinition
*/
public function testGetDefinition($entity_type_id, $expected) {
$entity = $this->prophesize(EntityTypeInterface::class);
$this->setUpEntityTypeDefinitions([
'apple' => $entity,
'banana' => $entity,
]);
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id, FALSE);
if ($expected) {
$this->assertInstanceOf(EntityTypeInterface::class, $entity_type);
}
else {
$this->assertNull($entity_type);
}
}
/**
* Provides test data for testGetDefinition().
*
* @return array
* Test data.
*/
public function providerTestGetDefinition() {
return [
[
'apple',
TRUE,
],
[
'banana',
TRUE,
],
[
'pear',
FALSE,
],
];
}
/**
* Tests the getDefinition() method with an invalid definition.
*
* @covers ::getDefinition
*/
public function testGetDefinitionInvalidException() {
$this->setUpEntityTypeDefinitions();
$this->expectException(PluginNotFoundException::class);
$this->expectExceptionMessage('The "pear" entity type does not exist.');
$this->entityTypeManager
->getDefinition('pear', TRUE);
}
/**
* Gets a mock controller class name.
*
* @return string
* A mock controller class name.
*/
protected function getTestHandlerClass() {
return get_class($this->getMockForAbstractClass(EntityHandlerBase::class));
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
EntityTypeManagerTest::$cacheBackend | protected | property | The cache backend. | |||
EntityTypeManagerTest::$discovery | protected | property | The plugin discovery. | |||
EntityTypeManagerTest::$entityLastInstalledSchemaRepository | protected | property | The entity last installed schema repository. | |||
EntityTypeManagerTest::$entityTypeManager | protected | property | The entity type manager under test. | |||
EntityTypeManagerTest::$moduleHandler | protected | property | The module handler. | |||
EntityTypeManagerTest::$translationManager | protected | property | The translation manager. | |||
EntityTypeManagerTest::getTestHandlerClass | protected | function | Gets a mock controller class name. | |||
EntityTypeManagerTest::providerTestGetDefinition | public | function | Provides test data for testGetDefinition(). | |||
EntityTypeManagerTest::providerTestHasHandler | public | function | Provides test data for testHasHandler(). | |||
EntityTypeManagerTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
EntityTypeManagerTest::setUpEntityTypeDefinitions | protected | function | Sets up the entity type manager to be tested. | |||
EntityTypeManagerTest::testGetAccessControlHandler | public | function | Tests the getAccessControlHandler() method. | |||
EntityTypeManagerTest::testGetDefinition | public | function | Tests the getDefinition() method. | |||
EntityTypeManagerTest::testGetDefinitionInvalidException | public | function | Tests the getDefinition() method with an invalid definition. | |||
EntityTypeManagerTest::testGetFormObject | public | function | Tests the getFormObject() method. | |||
EntityTypeManagerTest::testGetFormObjectInvalidOperation | public | function | Tests the getFormObject() method with an invalid operation. | |||
EntityTypeManagerTest::testGetHandler | public | function | Tests the getHandler() method. | |||
EntityTypeManagerTest::testGetHandlerMissingHandler | public | function | Tests the getHandler() method when no controller is defined. | |||
EntityTypeManagerTest::testGetListBuilder | public | function | Tests the getListBuilder() method. | |||
EntityTypeManagerTest::testGetRouteProviders | public | function | @covers ::getRouteProviders | |||
EntityTypeManagerTest::testGetStorage | public | function | Tests the getStorage() method. | |||
EntityTypeManagerTest::testGetViewBuilder | public | function | Tests the getViewBuilder() method. | |||
EntityTypeManagerTest::testHasHandler | public | function | Tests the hasHandler() method. | |||
EntityTypeManagerTest::testProcessDefinition | public | function | Tests the processDefinition() method. | |||
PhpunitCompatibilityTrait::getMock | Deprecated | public | function | Returns a mock object for the specified class using the available method. | ||
PhpunitCompatibilityTrait::setExpectedException | Deprecated | public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | ||
UnitTestCase::$randomGenerator | protected | property | The random generator. | |||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::assertArrayEquals | protected | function | Asserts if two arrays are equal by sorting them first. | |||
UnitTestCase::getBlockMockWithMachineName | Deprecated | protected | function | Mocks a block with a block plugin. | 1 | |
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
UnitTestCase::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::randomMachineName | public | function | Generates a unique random string containing letters and numbers. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.