You are here

public function FixGetConfigFactoryStubTrait::getConfigFactoryStub in Purge 8.3

Overrides ::getConfigFactoryStub().

@todo Lines 52-57 have been added in order to make mutable stub calls work in unit tests, e.g.: ->getEditable()->set()->save(). This hasn't yet been reported to drupal.org, feel free!

See also

\Drupal\Tests\UnitTestCase::getConfigFactoryStub

9 calls to FixGetConfigFactoryStubTrait::getConfigFactoryStub()
LoggerServiceTest::testDeleteChannel in tests/src/Unit/Logger/LoggerServiceTest.php
@covers ::deleteChannel
LoggerServiceTest::testDeleteChannels in tests/src/Unit/Logger/LoggerServiceTest.php
@covers ::deleteChannels
LoggerServiceTest::testDestruct in tests/src/Unit/Logger/LoggerServiceTest.php
@covers ::destruct
LoggerServiceTest::testGet in tests/src/Unit/Logger/LoggerServiceTest.php
@covers ::get
LoggerServiceTest::testGetChannels in tests/src/Unit/Logger/LoggerServiceTest.php
@covers ::getChannels

... See full list

File

tests/src/Unit/FixGetConfigFactoryStubTrait.php, line 22

Class

FixGetConfigFactoryStubTrait
Overrides ::getConfigFactoryStub().

Namespace

Drupal\Tests\purge\Unit

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) {
    $map = [];
    foreach ($config_values as $key => $value) {
      $map[] = [
        $key,
        $value,
      ];
    }

    // Also allow to pass in no argument.
    $map[] = [
      '',
      $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[] = [
      $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));
    $mutable_config_object
      ->expects($this
      ->any())
      ->method('set')
      ->will($this
      ->returnValue($mutable_config_object));
    $mutable_config_object
      ->expects($this
      ->any())
      ->method('save')
      ->will($this
      ->returnValue($mutable_config_object));
    $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;
}