class ExplodeTest

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

Tests the Explode process plugin.

@group migrate

Hierarchy

Expanded class hierarchy of ExplodeTest

File

core/modules/migrate/tests/src/Unit/process/ExplodeTest.php, line 16

Namespace

Drupal\Tests\migrate\Unit\process
View source
class ExplodeTest extends MigrateProcessTestCase {
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        $configuration = [
            'delimiter' => ',',
        ];
        $this->plugin = new Explode($configuration, 'map', []);
        parent::setUp();
    }
    
    /**
     * Tests explode transform process works.
     */
    public function testTransform() : void {
        $value = $this->plugin
            ->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destination_property');
        $this->assertSame([
            'foo',
            'bar',
            'tik',
        ], $value);
    }
    
    /**
     * Tests explode transform process works with a limit.
     */
    public function testTransformLimit() : void {
        $plugin = new Explode([
            'delimiter' => '_',
            'limit' => 2,
        ], 'map', []);
        $value = $plugin->transform('foo_bar_tik', $this->migrateExecutable, $this->row, 'destination_property');
        $this->assertSame([
            'foo',
            'bar_tik',
        ], $value);
    }
    
    /**
     * Tests if the explode process can be chained with handles_multiple process.
     */
    public function testChainedTransform() : void {
        $exploded = $this->plugin
            ->transform('One,Two,Three', $this->migrateExecutable, $this->row, 'destination_property');
        $concat = new Concat([], 'map', []);
        $concatenated = $concat->transform($exploded, $this->migrateExecutable, $this->row, 'destination_property');
        $this->assertSame('OneTwoThree', $concatenated);
    }
    
    /**
     * Tests explode fails properly on non-strings.
     */
    public function testExplodeWithNonString() : void {
        $this->expectException(MigrateException::class);
        $this->expectExceptionMessage('is not a string');
        $this->plugin
            ->transform([
            'foo',
        ], $this->migrateExecutable, $this->row, 'destination_property');
    }
    
    /**
     * Tests that explode works on non-strings but with strict set to FALSE.
     *
     * @dataProvider providerExplodeWithNonStrictAndEmptySource
     */
    public function testExplodeWithNonStrictAndEmptySource($value, $expected) : void {
        $plugin = new Explode([
            'delimiter' => '|',
            'strict' => FALSE,
        ], 'map', []);
        $processed = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destination_property');
        $this->assertSame($expected, $processed);
    }
    
    /**
     * Data provider for ::testExplodeWithNonStrictAndEmptySource().
     */
    public static function providerExplodeWithNonStrictAndEmptySource() {
        return [
            'normal_string' => [
                'a|b|c',
                [
                    'a',
                    'b',
                    'c',
                ],
            ],
            'integer_cast_to_string' => [
                123,
                [
                    '123',
                ],
            ],
            'zero_integer_cast_to_string' => [
                0,
                [
                    '0',
                ],
            ],
            'true_cast_to_string' => [
                TRUE,
                [
                    '1',
                ],
            ],
            'null_empty_array' => [
                NULL,
                [],
            ],
            'false_empty_array' => [
                FALSE,
                [],
            ],
            'empty_string_empty_array' => [
                '',
                [],
            ],
        ];
    }
    
    /**
     * Tests Explode exception handling when string-cast fails.
     */
    public function testExplodeWithNonStrictAndNonCastable() : void {
        $plugin = new Explode([
            'delimiter' => '|',
            'strict' => FALSE,
        ], 'map', []);
        $this->expectException(MigrateException::class);
        $this->expectExceptionMessage('cannot be casted to a string');
        $processed = $plugin->transform([
            'foo',
        ], $this->migrateExecutable, $this->row, 'destination_property');
        $this->assertSame([
            'foo',
        ], $processed);
    }
    
    /**
     * Tests Explode return values with an empty string and strict check.
     */
    public function testExplodeWithStrictAndEmptyString() : void {
        $plugin = new Explode([
            'delimiter' => '|',
        ], 'map', []);
        $processed = $plugin->transform('', $this->migrateExecutable, $this->row, 'destination_property');
        $this->assertSame([
            '',
        ], $processed);
    }
    
    /**
     * Tests explode fails with empty delimiter.
     */
    public function testExplodeWithEmptyDelimiter() : void {
        $this->expectException(MigrateException::class);
        $this->expectExceptionMessage('delimiter is empty');
        $plugin = new Explode([
            'delimiter' => '',
        ], 'map', []);
        $plugin->transform('foo,bar', $this->migrateExecutable, $this->row, 'destination_property');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
ExplodeTest::providerExplodeWithNonStrictAndEmptySource public static function Data provider for ::testExplodeWithNonStrictAndEmptySource().
ExplodeTest::setUp protected function Overrides MigrateProcessTestCase::setUp
ExplodeTest::testChainedTransform public function Tests if the explode process can be chained with handles_multiple process.
ExplodeTest::testExplodeWithEmptyDelimiter public function Tests explode fails with empty delimiter.
ExplodeTest::testExplodeWithNonStrictAndEmptySource public function Tests that explode works on non-strings but with strict set to FALSE.
ExplodeTest::testExplodeWithNonStrictAndNonCastable public function Tests Explode exception handling when string-cast fails.
ExplodeTest::testExplodeWithNonString public function Tests explode fails properly on non-strings.
ExplodeTest::testExplodeWithStrictAndEmptyString public function Tests Explode return values with an empty string and strict check.
ExplodeTest::testTransform public function Tests explode transform process works.
ExplodeTest::testTransformLimit public function Tests explode transform process works with a limit.
MigrateProcessTestCase::$migrateExecutable protected property The migration executable or a mock.
MigrateProcessTestCase::$plugin protected property The migration process plugin. 1
MigrateProcessTestCase::$row protected property A mock of a process row.
MigrateTestCase::$idMap protected property The migration ID map.
MigrateTestCase::$migrationConfiguration protected property An array of migration configuration values. 10
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.
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::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::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.

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