class NullCoalesceTest

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

Tests the null_coalesce process plugin.

@group migrate

@coversDefaultClass \Drupal\migrate\Plugin\migrate\process\NullCoalesce

Hierarchy

Expanded class hierarchy of NullCoalesceTest

File

core/modules/migrate/tests/src/Unit/process/NullCoalesceTest.php, line 17

Namespace

Drupal\Tests\migrate\Unit\process
View source
class NullCoalesceTest extends MigrateProcessTestCase {
    
    /**
     * Tests that an exception is thrown for a non-array value.
     *
     * @covers ::transform
     */
    public function testExceptionOnInvalidValue() : void {
        $this->expectException(MigrateException::class);
        (new NullCoalesce([], 'null_coalesce', []))->transform('invalid', $this->migrateExecutable, $this->row, 'destination_property');
    }
    
    /**
     * Tests null_coalesce.
     *
     * @param array $source
     *   The source value.
     * @param mixed $expected_result
     *   The expected result.
     *
     * @covers ::transform
     *
     * @dataProvider transformDataProvider
     *
     * @throws \Drupal\migrate\MigrateException
     */
    public function testTransform(array $source, $expected_result) : void {
        $plugin = new NullCoalesce([], 'null_coalesce', []);
        $result = $plugin->transform($source, $this->migrateExecutable, $this->row, 'destination_property');
        $this->assertSame($expected_result, $result);
    }
    
    /**
     * Provides Data for ::testTransform.
     */
    public static function transformDataProvider() {
        return [
            'all null' => [
                'source' => [
                    NULL,
                    NULL,
                    NULL,
                ],
                'expected_result' => NULL,
            ],
            'false first' => [
                'source' => [
                    FALSE,
                    NULL,
                    NULL,
                ],
                'expected_result' => FALSE,
            ],
            'no null' => [
                'source' => [
                    'test',
                    'test2',
                ],
                'expected_result' => 'test',
            ],
            'string first' => [
                'source' => [
                    'test',
                    NULL,
                    'test2',
                ],
                'expected_result' => 'test',
            ],
            'empty string' => [
                'source' => [
                    NULL,
                    '',
                    NULL,
                ],
                'expected_result' => '',
            ],
            'array' => [
                'source' => [
                    NULL,
                    NULL,
                    [
                        1,
                        2,
                        3,
                    ],
                ],
                'expected_result' => [
                    1,
                    2,
                    3,
                ],
            ],
        ];
    }
    
    /**
     * Tests null_coalesce.
     *
     * @param array $source
     *   The source value.
     * @param string $default_value
     *   The default value.
     * @param mixed $expected_result
     *   The expected result.
     *
     * @covers ::transform
     *
     * @dataProvider transformWithDefaultProvider
     *
     * @throws \Drupal\migrate\MigrateException
     */
    public function testTransformWithDefault(array $source, $default_value, $expected_result) : void {
        $plugin = new NullCoalesce([
            'default_value' => $default_value,
        ], 'null_coalesce', []);
        $result = $plugin->transform($source, $this->migrateExecutable, $this->row, 'destination_property');
        $this->assertSame($expected_result, $result);
    }
    
    /**
     * Provides Data for ::testTransformWithDefault.
     */
    public static function transformWithDefaultProvider() {
        return [
            'default not used' => [
                'source' => [
                    NULL,
                    NULL,
                    'Test',
                    'Test 2',
                ],
                'default_value' => 'default',
                'expected_result' => 'Test',
            ],
            'default string' => [
                'source' => [
                    NULL,
                    NULL,
                ],
                'default_value' => 'default',
                'expected_result' => 'default',
            ],
            'default NULL' => [
                'source' => [
                    NULL,
                    NULL,
                ],
                'default_value' => NULL,
                'expected_result' => NULL,
            ],
        ];
    }

}

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::$plugin protected property 1
MigrateProcessTestCase::$row protected property
MigrateProcessTestCase::setUp protected function Overrides UnitTestCase::setUp 17
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.
NullCoalesceTest::testExceptionOnInvalidValue public function Tests that an exception is thrown for a non-array value.
NullCoalesceTest::testTransform public function Tests null_coalesce.
NullCoalesceTest::testTransformWithDefault public function Tests null_coalesce.
NullCoalesceTest::transformDataProvider public static function Provides Data for ::testTransform.
NullCoalesceTest::transformWithDefaultProvider public static function Provides Data for ::testTransformWithDefault.
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::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.