comparison core/tests/Drupal/Tests/UnitTestCase.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests; 3 namespace Drupal\Tests;
4 4
5 use Drupal\Component\FileCache\FileCacheFactory; 5 use Drupal\Component\FileCache\FileCacheFactory;
6 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Component\Utility\Random; 7 use Drupal\Component\Utility\Random;
7 use Drupal\Core\Cache\CacheTagsInvalidatorInterface; 8 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
8 use Drupal\Core\DependencyInjection\ContainerBuilder; 9 use Drupal\Core\DependencyInjection\ContainerBuilder;
9 use Drupal\Core\StringTranslation\TranslatableMarkup; 10 use Drupal\Core\StringTranslation\TranslatableMarkup;
10 use Drupal\Core\StringTranslation\PluralTranslatableMarkup; 11 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
91 ksort($actual); 92 ksort($actual);
92 $this->assertEquals($expected, $actual, $message); 93 $this->assertEquals($expected, $actual, $message);
93 } 94 }
94 95
95 /** 96 /**
96 * Returns a stub config factory that behaves according to the passed in array. 97 * Returns a stub config factory that behaves according to the passed array.
97 * 98 *
98 * Use this to generate a config factory that will return the desired values 99 * Use this to generate a config factory that will return the desired values
99 * for the given config names. 100 * for the given config names.
100 * 101 *
101 * @param array $configs 102 * @param array $configs
102 * An associative array of configuration settings whose keys are configuration 103 * An associative array of configuration settings whose keys are
103 * object names and whose values are key => value arrays for the configuration 104 * configuration object names and whose values are key => value arrays for
104 * object in question. Defaults to an empty array. 105 * the configuration object in question. Defaults to an empty array.
105 * 106 *
106 * @return \PHPUnit_Framework_MockObject_MockBuilder 107 * @return \PHPUnit_Framework_MockObject_MockBuilder
107 * A MockBuilder object for the ConfigFactory with the desired return values. 108 * A MockBuilder object for the ConfigFactory with the desired return
109 * values.
108 */ 110 */
109 public function getConfigFactoryStub(array $configs = []) { 111 public function getConfigFactoryStub(array $configs = []) {
110 $config_get_map = []; 112 $config_get_map = [];
111 $config_editable_map = []; 113 $config_editable_map = [];
112 // Construct the desired configuration object stubs, each with its own 114 // Construct the desired configuration object stubs, each with its own
113 // desired return map. 115 // desired return map.
114 foreach ($configs as $config_name => $config_values) { 116 foreach ($configs as $config_name => $config_values) {
115 $map = []; 117 // Define a closure over the $config_values, which will be used as a
116 foreach ($config_values as $key => $value) { 118 // returnCallback below. This function will mimic
117 $map[] = [$key, $value]; 119 // \Drupal\Core\Config\Config::get and allow using dotted keys.
118 } 120 $config_get = function ($key = '') use ($config_values) {
119 // Also allow to pass in no argument. 121 // Allow to pass in no argument.
120 $map[] = ['', $config_values]; 122 if (empty($key)) {
123 return $config_values;
124 }
125 // See if we have the key as is.
126 if (isset($config_values[$key])) {
127 return $config_values[$key];
128 }
129 $parts = explode('.', $key);
130 $value = NestedArray::getValue($config_values, $parts, $key_exists);
131 return $key_exists ? $value : NULL;
132 };
121 133
122 $immutable_config_object = $this->getMockBuilder('Drupal\Core\Config\ImmutableConfig') 134 $immutable_config_object = $this->getMockBuilder('Drupal\Core\Config\ImmutableConfig')
123 ->disableOriginalConstructor() 135 ->disableOriginalConstructor()
124 ->getMock(); 136 ->getMock();
125 $immutable_config_object->expects($this->any()) 137 $immutable_config_object->expects($this->any())
126 ->method('get') 138 ->method('get')
127 ->will($this->returnValueMap($map)); 139 ->will($this->returnCallback($config_get));
128 $config_get_map[] = [$config_name, $immutable_config_object]; 140 $config_get_map[] = [$config_name, $immutable_config_object];
129 141
130 $mutable_config_object = $this->getMockBuilder('Drupal\Core\Config\Config') 142 $mutable_config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
131 ->disableOriginalConstructor() 143 ->disableOriginalConstructor()
132 ->getMock(); 144 ->getMock();
133 $mutable_config_object->expects($this->any()) 145 $mutable_config_object->expects($this->any())
134 ->method('get') 146 ->method('get')
135 ->will($this->returnValueMap($map)); 147 ->will($this->returnCallback($config_get));
136 $config_editable_map[] = [$config_name, $mutable_config_object]; 148 $config_editable_map[] = [$config_name, $mutable_config_object];
137 } 149 }
138 // Construct a config factory with the array of configuration object stubs 150 // Construct a config factory with the array of configuration object stubs
139 // as its return map. 151 // as its return map.
140 $config_factory = $this->createMock('Drupal\Core\Config\ConfigFactoryInterface'); 152 $config_factory = $this->createMock('Drupal\Core\Config\ConfigFactoryInterface');
179 * @param string $machine_name 191 * @param string $machine_name
180 * The machine name of the block plugin. 192 * The machine name of the block plugin.
181 * 193 *
182 * @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject 194 * @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject
183 * The mocked block. 195 * The mocked block.
196 *
197 * @deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Unit test
198 * base classes should not have dependencies on extensions. Set up mocks in
199 * individual tests.
200 *
201 * @see https://www.drupal.org/node/2896072
184 */ 202 */
185 protected function getBlockMockWithMachineName($machine_name) { 203 protected function getBlockMockWithMachineName($machine_name) {
186 $plugin = $this->getMockBuilder('Drupal\Core\Block\BlockBase') 204 $plugin = $this->getMockBuilder('Drupal\Core\Block\BlockBase')
187 ->disableOriginalConstructor() 205 ->disableOriginalConstructor()
188 ->getMock(); 206 ->getMock();
194 ->disableOriginalConstructor() 212 ->disableOriginalConstructor()
195 ->getMock(); 213 ->getMock();
196 $block->expects($this->any()) 214 $block->expects($this->any())
197 ->method('getPlugin') 215 ->method('getPlugin')
198 ->will($this->returnValue($plugin)); 216 ->will($this->returnValue($plugin));
217 @trigger_error(__METHOD__ . ' is deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Unit test base classes should not have dependencies on extensions. Set up mocks in individual tests.', E_USER_DEPRECATED);
199 return $block; 218 return $block;
200 } 219 }
201 220
202 /** 221 /**
203 * Returns a stub translation manager that just returns the passed string. 222 * Returns a stub translation manager that just returns the passed string.