You are here

class ConfigMergerTest in Config Merge 8

@coversDefaultClass \Drupal\config_merge\ConfigMerger @group config_merge

Hierarchy

Expanded class hierarchy of ConfigMergerTest

File

tests/src/Unit/ConfigMergerTest.php, line 12

Namespace

Drupal\Tests\config_merge\Unit
View source
class ConfigMergerTest extends UnitTestCase {

  /**
   * @var \Drupal\config_merge\ConfigMerger
   */
  public $configMerger;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->configMerger = new ConfigMerger();
  }

  /**
   * Provides associative data for previous, current, and active states.
   *
   * @return array
   *   An array of three arrays representing previous, current, and active
   *   states of a piece of configuration.
   */
  protected function getAssociativeStates() {
    $previous = [
      'first' => 1,
      'second' => [
        'one',
        'two',
      ],
      'third' => [
        'one' => 'first',
        'two' => 'second',
      ],
      'fourth' => 'fourth',
    ];
    $current = $previous;
    $active = $previous;
    $active['fifth'] = 'fifth';
    return [
      $previous,
      $current,
      $active,
    ];
  }

  /**
   * Provides indexed data for previous, current, and active states.
   *
   * @return array
   *   An array of three arrays representing previous, current, and active
   *   states of a piece of configuration.
   */
  protected function getIndexedStates() {
    $previous = [
      0 => 1,
      1 => [
        'one',
        'two',
      ],
      2 => [
        'one' => 'first',
        'two' => 'second',
      ],
      3 => 'fourth',
    ];
    $current = $previous;
    $active = $previous;
    return [
      $previous,
      $current,
      $active,
    ];
  }

  /**
   * Provides data to ::testMergeConfigItemStates().
   */
  public function statesProvider() {
    $data = [];

    // Provide associative data.
    // Test the case that there is no change between previous and current.
    list($previous, $current, $active) = $this
      ->getAssociativeStates();

    // If there is no difference between previous and current, no changes should
    // be made to active.
    $expected = $active;
    $data['associative no difference'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];

    // Test additions.
    list($previous, $current, $active) = $this
      ->getAssociativeStates();
    $current['second'][] = 'three';
    $current['third']['third'] = 'three';
    $current = array_merge(array_slice($current, 0, 1), [
      'another' => 'test',
    ], array_slice($current, 1));

    // Additions should be merged into active.
    $expected = $active;
    $expected['second'][] = 'three';
    $expected['third']['third'] = 'three';

    // The new array key should be merged at the same position.
    $expected = array_merge(array_slice($expected, 0, 1), [
      'another' => 'test',
    ], array_slice($expected, 1));
    $data['associative additions'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];

    // Test deletions.
    list($previous, $current, $active) = $this
      ->getAssociativeStates();
    unset($current['first']);
    unset($current['second'][array_search('two', $current['second'])]);
    unset($current['third']['one']);

    // Deletions should be made to active.
    $expected = $active;
    unset($expected['first']);
    unset($expected['second'][array_search('two', $expected['second'])]);
    unset($expected['third']['one']);
    $data['associative deletions'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];

    // Test deletions when the value has been customized.
    // Expected is unchanged because a customized value should not be
    // deleted.
    $active['fifth'] = 'customized';
    unset($current['fifth']);
    $expected['fifth'] = 'customized';
    $data['associative deletions with customization'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];

    // Test changes.
    list($previous, $current, $active) = $this
      ->getAssociativeStates();
    $current['third']['one'] = 'change';
    $current['fourth'] = 'change';
    $expected = $active;
    $expected['third']['one'] = 'change';
    $expected['fourth'] = 'change';
    $data['associative changes'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];

    // Test changes with customization.
    // In this case, the active value should be retained despite the
    // availability of an update.
    $active['third']['one'] = 'active';
    $expected['third']['one'] = 'active';
    $data['associative changes with customization'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];

    // Provide indexed data.
    // Test the case that there is no change between previous and current.
    list($previous, $current, $active) = $this
      ->getIndexedStates();
    $active[4] = 'fifth';

    // If there is no difference between previous and current, no changes should
    // be made to active.
    $expected = $active;
    $data['indexed no difference'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];

    // Test additions.
    list($previous, $current, $active) = $this
      ->getIndexedStates();
    $current[1][] = 'three';
    $current[2]['three'] = 'third';
    $current[] = 'test';

    // Current is changed but active is not, so we expect current.
    $expected = $current;
    $data['indexed additions current'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];
    $current[2]['three'] = 'third';
    $active[1][] = 'something';

    // Current is changed but active is also so we expect active.
    $expected = $active;
    $data['indexed additions active'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];

    // Test deletions.
    list($previous, $current, $active) = $this
      ->getIndexedStates();
    unset($current[0]);
    unset($current[1][array_search('two', $current[1])]);
    unset($current[2]['one']);

    // Deletions should be made to active.
    $expected = $active;
    unset($expected[0]);
    unset($expected[1][array_search('two', $expected[1])]);
    unset($expected[2]['one']);
    $data['indexed deletions'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];

    // Test changes.
    list($previous, $current, $active) = $this
      ->getIndexedStates();
    $current[2]['one'] = 'change';
    $current[3] = 'change';
    $expected = $active;
    $expected[2]['one'] = 'change';
    $expected[3] = 'change';
    $data['indexed changes'] = [
      $previous,
      $current,
      $active,
      $expected,
    ];
    return $data;
  }

  /**
   * @covers ::mergeConfigItemStates
   * @dataProvider statesProvider
   */
  public function testMergeConfigItemStates($previous, $current, $active, $expected) {
    $result = $this->configMerger
      ->mergeConfigItemStates($previous, $current, $active);
    $this
      ->assertSame($expected, $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigMergerTest::$configMerger public property
ConfigMergerTest::getAssociativeStates protected function Provides associative data for previous, current, and active states.
ConfigMergerTest::getIndexedStates protected function Provides indexed data for previous, current, and active states.
ConfigMergerTest::setUp public function Overrides UnitTestCase::setUp
ConfigMergerTest::statesProvider public function Provides data to ::testMergeConfigItemStates().
ConfigMergerTest::testMergeConfigItemStates public function @covers ::mergeConfigItemStates @dataProvider statesProvider
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.