You are here

class RangeFieldTest in Range 8

Same name in this branch
  1. 8 tests/src/Unit/Plugin/migrate/process/d6/RangeFieldTest.php \Drupal\Tests\range\Unit\Plugin\migrate\process\d6\RangeFieldTest
  2. 8 tests/src/Unit/Plugin/migrate/field/d6/RangeFieldTest.php \Drupal\Tests\range\Unit\Plugin\migrate\field\d6\RangeFieldTest
  3. 8 tests/src/Unit/Plugin/migrate/field/d7/RangeFieldTest.php \Drupal\Tests\range\Unit\Plugin\migrate\field\d7\RangeFieldTest

@coversDefaultClass \Drupal\range\Plugin\migrate\field\d7\RangeField @group range

Hierarchy

Expanded class hierarchy of RangeFieldTest

File

tests/src/Unit/Plugin/migrate/field/d7/RangeFieldTest.php, line 15

Namespace

Drupal\Tests\range\Unit\Plugin\migrate\field\d7
View source
class RangeFieldTest extends UnitTestCase {
  protected const PLUGIN_DEFINITION = [
    'id' => 'd7_range',
    'core' => [
      7,
    ],
    'type_map' => [
      'range_integer' => 'range_integer',
      'range_decimal' => 'range_decimal',
      'range_float' => 'range_float',
    ],
    'source_module' => 'range',
    'destination_module' => 'range',
  ];

  /**
   * Current migration row.
   *
   * @var \Drupal\migrate\Row|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $row;

  /**
   * Migration source.
   *
   * @var \Drupal\migrate\Plugin\MigrateSourceInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $migrationSource;

  /**
   * Migration itself.
   *
   * @var \Drupal\migrate\Plugin\Migration|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $migration;

  /**
   * RangeField migration plugin.
   *
   * @var \Drupal\range\Plugin\migrate\field\d7\RangeField
   */
  protected $plugin;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->row = $this
      ->createMock(Row::class);
    $this->migrationSource = $this
      ->createMock(MigrateSourceInterface::class);
    $this->migrationSource
      ->expects(self::once())
      ->method('current')
      ->willReturn($this->row);
    $this->migration = $this
      ->createPartialMock(Migration::class, [
      'getSourcePlugin',
    ]);
    $this->migration
      ->expects(self::once())
      ->method('getSourcePlugin')
      ->willReturn($this->migrationSource);
    $this->plugin = new RangeField([], 'range', self::PLUGIN_DEFINITION);
  }

  /**
   * @covers ::alterFieldWidgetMigration
   * @dataProvider alterMigrationDataProvider
   */
  public function testAlterFieldWidgetMigration($field_type, $is_range_field_type) {
    $this->row
      ->expects(self::once())
      ->method('getSourceProperty')
      ->willReturn($field_type);
    $this->plugin
      ->alterFieldWidgetMigration($this->migration);
    $process = $this->migration
      ->getProcess();
    if ($is_range_field_type) {
      $this
        ->assertSame([
        'range' => 'range',
      ], $process['options/type']['type']['map']);
      $expected_process = [
        'plugin' => 'd7_range_field_instance_widget_settings',
        'source' => 'settings',
      ];
      $this
        ->assertSame([
        $expected_process,
      ], $process['options/settings']);
    }
    else {
      $this
        ->assertArrayNotHasKey('options/settings', $process);
    }
  }

  /**
   * @covers ::alterFieldInstanceMigration
   * @dataProvider alterMigrationDataProvider
   */
  public function testAlterFieldInstanceMigration($field_type, $is_range_field_type) {
    $this->row
      ->expects(self::once())
      ->method('getSourceProperty')
      ->willReturn($field_type);
    $this->plugin
      ->alterFieldInstanceMigration($this->migration);
    $process = $this->migration
      ->getProcess();
    if ($is_range_field_type) {
      $expected_process = [
        'plugin' => 'd7_range_field_instance_settings',
      ];
      $this
        ->assertSame([
        $expected_process,
      ], $process['settings']);
    }
    else {
      $this
        ->assertSame([], $process);
    }
  }

  /**
   * Data provider.
   */
  public function alterMigrationDataProvider() {
    return [
      'not range field' => [
        'link',
        FALSE,
      ],
      'range integer field' => [
        'range_integer',
        TRUE,
      ],
      'range decimal field' => [
        'range_decimal',
        TRUE,
      ],
      'range float field' => [
        'range_float',
        TRUE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RangeFieldTest::$migration protected property Migration itself.
RangeFieldTest::$migrationSource protected property Migration source.
RangeFieldTest::$plugin protected property RangeField migration plugin.
RangeFieldTest::$row protected property Current migration row.
RangeFieldTest::alterMigrationDataProvider public function Data provider.
RangeFieldTest::PLUGIN_DEFINITION protected constant
RangeFieldTest::setUp protected function Overrides UnitTestCase::setUp
RangeFieldTest::testAlterFieldInstanceMigration public function @covers ::alterFieldInstanceMigration @dataProvider alterMigrationDataProvider
RangeFieldTest::testAlterFieldWidgetMigration public function @covers ::alterFieldWidgetMigration @dataProvider alterMigrationDataProvider
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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.