X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Ftests%2FDrupal%2FTests%2FUnitTestCase.php;h=bd183c7961746a48a0a2ee75f0602875683a7e14;hb=74df008bdbb3a11eeea356744f39b802369bda3c;hp=f96b5109e83ec38151f47b2bf25e31dfe133a1c1;hpb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;p=yaffs-website diff --git a/web/core/tests/Drupal/Tests/UnitTestCase.php b/web/core/tests/Drupal/Tests/UnitTestCase.php index f96b5109e..bd183c796 100644 --- a/web/core/tests/Drupal/Tests/UnitTestCase.php +++ b/web/core/tests/Drupal/Tests/UnitTestCase.php @@ -3,19 +3,22 @@ namespace Drupal\Tests; use Drupal\Component\FileCache\FileCacheFactory; +use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Random; use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\PluralTranslatableMarkup; - +use PHPUnit\Framework\TestCase; /** * Provides a base class and helpers for Drupal unit tests. * * @ingroup testing */ -abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { +abstract class UnitTestCase extends TestCase { + + use PhpunitCompatibilityTrait; /** * The random generator. @@ -91,18 +94,19 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { } /** - * 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 * for the given config names. * * @param 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. + * 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 \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 = []) { $config_get_map = []; @@ -110,19 +114,29 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { // Construct the desired configuration object stubs, each with its own // desired return map. foreach ($configs as $config_name => $config_values) { - $map = []; - foreach ($config_values as $key => $value) { - $map[] = [$key, $value]; - } - // Also allow to pass in no argument. - $map[] = ['', $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->returnValueMap($map)); + ->will($this->returnCallback($config_get)); $config_get_map[] = [$config_name, $immutable_config_object]; $mutable_config_object = $this->getMockBuilder('Drupal\Core\Config\Config') @@ -130,12 +144,12 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { ->getMock(); $mutable_config_object->expects($this->any()) ->method('get') - ->will($this->returnValueMap($map)); + ->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->getMock('Drupal\Core\Config\ConfigFactoryInterface'); + $config_factory = $this->createMock('Drupal\Core\Config\ConfigFactoryInterface'); $config_factory->expects($this->any()) ->method('get') ->will($this->returnValueMap($config_get_map)); @@ -157,7 +171,7 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { * A mocked config storage. */ public function getConfigStorageStub(array $configs) { - $config_storage = $this->getMock('Drupal\Core\Config\NullStorage'); + $config_storage = $this->createMock('Drupal\Core\Config\NullStorage'); $config_storage->expects($this->any()) ->method('listAll') ->will($this->returnValue(array_keys($configs))); @@ -179,6 +193,12 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { * * @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject * The mocked block. + * + * @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. + * + * @see https://www.drupal.org/node/2896072 */ protected function getBlockMockWithMachineName($machine_name) { $plugin = $this->getMockBuilder('Drupal\Core\Block\BlockBase') @@ -194,6 +214,7 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { $block->expects($this->any()) ->method('getPlugin') ->will($this->returnValue($plugin)); + @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); return $block; } @@ -204,7 +225,7 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { * A mock translation object. */ public function getStringTranslationStub() { - $translation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface'); + $translation = $this->createMock('Drupal\Core\StringTranslation\TranslationInterface'); $translation->expects($this->any()) ->method('translate') ->willReturnCallback(function ($string, array $args = [], array $options = []) use ($translation) { @@ -234,7 +255,7 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { * The container with the cache tags invalidator service. */ protected function getContainerWithCacheTagsInvalidator(CacheTagsInvalidatorInterface $cache_tags_validator) { - $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface'); $container->expects($this->any()) ->method('get') ->with('cache_tags.invalidator') @@ -251,7 +272,7 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase { * The class resolver stub. */ protected function getClassResolverStub() { - $class_resolver = $this->getMock('Drupal\Core\DependencyInjection\ClassResolverInterface'); + $class_resolver = $this->createMock('Drupal\Core\DependencyInjection\ClassResolverInterface'); $class_resolver->expects($this->any()) ->method('getInstanceFromDefinition') ->will($this->returnCallback(function ($class) {