class SubProcessTest

Same name and namespace in other branches
  1. 9 core/modules/migrate/tests/src/Unit/process/SubProcessTest.php \Drupal\Tests\migrate\Unit\process\SubProcessTest
  2. 8.9.x core/modules/migrate/tests/src/Unit/process/SubProcessTest.php \Drupal\Tests\migrate\Unit\process\SubProcessTest
  3. 10 core/modules/migrate/tests/src/Unit/process/SubProcessTest.php \Drupal\Tests\migrate\Unit\process\SubProcessTest

Tests the sub_process process plugin.

@group migrate

Hierarchy

Expanded class hierarchy of SubProcessTest

File

core/modules/migrate/tests/src/Unit/process/SubProcessTest.php, line 22

Namespace

Drupal\Tests\migrate\Unit\process
View source
class SubProcessTest extends MigrateProcessTestCase {
    
    /**
     * The sub_process plugin being tested.
     *
     * @var \Drupal\migrate\Plugin\migrate\process\SubProcess
     */
    protected $plugin;
    
    /**
     * @var array
     */
    protected $migrationConfiguration = [
        'id' => 'test',
    ];
    
    /**
     * Tests the sub_process process plugin.
     *
     * @dataProvider providerTestSubProcess
     */
    public function testSubProcess($process_configuration, $source_values = []) : void {
        $migration = $this->getMigration();
        // Set up the properties for the sub_process.
        $plugin = new SubProcess($process_configuration, 'sub_process', []);
        // Manually create the plugins. Migration::getProcessPlugins does this
        // normally but the plugin system is not available.
        foreach ($process_configuration['process'] as $destination => $source) {
            $sub_process_plugins[$destination][] = new Get([
                'source' => $source,
            ], 'get', []);
        }
        // Set up the key plugins.
        $key_plugin['key'][] = new Get([
            'source' => '@id',
        ], 'get', []);
        $migration->expects($this->exactly(2))
            ->method('getProcessPlugins')
            ->willReturnOnConsecutiveCalls($sub_process_plugins, $key_plugin);
        $event_dispatcher = $this->createMock(EventDispatcherInterface::class);
        $migrate_executable = new MigrateExecutable($migration, $this->createMock(MigrateMessageInterface::class), $event_dispatcher);
        // The current value of the pipeline.
        $current_value = [
            [
                'source_foo' => 'test',
                'source_id' => 42,
            ] + $source_values,
        ];
        // This is not used but the interface requires it, so create an empty row.
        $row = new Row($source_values);
        // After transformation, check to make sure that source_foo and source_id's
        // values ended up in the proper destinations, and that the value of the
        // key (@id) is the same as the destination ID (42).
        $new_value = $plugin->transform($current_value, $migrate_executable, $row, 'test');
        $this->assertCount(1, $new_value);
        $this->assertSameSize($process_configuration['process'], $new_value[42]);
        $this->assertSame('test', $new_value[42]['foo']);
        if ($source_values) {
            $this->assertSame('source_baz', $new_value[42]['baaa']);
        }
        $this->assertSame(42, $new_value[42]['id']);
    }
    
    /**
     * Data provider for testSubProcess().
     */
    public static function providerTestSubProcess() {
        return [
            'no source context' => [
                'process_configuration' => [
                    'process' => [
                        'foo' => 'source_foo',
                        'id' => 'source_id',
                    ],
                    'key' => '@id',
                ],
            ],
            'default source key' => [
                'process_configuration' => [
                    'process' => [
                        'foo' => 'source_foo',
                        'id' => 'source_id',
                        'baaa' => 'source/baf',
                    ],
                    'key' => '@id',
                    'include_source' => TRUE,
                ],
                'source_values' => [
                    'baf' => 'source_baz',
                ],
            ],
            'renamed source key' => [
                'process_configuration' => [
                    'process' => [
                        'foo' => 'source_foo',
                        'id' => 'source_id',
                        'baaa' => 'my_source/baf',
                    ],
                    'key' => '@id',
                    'include_source' => TRUE,
                    'source_key' => 'my_source',
                ],
                'source_values' => [
                    'baf' => 'source_baz',
                ],
            ],
        ];
    }
    
    /**
     * Tests the sub_process process plugin.
     *
     * @dataProvider providerTestNotFoundSubProcess
     */
    public function testNotFoundSubProcess($process_configuration, $source_values = []) : void {
        $migration = $this->getMigration();
        // Set up the properties for the sub_process.
        $plugin = new SubProcess($process_configuration, 'sub_process', []);
        // Manually create the plugins. Migration::getProcessPlugins does this
        // normally but the plugin system is not available.
        foreach ($process_configuration['process'] as $destination => $source) {
            $sub_process_plugins[$destination][] = new Get([
                'source' => $source,
            ], 'get', []);
        }
        $key_plugin['key'][] = new Get([
            'source' => '@id',
        ], 'get', []);
        $migration->expects($this->exactly(2))
            ->method('getProcessPlugins')
            ->willReturnOnConsecutiveCalls($sub_process_plugins, $key_plugin);
        $event_dispatcher = $this->createMock(EventDispatcherInterface::class);
        $migrate_executable = new MigrateExecutable($migration, $this->createMock(MigrateMessageInterface::class), $event_dispatcher);
        // The current value of the pipeline.
        $current_value = [
            [
                'source_foo' => 'test',
                'source_id' => NULL,
            ] + $source_values,
        ];
        // This is not used but the interface requires it, so create an empty row.
        $row = new Row($source_values);
        // After transformation, check to make sure that source_foo and source_id's
        // values ended up in the proper destinations, and that the value of the
        // key (@id) is the same as the destination ID (42).
        $new_value = $plugin->transform($current_value, $migrate_executable, $row, 'test');
        $this->assertSame([], $new_value);
    }
    
    /**
     * Data provider for testNotFoundSubProcess().
     */
    public static function providerTestNotFoundSubProcess() {
        return [
            'no key' => [
                'process_configuration' => [
                    'process' => [
                        'foo' => 'source_foo',
                    ],
                    'key' => '@id',
                ],
            ],
            'lookup returns NULL' => [
                'process_configuration' => [
                    'process' => [
                        'foo' => 'source_foo',
                        'id' => 'source_id',
                    ],
                    'key' => '@id',
                ],
            ],
        ];
    }
    
    /**
     * Tests behavior when source children are not arrays.
     *
     * @dataProvider providerTestSourceNotArray
     */
    public function testSourceNotArray($source_values, $type) : void {
        $process = new SubProcess([
            'process' => [
                'foo' => 'source_foo',
            ],
        ], 'sub_process', []);
        $this->expectException(MigrateException::class);
        $this->expectExceptionMessage("Input array should hold elements of type array, instead element was of type '{$type}'");
        $process->transform($source_values, $this->migrateExecutable, $this->row, 'destination_property');
    }
    
    /**
     * Data provider for testSourceNotArray().
     */
    public static function providerTestSourceNotArray() {
        return [
            'strings cannot be subprocess items' => [
                [
                    'strings',
                    'cannot',
                    'be',
                    'children',
                ],
                'string',
            ],
            'xml elements cannot be subprocess items' => [
                [
                    new \SimpleXMLElement("<element>Content</element>"),
                ],
                'object',
            ],
            'integers cannot be subprocess items' => [
                [
                    1,
                    2,
                    3,
                    4,
                ],
                'integer',
            ],
            'booleans cannot be subprocess items' => [
                [
                    TRUE,
                    FALSE,
                ],
                'boolean',
            ],
            'null cannot be subprocess items' => [
                [
                    NULL,
                ],
                'NULL',
            ],
            'iterator cannot be subprocess items' => [
                [
                    new \ArrayIterator([
                        'some',
                        'array',
                    ]),
                ],
                'object',
            ],
            'all subprocess items must be arrays' => [
                [
                    [
                        'array',
                    ],
                    'not array',
                ],
                'string',
            ],
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
MigrateProcessTestCase::$migrateExecutable protected property
MigrateProcessTestCase::$row protected property
MigrateProcessTestCase::setUp protected function Overrides UnitTestCase::setUp 17
MigrateTestCase::$idMap protected property The migration ID map.
MigrateTestCase::$migrationStatus protected property Local store for mocking setStatus()/getStatus().
MigrateTestCase::createSchemaFromRow protected function Generates a table schema from a row.
MigrateTestCase::getDatabase protected function Gets an SQLite database connection object for use in tests.
MigrateTestCase::getMigration protected function Retrieves a mocked migration.
MigrateTestCase::getValue protected function Gets the value on a row for a given key.
MigrateTestCase::queryResultTest public function Tests a query.
MigrateTestCase::retrievalAssertHelper protected function Asserts tested values during test retrieval.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
SubProcessTest::$migrationConfiguration protected property Overrides MigrateTestCase::$migrationConfiguration
SubProcessTest::$plugin protected property The sub_process plugin being tested. Overrides MigrateProcessTestCase::$plugin
SubProcessTest::providerTestNotFoundSubProcess public static function Data provider for testNotFoundSubProcess().
SubProcessTest::providerTestSourceNotArray public static function Data provider for testSourceNotArray().
SubProcessTest::providerTestSubProcess public static function Data provider for testSubProcess().
SubProcessTest::testNotFoundSubProcess public function Tests the sub_process process plugin.
SubProcessTest::testSourceNotArray public function Tests behavior when source children are not arrays.
SubProcessTest::testSubProcess public function Tests the sub_process process plugin.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.