You are here

public function UnitTestCase::getConfigFactoryStub in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/UnitTestCase.php \Drupal\Tests\UnitTestCase::getConfigFactoryStub()

Returns a stub config factory that behaves according to the passed in 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.

37 calls to UnitTestCase::getConfigFactoryStub()
AggregatorPluginSettingsBaseTest::setUp in core/modules/aggregator/tests/src/Unit/Plugin/AggregatorPluginSettingsBaseTest.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
BookManagerTest::setUp in core/modules/book/tests/src/Unit/BookManagerTest.php
ConfigEntityMapperTest::setUp in core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php

... See full list

File

core/tests/Drupal/Tests/UnitTestCase.php, line 110
Contains \Drupal\Tests\UnitTestCase.

Class

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

Namespace

Drupal\Tests

Code

public function getConfigFactoryStub(array $configs = array()) {
  $config_get_map = array();
  $config_editable_map = array();

  // Construct the desired configuration object stubs, each with its own
  // desired return map.
  foreach ($configs as $config_name => $config_values) {
    $map = array();
    foreach ($config_values as $key => $value) {
      $map[] = array(
        $key,
        $value,
      );
    }

    // Also allow to pass in no argument.
    $map[] = array(
      '',
      $config_values,
    );
    $immutable_config_object = $this
      ->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
      ->disableOriginalConstructor()
      ->getMock();
    $immutable_config_object
      ->expects($this
      ->any())
      ->method('get')
      ->will($this
      ->returnValueMap($map));
    $config_get_map[] = array(
      $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
      ->returnValueMap($map));
    $config_editable_map[] = array(
      $config_name,
      $mutable_config_object,
    );
  }

  // Construct a config factory with the array of configuration object stubs
  // as its return map.
  $config_factory = $this
    ->getMock('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;
}