View source
<?php
namespace Drupal\Tests\config_merge\Unit;
use Drupal\config_merge\ConfigMerger;
use Drupal\Tests\UnitTestCase;
class ConfigMergerTest extends UnitTestCase {
public $configMerger;
public function setUp() {
$this->configMerger = new ConfigMerger();
}
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,
];
}
protected function getIndexedStates() {
$previous = [
0 => 1,
1 => [
'one',
'two',
],
2 => [
'one' => 'first',
'two' => 'second',
],
3 => 'fourth',
];
$current = $previous;
$active = $previous;
return [
$previous,
$current,
$active,
];
}
public function statesProvider() {
$data = [];
list($previous, $current, $active) = $this
->getAssociativeStates();
$expected = $active;
$data['associative no difference'] = [
$previous,
$current,
$active,
$expected,
];
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));
$expected = $active;
$expected['second'][] = 'three';
$expected['third']['third'] = 'three';
$expected = array_merge(array_slice($expected, 0, 1), [
'another' => 'test',
], array_slice($expected, 1));
$data['associative additions'] = [
$previous,
$current,
$active,
$expected,
];
list($previous, $current, $active) = $this
->getAssociativeStates();
unset($current['first']);
unset($current['second'][array_search('two', $current['second'])]);
unset($current['third']['one']);
$expected = $active;
unset($expected['first']);
unset($expected['second'][array_search('two', $expected['second'])]);
unset($expected['third']['one']);
$data['associative deletions'] = [
$previous,
$current,
$active,
$expected,
];
$active['fifth'] = 'customized';
unset($current['fifth']);
$expected['fifth'] = 'customized';
$data['associative deletions with customization'] = [
$previous,
$current,
$active,
$expected,
];
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,
];
$active['third']['one'] = 'active';
$expected['third']['one'] = 'active';
$data['associative changes with customization'] = [
$previous,
$current,
$active,
$expected,
];
list($previous, $current, $active) = $this
->getIndexedStates();
$active[4] = 'fifth';
$expected = $active;
$data['indexed no difference'] = [
$previous,
$current,
$active,
$expected,
];
list($previous, $current, $active) = $this
->getIndexedStates();
$current[1][] = 'three';
$current[2]['three'] = 'third';
$current[] = 'test';
$expected = $current;
$data['indexed additions current'] = [
$previous,
$current,
$active,
$expected,
];
$current[2]['three'] = 'third';
$active[1][] = 'something';
$expected = $active;
$data['indexed additions active'] = [
$previous,
$current,
$active,
$expected,
];
list($previous, $current, $active) = $this
->getIndexedStates();
unset($current[0]);
unset($current[1][array_search('two', $current[1])]);
unset($current[2]['one']);
$expected = $active;
unset($expected[0]);
unset($expected[1][array_search('two', $expected[1])]);
unset($expected[2]['one']);
$data['indexed deletions'] = [
$previous,
$current,
$active,
$expected,
];
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;
}
public function testMergeConfigItemStates($previous, $current, $active, $expected) {
$result = $this->configMerger
->mergeConfigItemStates($previous, $current, $active);
$this
->assertSame($expected, $result);
}
}