class ConfigMergerTest in Config Merge 8
@coversDefaultClass \Drupal\config_merge\ConfigMerger @group config_merge
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\config_merge\Unit\ConfigMergerTest
Expanded class hierarchy of ConfigMergerTest
File
- tests/
src/ Unit/ ConfigMergerTest.php, line 12
Namespace
Drupal\Tests\config_merge\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigMergerTest:: |
public | property | ||
ConfigMergerTest:: |
protected | function | Provides associative data for previous, current, and active states. | |
ConfigMergerTest:: |
protected | function | Provides indexed data for previous, current, and active states. | |
ConfigMergerTest:: |
public | function |
Overrides UnitTestCase:: |
|
ConfigMergerTest:: |
public | function | Provides data to ::testMergeConfigItemStates(). | |
ConfigMergerTest:: |
public | function | @covers ::mergeConfigItemStates @dataProvider statesProvider | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |