migmag-1.0.x-dev/tests/src/Unit/MigMagArrayUtilityTest.php
tests/src/Unit/MigMagArrayUtilityTest.php
<?php
namespace Drupal\Tests\migmag\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\migmag\Utility\MigMagArrayUtility;
/**
* Tests MigMagArrayUtility.
*
* @coversDefaultClass \Drupal\migmag\Utility\MigMagArrayUtility
*
* @group migmag
*/
class MigMagArrayUtilityTest extends UnitTestCase {
/**
* A dummy migration process pipeline array used for testing.
*
* @const array[]
*/
const TEST_MIGRATION_PROCESS = [
'first' => 'foo',
'second' => 'bar',
'third' => 'baz',
];
/**
* @covers \Drupal\migmag\Utility\MigMagArrayUtility::insertInFrontOfKey
*
* @dataProvider providerInsertInFront
*/
public function testInsertInFrontOfKey(array $test_migration_processes, string $next_destination, $new_process_pipeline, bool $overwrite, array $expected_processes, ?string $expected_exception = NULL) {
if ($expected_exception) {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage($expected_exception);
}
MigMagArrayUtility::insertInFrontOfKey(
$test_migration_processes,
$next_destination,
'new',
$new_process_pipeline,
$overwrite
);
$this->assertSame(
$expected_processes,
$test_migration_processes
);
}
/**
* @covers \Drupal\migmag\Utility\MigMagArrayUtility::insertAfterKey
*
* @dataProvider providerInsertAfter
*/
public function testInsertAfterKey(array $test_migration_processes, string $next_destination, $new_process_pipeline, bool $overwrite, array $expected_processes, ?string $expected_exception = NULL) {
if ($expected_exception) {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage($expected_exception);
}
MigMagArrayUtility::insertAfterKey(
$test_migration_processes,
$next_destination,
'new',
$new_process_pipeline,
$overwrite
);
$this->assertSame(
$expected_processes,
$test_migration_processes
);
}
/**
* @covers \Drupal\migmag\Utility\MigMagArrayUtility::moveInFrontOfKey
*
* @dataProvider providerMoveInFrontOf
*/
public function testMoveInFrontOf(array $test_array, string $reference_key, string $moved_key, array $expected_array, ?string $expected_exception = NULL) {
if ($expected_exception) {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage($expected_exception);
}
MigMagArrayUtility::moveInFrontOfKey(
$test_array,
$reference_key,
$moved_key
);
$this->assertSame(
$expected_array,
$test_array
);
}
/**
* @covers \Drupal\migmag\Utility\MigMagArrayUtility::moveAfterKey
*
* @dataProvider providerMoveAfter
*/
public function testMoveAfter(array $test_array, string $reference_key, string $moved_key, array $expected_array, ?string $expected_exception = NULL) {
if ($expected_exception) {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage($expected_exception);
}
MigMagArrayUtility::moveAfterKey(
$test_array,
$reference_key,
$moved_key
);
$this->assertSame(
$expected_array,
$test_array
);
}
/**
* Data provider for ::testInsertInFrontOfKey.
*
* @return array
* The test cases.
*/
public static function providerInsertInFront(): array {
return [
'Insert in front of first' => [
'test_migration_processes' => self::TEST_MIGRATION_PROCESS,
'next_destination' => 'first',
'new_process_pipeline' => 'new',
'overwrite' => FALSE,
'expected_processes' => [
'new' => 'new',
'first' => 'foo',
'second' => 'bar',
'third' => 'baz',
],
],
'Insert in front of second' => [
'test_migration_processes' => self::TEST_MIGRATION_PROCESS,
'next_destination' => 'second',
'new_process_pipeline' => 'new',
'overwrite' => FALSE,
'expected_processes' => [
'first' => 'foo',
'new' => 'new',
'second' => 'bar',
'third' => 'baz',
],
],
'Insert in front of third' => [
'test_migration_processes' => self::TEST_MIGRATION_PROCESS,
'next_destination' => 'third',
'new_process_pipeline' => 'new',
'overwrite' => FALSE,
'expected_processes' => [
'first' => 'foo',
'second' => 'bar',
'new' => 'new',
'third' => 'baz',
],
],
'Preexisting process in the right pos, no overwrite' => [
'test_migration_processes' => ['new' => 'new'] + self::TEST_MIGRATION_PROCESS,
'next_destination' => 'third',
'new_process_pipeline' => [
'plugin' => 'get',
'source' => 'foo',
],
'overwrite' => FALSE,
'expected_processes' => [
'new' => 'new',
'first' => 'foo',
'second' => 'bar',
'third' => 'baz',
],
],
'Preexisting process in the right pos, with overwrite' => [
'test_migration_processes' => ['new' => 'new'] + self::TEST_MIGRATION_PROCESS,
'next_destination' => 'third',
'new_process_pipeline' => [
'plugin' => 'get',
'source' => 'foo',
],
'overwrite' => TRUE,
'expected_processes' => [
'new' => [
'plugin' => 'get',
'source' => 'foo',
],
'first' => 'foo',
'second' => 'bar',
'third' => 'baz',
],
],
'Missing reference point' => [
'test_migration_processes' => self::TEST_MIGRATION_PROCESS,
'next_destination' => 'missing',
'new_process_pipeline' => 'baz',
'overwrite' => FALSE,
'expected_processes' => [],
'expected_exception' => "The reference key 'missing' cannot be found in the array.",
],
];
}
/**
* Data provider for ::testInsertAfterKey.
*
* @return array
* The test cases.
*/
public static function providerInsertAfter(): array {
return [
'Insert after first' => [
'test_migration_processes' => self::TEST_MIGRATION_PROCESS,
'next_destination' => 'first',
'new_process_pipeline' => 'new',
'overwrite' => FALSE,
'expected_processes' => [
'first' => 'foo',
'new' => 'new',
'second' => 'bar',
'third' => 'baz',
],
],
'Insert after second' => [
'test_migration_processes' => self::TEST_MIGRATION_PROCESS,
'next_destination' => 'second',
'new_process_pipeline' => 'new',
'overwrite' => FALSE,
'expected_processes' => [
'first' => 'foo',
'second' => 'bar',
'new' => 'new',
'third' => 'baz',
],
],
'Insert after third' => [
'test_migration_processes' => self::TEST_MIGRATION_PROCESS,
'next_destination' => 'third',
'new_process_pipeline' => 'new',
'overwrite' => FALSE,
'expected_processes' => [
'first' => 'foo',
'second' => 'bar',
'third' => 'baz',
'new' => 'new',
],
],
'Preexisting process in the right pos, no overwrite' => [
'test_migration_processes' => self::TEST_MIGRATION_PROCESS + ['new' => 'new'],
'next_destination' => 'second',
'new_process_pipeline' => [
'plugin' => 'get',
'source' => 'foo',
],
'overwrite' => FALSE,
'expected_processes' => [
'first' => 'foo',
'second' => 'bar',
'third' => 'baz',
'new' => 'new',
],
],
'Preexisting process in the right pos, with overwrite' => [
'test_migration_processes' => self::TEST_MIGRATION_PROCESS + ['new' => 'new'],
'next_destination' => 'third',
'new_process_pipeline' => [
'plugin' => 'get',
'source' => 'foo',
],
'overwrite' => TRUE,
'expected_processes' => [
'first' => 'foo',
'second' => 'bar',
'third' => 'baz',
'new' => [
'plugin' => 'get',
'source' => 'foo',
],
],
],
'Missing reference point' => [
'test_migration_processes' => self::TEST_MIGRATION_PROCESS,
'next_destination' => 'missing',
'new_process_pipeline' => 'baz',
'overwrite' => FALSE,
'expected_processes' => [],
'expected_exception' => "The reference key 'missing' cannot be found in the array.",
],
];
}
/**
* Data provider for ::testMoveAfter.
*
* @return array
* The test cases.
*/
public static function providerMoveAfter(): array {
return [
'Move first after second' => [
'test_array' => self::TEST_MIGRATION_PROCESS,
'reference_key' => 'second',
'moved_key' => 'first',
'expected_array' => [
'second' => 'bar',
'first' => 'foo',
'third' => 'baz',
],
],
'Move first after third' => [
'test_array' => self::TEST_MIGRATION_PROCESS,
'reference_key' => 'third',
'moved_key' => 'first',
'expected_array' => [
'second' => 'bar',
'third' => 'baz',
'first' => 'foo',
],
],
'Third after first' => [
'test_array' => self::TEST_MIGRATION_PROCESS,
'reference_key' => 'first',
'moved_key' => 'third',
'expected_array' => [
'first' => 'foo',
'second' => 'bar',
'third' => 'baz',
],
],
];
}
/**
* Data provider for ::testMoveInFrontOf.
*
* @return array
* The test cases.
*/
public static function providerMoveInFrontOf(): array {
return [
'Move third in front of first' => [
'test_array' => self::TEST_MIGRATION_PROCESS,
'reference_key' => 'first',
'moved_key' => 'third',
'expected_array' => [
'third' => 'baz',
'first' => 'foo',
'second' => 'bar',
],
],
'Move third in front of second' => [
'test_array' => self::TEST_MIGRATION_PROCESS,
'reference_key' => 'second',
'moved_key' => 'third',
'expected_array' => [
'first' => 'foo',
'third' => 'baz',
'second' => 'bar',
],
],
'First in front of third' => [
'test_array' => self::TEST_MIGRATION_PROCESS,
'reference_key' => 'third',
'moved_key' => 'first',
'expected_array' => [
'first' => 'foo',
'second' => 'bar',
'third' => 'baz',
],
],
];
}
/**
* @covers ::addSuffixToArrayValues
*
* @dataProvider providerAddSuffixToArrayValues
*/
public function testAddSuffixToArrayValues(array $dependencies, array $dependency_ids_to_process, string $derivative_suffix, array $expected_dependencies): void {
MigMagArrayUtility::addSuffixToArrayValues($dependencies, $dependency_ids_to_process, $derivative_suffix);
$this->assertSame($expected_dependencies, $dependencies);
}
/**
* Data provider for ::testAddSuffixToMigrationDependencies.
*
* @return array
* The test cases.
*/
public static function providerAddSuffixToArrayValues(): array {
return [
'Single matching array key' => [
'dependencies' => [
'foo',
'bar',
'baz',
],
'dependency_ids_to_process' => ['bar'],
'derivative_suffix' => ':sub:bar',
'expected_dependencies' => [
'foo',
'bar:sub:bar',
'baz',
],
],
'No matching array key' => [
'dependencies' => [
'foo',
'bar',
'baz',
],
'dependency_ids_to_process' => ['missing'],
'derivative_suffix' => ':sub:bar',
'expected_dependencies' => [
'foo',
'bar',
'baz',
],
],
'Multiple matching array key' => [
'dependencies' => [
'foo',
'bar',
'baz',
],
'dependency_ids_to_process' => ['foo', 'bar', 'missing'],
'derivative_suffix' => ':sub:bar',
'expected_dependencies' => [
'foo:sub:bar',
'bar:sub:bar',
'baz',
],
],
'Tricky' => [
'dependencies' => [
'foo',
'foo_bar',
'baz',
],
'dependency_ids_to_process' => ['foo_bar'],
'derivative_suffix' => '_baz',
'expected_dependencies' => [
'foo',
'foo_bar_baz',
'baz',
],
],
];
}
}
