function UnitTestCase::getConfigFactoryStub

Same name in other branches
  1. 9 core/tests/Drupal/Tests/UnitTestCase.php \Drupal\Tests\UnitTestCase::getConfigFactoryStub()
  2. 8.9.x core/tests/Drupal/Tests/UnitTestCase.php \Drupal\Tests\UnitTestCase::getConfigFactoryStub()
  3. 10 core/tests/Drupal/Tests/UnitTestCase.php \Drupal\Tests\UnitTestCase::getConfigFactoryStub()

Returns a stub config factory that behaves according to the passed array.

Use this to generate a config factory that will return the desired values for the given config names.

Parameters

array $configs: An associative array of configuration settings whose keys are configuration object names and whose values are key => value arrays for the configuration object in question. Defaults to an empty array.

Return value

\PHPUnit\Framework\MockObject\MockObject|\Drupal\Core\Config\ConfigFactoryInterface A mock configuration factory object.

50 calls to UnitTestCase::getConfigFactoryStub()
AdminNegotiatorTest::testDetermineActiveTheme in core/modules/user/tests/src/Unit/Theme/AdminNegotiatorTest.php
@dataProvider getThemes
AjaxBasePageNegotiatorTest::setUp in core/tests/Drupal/Tests/Core/Theme/AjaxBasePageNegotiatorTest.php
AnnounceFetcherUnitTest::setUp in core/modules/announcements_feed/tests/src/Unit/AnnounceFetcherUnitTest.php
BlockContentLocalTasksTest::setUp in core/modules/block_content/tests/src/Unit/Menu/BlockContentLocalTasksTest.php
BlockLocalTasksTest::setUp in core/modules/block/tests/src/Unit/Menu/BlockLocalTasksTest.php

... See full list

File

core/tests/Drupal/Tests/UnitTestCase.php, line 86

Class

UnitTestCase
Provides a base class and helpers for Drupal unit tests.

Namespace

Drupal\Tests

Code

public function getConfigFactoryStub(array $configs = []) {
    $config_get_map = [];
    $config_editable_map = [];
    // Construct the desired configuration object stubs, each with its own
    // desired return map.
    foreach ($configs as $config_name => $config_values) {
        // Define a closure over the $config_values, which will be used as a
        // returnCallback below. This function will mimic
        // \Drupal\Core\Config\Config::get and allow using dotted keys.
        $config_get = function ($key = '') use ($config_values) {
            // Allow to pass in no argument.
            if (empty($key)) {
                return $config_values;
            }
            // See if we have the key as is.
            if (isset($config_values[$key])) {
                return $config_values[$key];
            }
            $parts = explode('.', $key);
            $value = NestedArray::getValue($config_values, $parts, $key_exists);
            return $key_exists ? $value : NULL;
        };
        $immutable_config_object = $this->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
            ->disableOriginalConstructor()
            ->getMock();
        $immutable_config_object->expects($this->any())
            ->method('get')
            ->willReturnCallback($config_get);
        $config_get_map[] = [
            $config_name,
            $immutable_config_object,
        ];
        $mutable_config_object = $this->getMockBuilder('Drupal\\Core\\Config\\Config')
            ->disableOriginalConstructor()
            ->getMock();
        $mutable_config_object->expects($this->any())
            ->method('get')
            ->willReturnCallback($config_get);
        $config_editable_map[] = [
            $config_name,
            $mutable_config_object,
        ];
    }
    // Construct a config factory with the array of configuration object stubs
    // as its return map.
    $config_factory = $this->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
    $config_factory->expects($this->any())
        ->method('get')
        ->willReturnMap($config_get_map);
    $config_factory->expects($this->any())
        ->method('getEditable')
        ->willReturnMap($config_editable_map);
    return $config_factory;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.