abstract class ConfigUpdateUnitTestBase in Configuration Update Manager 8
Base class for unit testing in Config Update Manager.
This class provides some mock classes for unit testing.
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\config_update\Unit\ConfigUpdateUnitTestBase
Expanded class hierarchy of ConfigUpdateUnitTestBase
File
- tests/
src/ Unit/ ConfigUpdateUnitTestBase.php, line 14
Namespace
Drupal\Tests\config_update\UnitView source
abstract class ConfigUpdateUnitTestBase extends UnitTestCase {
/**
* The mocked entity definition information.
*
* They are not sorted, to test that the methods sort them. Also there are a
* couple with prefixes that are subsets of each other.
*
* @var string[]
*
* @see ConfigUpdateUnitTestBase::getEntityManagerMock().
*/
protected $entityDefinitionInformation = [
[
'prefix' => 'foo.bar',
'type' => 'foo',
],
[
'prefix' => 'foo.barbaz',
'type' => 'bar',
],
[
'prefix' => 'baz.foo',
'type' => 'baz',
],
];
/**
* Creates a mock entity manager for the test.
*
* @see ConfigUpdateUnitTestBase::entityDefinitionInformation
*/
protected function getEntityManagerMock() {
$definitions = [];
$map = [];
foreach ($this->entityDefinitionInformation as $info) {
$def = $this
->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface')
->getMock();
$def
->expects($this
->any())
->method('getConfigPrefix')
->willReturn($info['prefix']);
$def
->expects($this
->any())
->method('entityClassImplements')
->willReturn(TRUE);
$def
->method('getKey')
->willReturn('id');
$def
->getConfigPrefix();
$definitions[$info['type']] = $def;
$map[] = [
$info['type'],
FALSE,
$def,
];
$map[] = [
$info['type'],
TRUE,
$def,
];
}
// Add in a content entity definition, which shouldn't be recognized by the
// config lister class.
$def = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityTypeInterface')
->getMock();
$def
->expects($this
->any())
->method('entityClassImplements')
->willReturn(FALSE);
$definitions['content_entity'] = $def;
$manager = $this
->getMockBuilder('Drupal\\Core\\Entity\\EntityTypeManagerInterface')
->getMock();
$manager
->method('getDefinitions')
->willReturn($definitions);
$manager
->method('getDefinition')
->will($this
->returnValueMap($map));
$manager
->method('getStorage')
->will($this
->returnCallback([
$this,
'mockGetStorage',
]));
return $manager;
}
/**
* Mocks the getStorage() method for the entity manager.
*/
public function mockGetStorage($entity_type) {
// Figure out the config prefix for this entity type.
$prefix = '';
foreach ($this->entityDefinitionInformation as $info) {
if ($info['type'] == $entity_type) {
$prefix = $info['prefix'];
}
}
// This is used in ConfigReverter::import(). Although it is supposed to
// be entity storage, we'll use our mock config object instead.
return new MockConfig('', $prefix, $this);
}
/**
* Array of active configuration information for mocking.
*
* Array structure: Each element is an array whose first element is a
* provider name, and second is an array of config items it provides.
*
* @var array
*
* @see ConfigUpdateUnitTestBase::getConfigStorageMock()
*/
protected $configStorageActiveInfo = [
[
'foo.bar',
[
'foo.bar.one',
'foo.bar.two',
'foo.bar.three',
],
],
[
'foo.barbaz',
[
'foo.barbaz.four',
'foo.barbaz.five',
'foo.barbaz.six',
],
],
[
'baz.foo',
[],
],
[
'',
[
'foo.bar.one',
'foo.bar.two',
'foo.bar.three',
'foo.barbaz.four',
'foo.barbaz.five',
'foo.barbaz.six',
'something.else',
'another.one',
],
],
];
/**
* Array of extension configuration information for mocking.
*
* Array structure: Each element is an array whose first element is a
* provider name, and second is an array of config items it provides.
*
* @var array
*
* @see ConfigUpdateUnitTestBase::getConfigStorageMock()
*/
protected $configStorageExtensionInfo = [
[
'foo.bar',
[
'foo.bar.one',
'foo.bar.two',
'foo.bar.seven',
],
],
[
'baz.foo',
[],
],
// This next item is assumed to be element 2 of the array. If not, you
// will need to change ConfigUpdateUnitTestBase::getConfigStorageMock().
[
'',
[
'foo.bar.one',
'foo.bar.two',
'foo.bar.seven',
'foo.barbaz.four',
'foo.barnot.three',
'something.else',
],
],
];
/**
* Array of optional configuration information for mocking.
*
* Array structure: Each element is an array whose first element is a
* provider name, and second is an array of config items it provides.
*
* @var array
*
* @see ConfigUpdateUnitTestBase::getConfigStorageMock()
*/
protected $configStorageOptionalInfo = [
[
'foo.bar',
[],
],
[
'foo.barbaz',
[
'foo.barbaz.four',
],
],
// This next item is assumed to be element 2 of the array. If not, you
// will need to change ConfigUpdateUnitTestBase::getConfigStorageMock().
[
'',
[
'foo.barbaz.four',
],
],
];
/**
* Creates a mock config storage object for the test.
*
* @param string $type
* Type of storage object to return: 'active', 'extension', or 'optional'.
* In active storage, the read() method is mocked to assume you are reading
* core.extension to get the profile name, so it returns that information.
* For extension and optional storage, the getComponentNames() method is
* mocked, and for all storages, the listAll() method is mocked.
*
* @see ConfigUpdateUnitTestBase::configStorageActiveInfo
* @see ConfigUpdateUnitTestBase::configStorageExtensionInfo
* @see ConfigUpdateUnitTestBase::configStorageOptionalInfo
*/
protected function getConfigStorageMock($type) {
if ($type == 'active') {
$storage = $this
->getMockBuilder('Drupal\\Core\\Config\\StorageInterface')
->getMock();
// Various tests assume various values of configuration that need to be
// read from active storage.
$map = [
[
'core.extension',
[
'profile' => 'standard',
],
],
[
'foo.bar.one',
[
'foo.bar.one' => 'active',
'id' => 'one',
],
],
[
'missing',
FALSE,
],
[
'in.extension',
[
'in.extension' => 'active',
'_core' => 'core_for_in.extension',
],
],
[
'in.both',
[
'in.both' => 'active',
],
],
[
'in.optional',
[
'in.optional' => 'active',
],
],
];
$storage
->method('read')
->will($this
->returnValueMap($map));
$storage
->method('listAll')
->will($this
->returnValueMap($this->configStorageActiveInfo));
}
elseif ($type == 'extension') {
$storage = $this
->getMockBuilder('Drupal\\Core\\Config\\ExtensionInstallStorage')
->disableOriginalConstructor()
->getMock();
$value = [];
foreach ($this->configStorageExtensionInfo[2][1] as $item) {
$value[$item] = 'ignored';
}
$storage
->method('getComponentNames')
->willReturn($value);
$storage
->method('listAll')
->will($this
->returnValueMap($this->configStorageExtensionInfo));
$map = [
[
'in.extension',
[
'in.extension' => 'extension',
],
],
[
'in.both',
[
'in.both' => 'extension',
],
],
[
'in.optional',
FALSE,
],
[
'foo.bar.one',
[
'foo.bar.one' => 'extension',
'id' => 'one',
],
],
[
'another',
[
'another' => 'extension',
'id' => 'one',
],
],
[
'missing2',
FALSE,
],
];
$storage
->method('read')
->will($this
->returnValueMap($map));
}
else {
$storage = $this
->getMockBuilder('Drupal\\Core\\Config\\ExtensionInstallStorage')
->disableOriginalConstructor()
->getMock();
$value = [];
foreach ($this->configStorageOptionalInfo[2][1] as $item) {
$value[$item] = 'ignored';
}
$storage
->method('getComponentNames')
->willReturn($value);
$storage
->method('listAll')
->will($this
->returnValueMap($this->configStorageOptionalInfo));
$map = [
[
'in.optional',
[
'in.optional' => 'optional',
],
],
[
'in.both',
[
'in.both' => 'optional',
],
],
[
'missing2',
FALSE,
],
];
$storage
->method('read')
->will($this
->returnValueMap($map));
}
return $storage;
}
/**
* Creates a mock module handler for the test.
*/
protected function getModuleHandlerMock() {
$manager = $this
->getMockBuilder('Drupal\\Core\\Extension\\ModuleHandlerInterface')
->getMock();
$manager
->method('getModuleList')
->willReturn([
'foo_module' => '',
'standard' => '',
]);
return $manager;
}
/**
* Creates a mock theme handler for the test.
*/
protected function getThemeHandlerMock() {
$manager = $this
->getMockBuilder('Drupal\\Core\\Extension\\ThemeHandlerInterface')
->getMock();
$manager
->method('listInfo')
->willReturn([
'foo_theme' => '',
]);
return $manager;
}
/**
* Creates a mock string translation class for the test.
*/
protected function getTranslationMock() {
$translation = $this
->getMockBuilder('Drupal\\Core\\StringTranslation\\TranslationInterface')
->getMock();
$translation
->method('translateString')
->will($this
->returnCallback([
$this,
'mockTranslate',
]));
return $translation;
}
/**
* Mocks the translateString() method for the string translation mock object.
*
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $input
* Object to translate.
*
* @return string
* The untranslated string from $input.
*/
public function mockTranslate(TranslatableMarkup $input) {
return $input
->getUntranslatedString();
}
/**
* List of mock-dispatched events.
*
* Each element of the array is the call parameters to dispatchEvent() in
* the mocked dispatch class: name and event instance.
*
* @var array
*
* @see ConfigUpdateUnitTestBase::getEventDispatcherMock()
*/
protected $dispatchedEvents = [];
/**
* Mocks the event dispatcher service.
*
* Stores dispatched events in ConfigUpdateUnitTestBase::dispatchedEvents.
*/
protected function getEventDispatcherMock() {
$event = $this
->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')
->getMock();
$event
->method('dispatch')
->will($this
->returnCallback([
$this,
'mockDispatch',
]));
return $event;
}
/**
* Mocks event dispatch.
*
* For \Symfony\Component\EventDispatcher\EventDispatchInterface::dispatch().
*/
public function mockDispatch($name, Event $event = NULL) {
$this->dispatchedEvents[] = [
$name,
$event,
];
}
/**
* Mock config storage for the mock config factory.
*
* This is actually managed by the MockConfig class in this file.
*
* @var array
*/
protected $configStorage = [];
/**
* Gets the value of the mocked config storage.
*/
public function getConfigStorage() {
return $this->configStorage;
}
/**
* Sets the value of the mocked config storage.
*/
public function setConfigStorage($values) {
$this->configStorage = $values;
}
/**
* Creates a mock config factory class for the test.
*/
protected function getConfigFactoryMock() {
$config = $this
->getMockBuilder('Drupal\\Core\\Config\\ConfigFactoryInterface')
->getMock();
$config
->method('getEditable')
->will($this
->returnCallback([
$this,
'mockGetEditable',
]));
return $config;
}
/**
* Mocks the getEditable() method for the mock config factory.
*
* @param string $name
* Name of the config object to get an editable object for.
*
* @return MockConfig
* Editable mock config object.
*/
public function mockGetEditable($name) {
return new MockConfig($name, '', $this);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigUpdateUnitTestBase:: |
protected | property | Mock config storage for the mock config factory. | |
ConfigUpdateUnitTestBase:: |
protected | property | Array of active configuration information for mocking. | |
ConfigUpdateUnitTestBase:: |
protected | property | Array of extension configuration information for mocking. | |
ConfigUpdateUnitTestBase:: |
protected | property | Array of optional configuration information for mocking. | |
ConfigUpdateUnitTestBase:: |
protected | property | List of mock-dispatched events. | |
ConfigUpdateUnitTestBase:: |
protected | property | The mocked entity definition information. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock config factory class for the test. | |
ConfigUpdateUnitTestBase:: |
public | function | Gets the value of the mocked config storage. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock config storage object for the test. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock entity manager for the test. | |
ConfigUpdateUnitTestBase:: |
protected | function | Mocks the event dispatcher service. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock module handler for the test. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock theme handler for the test. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock string translation class for the test. | |
ConfigUpdateUnitTestBase:: |
public | function | Mocks event dispatch. | |
ConfigUpdateUnitTestBase:: |
public | function | Mocks the getEditable() method for the mock config factory. | |
ConfigUpdateUnitTestBase:: |
public | function | Mocks the getStorage() method for the entity manager. | |
ConfigUpdateUnitTestBase:: |
public | function | Mocks the translateString() method for the string translation mock object. | |
ConfigUpdateUnitTestBase:: |
public | function | Sets the value of the mocked config storage. | |
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. | |
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. | |
UnitTestCase:: |
protected | function | 340 |