You are here

class NullCoalesceTest in Drupal 9

Same name and namespace in other branches
  1. 8 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 15

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() {
    $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) {
    $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 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) {
    $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 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

Namesort descending Modifiers Type Description Overrides
MigrateProcessTestCase::$migrateExecutable protected property
MigrateProcessTestCase::$plugin protected property
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. 7
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 function Provides Data for ::testTransform.
NullCoalesceTest::transformWithDefaultProvider public function Provides Data for ::testTransformWithDefault.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function