Unverified Commit bcb5afd4 authored by larowlan's avatar larowlan

Issue #2862248 by m4olivei, Wim Leers, cburschka, larowlan:...

Issue #2862248 by m4olivei, Wim Leers, cburschka, larowlan: UnitTestCase::getConfigFactoryStub() doesn't accept dotted keys
parent 21b9f7d1
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Drupal\Tests; namespace Drupal\Tests;
use Drupal\Component\FileCache\FileCacheFactory; use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Random; use Drupal\Component\Utility\Random;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
...@@ -93,18 +94,19 @@ protected function assertArrayEquals(array $expected, array $actual, $message = ...@@ -93,18 +94,19 @@ protected function assertArrayEquals(array $expected, array $actual, $message =
} }
/** /**
* Returns a stub config factory that behaves according to the passed in array. * 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 * Use this to generate a config factory that will return the desired values
* for the given config names. * for the given config names.
* *
* @param array $configs * @param array $configs
* An associative array of configuration settings whose keys are configuration * An associative array of configuration settings whose keys are
* object names and whose values are key => value arrays for the configuration * configuration object names and whose values are key => value arrays for
* object in question. Defaults to an empty array. * the configuration object in question. Defaults to an empty array.
* *
* @return \PHPUnit_Framework_MockObject_MockBuilder * @return \PHPUnit_Framework_MockObject_MockBuilder
* A MockBuilder object for the ConfigFactory with the desired return values. * A MockBuilder object for the ConfigFactory with the desired return
* values.
*/ */
public function getConfigFactoryStub(array $configs = []) { public function getConfigFactoryStub(array $configs = []) {
$config_get_map = []; $config_get_map = [];
...@@ -112,19 +114,29 @@ public function getConfigFactoryStub(array $configs = []) { ...@@ -112,19 +114,29 @@ public function getConfigFactoryStub(array $configs = []) {
// Construct the desired configuration object stubs, each with its own // Construct the desired configuration object stubs, each with its own
// desired return map. // desired return map.
foreach ($configs as $config_name => $config_values) { foreach ($configs as $config_name => $config_values) {
$map = []; // Define a closure over the $config_values, which will be used as a
foreach ($config_values as $key => $value) { // returnCallback below. This function will mimic
$map[] = [$key, $value]; // \Drupal\Core\Config\Config::get and allow using dotted keys.
} $config_get = function ($key = '') use ($config_values) {
// Also allow to pass in no argument. // Allow to pass in no argument.
$map[] = ['', $config_values]; 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') $immutable_config_object = $this->getMockBuilder('Drupal\Core\Config\ImmutableConfig')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$immutable_config_object->expects($this->any()) $immutable_config_object->expects($this->any())
->method('get') ->method('get')
->will($this->returnValueMap($map)); ->will($this->returnCallback($config_get));
$config_get_map[] = [$config_name, $immutable_config_object]; $config_get_map[] = [$config_name, $immutable_config_object];
$mutable_config_object = $this->getMockBuilder('Drupal\Core\Config\Config') $mutable_config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
...@@ -132,7 +144,7 @@ public function getConfigFactoryStub(array $configs = []) { ...@@ -132,7 +144,7 @@ public function getConfigFactoryStub(array $configs = []) {
->getMock(); ->getMock();
$mutable_config_object->expects($this->any()) $mutable_config_object->expects($this->any())
->method('get') ->method('get')
->will($this->returnValueMap($map)); ->will($this->returnCallback($config_get));
$config_editable_map[] = [$config_name, $mutable_config_object]; $config_editable_map[] = [$config_name, $mutable_config_object];
} }
// Construct a config factory with the array of configuration object stubs // Construct a config factory with the array of configuration object stubs
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment