You are here

public function UnitTestCase::getConfigFactoryStub in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/UnitTestCase.php \Drupal\Tests\UnitTestCase::getConfigFactoryStub()
  2. 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\MockBuilder A MockBuilder object for the ConfigFactory with the desired return values.

42 calls to UnitTestCase::getConfigFactoryStub()
AdminNegotiatorTest::testDetermineActiveTheme in core/modules/user/tests/src/Unit/Theme/AdminNegotiatorTest.php
@dataProvider getThemes
AggregatorPluginSettingsBaseTest::setUp in core/modules/aggregator/tests/src/Unit/Plugin/AggregatorPluginSettingsBaseTest.php
AjaxBasePageNegotiatorTest::setUp in core/tests/Drupal/Tests/Core/Theme/AjaxBasePageNegotiatorTest.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 111

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')
      ->will($this
      ->returnCallback($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')
      ->will($this
      ->returnCallback($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')
    ->will($this
    ->returnValueMap($config_get_map));
  $config_factory
    ->expects($this
    ->any())
    ->method('getEditable')
    ->will($this
    ->returnValueMap($config_editable_map));
  return $config_factory;
}