You are here

class GateTest in Migrate Plus 8.5

Tests the gate process plugin.

@group migrate @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\Gate

Hierarchy

Expanded class hierarchy of GateTest

File

tests/src/Unit/process/GateTest.php, line 17

Namespace

Drupal\Tests\migrate_plus\Unit\process
View source
class GateTest extends MigrateProcessTestCase {

  /**
   * Test Gate plugin.
   *
   * @dataProvider gateProvider
   */
  public function testGate($row_data, $destination_data, $configuration, $message) : void {
    $row = new Row($row_data);
    if (!empty($destination_data)) {
      foreach ($destination_data as $key => $val) {
        $row
          ->setDestinationProperty($key, $val);
      }
    }
    if (!empty($message)) {
      $this
        ->expectException(MigrateSkipProcessException::class);
      $this
        ->expectExceptionMessage($message);
    }
    $plugin = new Gate($configuration, 'gate', []);
    $value = $row_data[$configuration['source']];
    $transformed = $plugin
      ->transform($value, $this->migrateExecutable, $row, 'destinationproperty');
    if (empty($message)) {
      $this
        ->assertSame($value, $transformed);
    }
  }

  /**
   * Row and plugin configuration for tests.
   *
   * @return array
   */
  public function gateProvider() {
    return [
      'Gate does not unlock' => [
        [
          'state_abbr' => 'MO',
          'source_data' => 'Let me through!',
        ],
        NULL,
        [
          'source' => 'source_data',
          'use_as_key' => 'state_abbr',
          'valid_keys' => 'CO',
          'key_direction' => 'unlock',
        ],
        'Processing of destination property destinationproperty was skipped: Gate was not unlocked by property state_abbr with value MO.',
      ],
      'Gate unlocks (with valid_keys array)' => [
        [
          'state_abbr' => 'Colorado',
          'source_data' => 'Let me through!',
        ],
        NULL,
        [
          'source' => 'source_data',
          'use_as_key' => 'state_abbr',
          'valid_keys' => [
            'CO',
            'Colorado',
          ],
          'key_direction' => 'unlock',
        ],
        NULL,
      ],
      'Gate locks' => [
        [
          'state_abbr' => 'CO',
          'source_data' => 'Let me through!',
        ],
        NULL,
        [
          'source' => 'source_data',
          'use_as_key' => 'state_abbr',
          'valid_keys' => 'CO',
          'key_direction' => 'lock',
        ],
        'Processing of destination property destinationproperty was skipped: Gate was locked by property state_abbr with value CO.',
      ],
      'Gate stays unlocked' => [
        [
          'state_abbr' => 'MO',
          'source_data' => 'Let me through!',
        ],
        NULL,
        [
          'source' => 'source_data',
          'use_as_key' => 'state_abbr',
          'valid_keys' => 'CO',
          'key_direction' => 'lock',
        ],
        NULL,
      ],
      'Destination prop does not unlock gate' => [
        [
          'source_data' => 'Let me through!',
        ],
        [
          'state_abbr' => 'MO',
        ],
        [
          'source' => 'source_data',
          'use_as_key' => '@state_abbr',
          'valid_keys' => 'CO',
          'key_direction' => 'unlock',
        ],
        'Processing of destination property destinationproperty was skipped: Gate was not unlocked by property @state_abbr with value MO.',
      ],
      'Destination prop unlocks gate' => [
        [
          'source_data' => 'Let me through!',
        ],
        [
          'state_abbr' => 'CO',
        ],
        [
          'source' => 'source_data',
          'use_as_key' => '@state_abbr',
          'valid_keys' => 'CO',
          'key_direction' => 'unlock',
        ],
        NULL,
      ],
      'Destination prop locks gate' => [
        [
          'source_data' => 'Let me through!',
        ],
        [
          'state_abbr' => 'CO',
        ],
        [
          'source' => 'source_data',
          'use_as_key' => '@state_abbr',
          'valid_keys' => 'CO',
          'key_direction' => 'lock',
        ],
        'Processing of destination property destinationproperty was skipped: Gate was locked by property @state_abbr with value CO.',
      ],
      'Gate stays unlocked with destination prop' => [
        [
          'source_data' => 'Let me through!',
        ],
        [
          'state_abbr' => 'MO',
        ],
        [
          'source' => 'source_data',
          'use_as_key' => '@state_abbr',
          'valid_keys' => 'CO',
          'key_direction' => 'lock',
        ],
        NULL,
      ],
    ];
  }

  /**
   * Test Gate plugin with bad configuration.
   *
   * @dataProvider badConfigurationProvider
   */
  public function testGateBadConfigration($configuration, $message) : void {
    $this
      ->expectException(MigrateException::class);
    $this
      ->expectExceptionMessage($message);
    $plugin = new Gate($configuration, 'gate', []);
    $plugin
      ->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
  }

  /**
   * Provider for bad configuration.
   *
   * @return array
   */
  public function badConfigurationProvider() {
    return [
      'Missing use_as_key' => [
        [
          'source' => 'source_data',
          'valid_keys' => 'CO',
          'key_direction' => 'unlock',
        ],
        'Gate plugin is missing use_as_key configuration.',
      ],
      'Missing valid_keys' => [
        [
          'source' => 'source_data',
          'use_as_key' => 'state_abbr',
          'key_direction' => 'unlock',
        ],
        'Gate plugin is missing valid_keys configuration.',
      ],
      'Missing key_direction' => [
        [
          'source' => 'source_data',
          'use_as_key' => 'state_abbr',
          'valid_keys' => 'CO',
        ],
        'Gate plugin is missing key_direction configuration.',
      ],
      'Invalid key_direction' => [
        [
          'source' => 'source_data',
          'use_as_key' => 'state_abbr',
          'valid_keys' => 'CO',
          'key_direction' => 'open',
        ],
        'Gate plugin only accepts the following values for key_direction: lock and unlock.',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GateTest::badConfigurationProvider public function Provider for bad configuration.
GateTest::gateProvider public function Row and plugin configuration for tests.
GateTest::testGate public function Test Gate plugin.
GateTest::testGateBadConfigration public function Test Gate plugin with bad configuration.
MigrateProcessTestCase::$migrateExecutable protected property
MigrateProcessTestCase::$plugin protected property
MigrateProcessTestCase::$row protected property
MigrateProcessTestCase::setUp protected function Overrides UnitTestCase::setUp 19
MigrateTestCase::$idMap protected property The migration ID map.
MigrateTestCase::$migrationConfiguration protected property An array of migration configuration values. 16
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. 1
MigrateTestCase::getValue protected function Gets the value on a row for a given key. 1
MigrateTestCase::queryResultTest public function Tests a query.
MigrateTestCase::retrievalAssertHelper protected function Asserts tested values during test retrieval.
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.
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.