abstract class UnitTestBase in Entity Construction Kit (ECK) 8
Base class for unit tests.
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\eck\Unit\UnitTestBase
Expanded class hierarchy of UnitTestBase
File
- tests/
src/ Unit/ UnitTestBase.php, line 17
Namespace
Drupal\Tests\eck\UnitView source
abstract class UnitTestBase extends UnitTestCase {
/**
* The entities.
*
* @var array
*/
protected $entities;
/**
* The services.
*
* @var array
*/
private $services;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->entities = [];
$this
->prepareContainer();
$this
->registerServiceWithContainerMock('current_user', $this
->getNewUserMock());
$this
->registerServiceWithContainerMock('entity_type.manager', $this
->getEntityTypeManagerMock());
$this
->registerServiceWithContainerMock('entity_type.repository', $this
->getEntityTypeRepositoryMock());
$this
->registerServiceWithContainerMock('plugin.manager.field.field_type', new FieldTypePluginManagerMock());
}
/**
* Prepares a mocked service container.
*/
private function prepareContainer() {
$container_class = 'Drupal\\Core\\DependencyInjection\\Container';
$methods = get_class_methods($container_class);
$container = $this
->getMockBuilder($container_class)
->disableOriginalConstructor()
->setMethods($methods)
->getMock();
\Drupal::setContainer($container);
$container
->method('get')
->willReturnCallback([
$this,
'containerMockGetServiceCallback',
]);
}
/**
* Retrieves the entity storage mock.
*/
private function getEntityStorageMock() {
$entity_storage = $this
->getMockForAbstractClass('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_storage
->method('loadMultiple')
->willReturnCallback([
$this,
'entityStorageLoadMultiple',
]);
$entity_storage
->method('load')
->willReturnCallback([
$this,
'entityStorageLoadMultiple',
]);
return $entity_storage;
}
/**
* Retrieves the entity manager mock.
*/
private function getEntityManagerMock() {
$entity_storage = $this
->getEntityStorageMock();
$definition = $this
->getMockForAbstractClass(EntityTypeInterface::class);
$entity_manager = $this
->getMockForAbstractClass(EntityTypeManagerInterface::class);
$entity_manager
->method('getStorage')
->willReturn($entity_storage);
$entity_manager
->method('getDefinition')
->willReturn($definition);
return $entity_manager;
}
/**
* Retrieves the entity type manager mock.
*/
private function getEntityTypeManagerMock() {
$entity_storage = $this
->getEntityStorageMock();
$definition = $this
->getMockForAbstractClass(EntityTypeInterface::class);
$entity_type_manager = $this
->getMockForAbstractClass(EntityTypeManagerInterface::class);
$entity_type_manager
->method('getStorage')
->willReturn($entity_storage);
$entity_type_manager
->method('getDefinition')
->willReturn($definition);
return $entity_type_manager;
}
/**
* Retrieves the entity type repository mock.
*/
private function getEntityTypeRepositoryMock() {
$entity_type_repository = $this
->getMockForAbstractClass(EntityTypeRepositoryInterface::class);
$entity_type_repository
->method('getEntityTypeFromClass')
->willReturn('eck_entity_type');
return $entity_type_repository;
}
/**
* Registers a (mocked) service with the mocked service container.
*
* @param string $service_id
* The service id.
* @param mixed $service
* The service to be returned when the service_id is requested from the
* container.
*/
protected function registerServiceWithContainerMock($service_id, $service) {
$this->services[$service_id] = $service;
}
/**
* Creates and returns a mocked user.
*
* @return \Drupal\Core\Session\AccountProxyInterface
* The mocked user.
*/
private function getNewUserMock() {
$user_mock = $this
->getMockForAbstractClass('\\Drupal\\Core\\Session\\AccountProxyInterface');
$user_mock
->method('id')
->willReturn(1);
return $user_mock;
}
/**
* Callback for the get method on the mocked service container.
*
* @param string $service_id
* The service identifier being called.
*
* @return mixed
* A (mocked) service class if one has been set for the given service id
* or NULL.
*/
public function containerMockGetServiceCallback($service_id) {
if (isset($this->services[$service_id])) {
return $this->services[$service_id];
}
return NULL;
}
/**
* Creates the language manager mock.
*/
protected function createLanguageManagerMock() {
$current_language_mock = $this
->getMockForAbstractClass('\\Drupal\\Core\\Language\\LanguageInterface');
$current_language_mock
->method('getId')
->willReturn('en');
$mock = $this
->getMockForAbstractClass('\\Drupal\\Core\\Language\\LanguageManagerInterface');
$mock
->method('getCurrentLanguage')
->willReturn($current_language_mock);
return $mock;
}
/**
* Callback for entity storage load multiple.
*/
public function entityStorageLoadMultiple($id = '') {
if (!empty($id)) {
return $this->entities[$id];
}
else {
return $this->entities;
}
}
/**
* Adds an entity to the mock storage.
*/
protected function addEntityToStorage(EntityInterface $entity) {
$this->entities[$entity
->id()] = $entity;
}
/**
* Creates a test entity type.
*
* @param string $entity_type_id
* The entity type id.
* @param array $values
* The values to be set on the created entity.
*
* @return \Drupal\eck\Entity\EckEntityType
* The created eck entity type.
*/
protected function createEckEntityType($entity_type_id, array $values = []) {
$values = $values + [
'label' => ucfirst($entity_type_id),
'id' => $entity_type_id,
];
return new EckEntityType($values, $entity_type_id);
}
/**
* Asserts that the array keys of an array equal the expected keys.
*/
protected function assertArrayKeysEqual($expectedKeys, $arrayToAssert) {
$this
->assertEquals($expectedKeys, array_keys($arrayToAssert));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestBase:: |
protected | property | The entities. | |
UnitTestBase:: |
private | property | The services. | |
UnitTestBase:: |
protected | function | Adds an entity to the mock storage. | |
UnitTestBase:: |
protected | function | Asserts that the array keys of an array equal the expected keys. | |
UnitTestBase:: |
public | function | Callback for the get method on the mocked service container. | |
UnitTestBase:: |
protected | function | Creates a test entity type. | |
UnitTestBase:: |
protected | function | Creates the language manager mock. | |
UnitTestBase:: |
public | function | Callback for entity storage load multiple. | |
UnitTestBase:: |
private | function | Retrieves the entity manager mock. | |
UnitTestBase:: |
private | function | Retrieves the entity storage mock. | |
UnitTestBase:: |
private | function | Retrieves the entity type manager mock. | |
UnitTestBase:: |
private | function | Retrieves the entity type repository mock. | |
UnitTestBase:: |
private | function | Creates and returns a mocked user. | |
UnitTestBase:: |
private | function | Prepares a mocked service container. | |
UnitTestBase:: |
protected | function | Registers a (mocked) service with the mocked service container. | |
UnitTestBase:: |
protected | function |
Overrides UnitTestCase:: |
1 |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |