Same name in this branch
  1. 10 core/modules/migrate/tests/src/Kernel/SqlBaseTest.php \Drupal\Tests\migrate\Kernel\SqlBaseTest
  2. 10 core/modules/migrate/tests/src/Unit/SqlBaseTest.php \Drupal\Tests\migrate\Unit\SqlBaseTest
  3. 10 core/modules/views/tests/src/Unit/Plugin/pager/SqlBaseTest.php \Drupal\Tests\views\Unit\Plugin\pager\SqlBaseTest
Same name and namespace in other branches
  1. 8.9.x core/modules/migrate/tests/src/Unit/SqlBaseTest.php \Drupal\Tests\migrate\Unit\SqlBaseTest
  2. 9 core/modules/migrate/tests/src/Unit/SqlBaseTest.php \Drupal\Tests\migrate\Unit\SqlBaseTest

Tests the SqlBase class.

@group migrate

Hierarchy

Expanded class hierarchy of SqlBaseTest

File

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

Namespace

Drupal\Tests\migrate\Unit
View source
class SqlBaseTest extends UnitTestCase {

  /**
   * Tests that the ID map is joinable.
   *
   * @param bool $expected_result
   *   The expected result.
   * @param bool $id_map_is_sql
   *   TRUE if we want getIdMap() to return an instance of Sql.
   * @param bool $with_id_map
   *   TRUE if we want the ID map to have a valid map of IDs.
   * @param array $source_options
   *   (optional) An array of connection options for the source connection.
   *   Defaults to an empty array.
   * @param array $id_map_options
   *   (optional) An array of connection options for the ID map connection.
   *   Defaults to an empty array.
   *
   * @dataProvider sqlBaseTestProvider
   */
  public function testMapJoinable($expected_result, $id_map_is_sql, $with_id_map, $source_options = [], $id_map_options = []) {

    // Setup a connection object.
    $source_connection = $this
      ->getMockBuilder('Drupal\\Core\\Database\\Connection')
      ->disableOriginalConstructor()
      ->getMock();
    $source_connection
      ->expects($id_map_is_sql && $with_id_map ? $this
      ->once() : $this
      ->never())
      ->method('getConnectionOptions')
      ->willReturn($source_options);

    // Setup the ID map connection.
    $id_map_connection = $this
      ->getMockBuilder('Drupal\\Core\\Database\\Connection')
      ->disableOriginalConstructor()
      ->getMock();
    $id_map_connection
      ->expects($id_map_is_sql && $with_id_map ? $this
      ->once() : $this
      ->never())
      ->method('getConnectionOptions')
      ->willReturn($id_map_options);

    // Setup the Sql object.
    $sql = $this
      ->getMockBuilder('Drupal\\migrate\\Plugin\\migrate\\id_map\\Sql')
      ->disableOriginalConstructor()
      ->getMock();
    $sql
      ->expects($id_map_is_sql && $with_id_map ? $this
      ->once() : $this
      ->never())
      ->method('getDatabase')
      ->willReturn($id_map_connection);

    // Setup a migration entity.
    $migration = $this
      ->createMock(MigrationInterface::class);
    $migration
      ->expects($with_id_map ? $this
      ->once() : $this
      ->never())
      ->method('getIdMap')
      ->willReturn($id_map_is_sql ? $sql : NULL);

    // Create our SqlBase test class.
    $sql_base = new TestSqlBase();
    $sql_base
      ->setMigration($migration);
    $sql_base
      ->setDatabase($source_connection);

    // Configure the idMap to make the check in mapJoinable() pass.
    if ($with_id_map) {
      $sql_base
        ->setIds([
        'uid' => [
          'type' => 'integer',
          'alias' => 'u',
        ],
      ]);
    }
    $this
      ->assertEquals($expected_result, $sql_base
      ->mapJoinable());
  }

  /**
   * The data provider for SqlBase.
   *
   * @return array
   *   An array of data per test run.
   */
  public static function sqlBaseTestProvider() {
    return [
      // Source ids are empty so mapJoinable() is false.
      [
        FALSE,
        FALSE,
        FALSE,
      ],
      // Still false because getIdMap() is not a subclass of Sql.
      [
        FALSE,
        FALSE,
        TRUE,
      ],
      // Test mapJoinable() returns false when source and id connection options
      // differ.
      [
        FALSE,
        TRUE,
        TRUE,
        [
          'driver' => 'mysql',
          'username' => 'different_from_map',
          'password' => 'different_from_map',
        ],
        [
          'driver' => 'mysql',
          'username' => 'different_from_source',
          'password' => 'different_from_source',
        ],
      ],
      // Returns false because driver is pgsql and the databases are not the
      // same.
      [
        FALSE,
        TRUE,
        TRUE,
        [
          'driver' => 'pgsql',
          'database' => '1.pgsql',
          'username' => 'same_value',
          'password' => 'same_value',
        ],
        [
          'driver' => 'pgsql',
          'database' => '2.pgsql',
          'username' => 'same_value',
          'password' => 'same_value',
        ],
      ],
      // Returns false because driver is sqlite and the databases are not the
      // same.
      [
        FALSE,
        TRUE,
        TRUE,
        [
          'driver' => 'sqlite',
          'database' => '1.sqlite',
          'username' => '',
          'password' => '',
        ],
        [
          'driver' => 'sqlite',
          'database' => '2.sqlite',
          'username' => '',
          'password' => '',
        ],
      ],
      // Returns false because driver is not the same.
      [
        FALSE,
        TRUE,
        TRUE,
        [
          'driver' => 'pgsql',
          'username' => 'same_value',
          'password' => 'same_value',
        ],
        [
          'driver' => 'mysql',
          'username' => 'same_value',
          'password' => 'same_value',
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
SqlBaseTest::sqlBaseTestProvider public static function The data provider for SqlBase.
SqlBaseTest::testMapJoinable public function Tests that the ID map is joinable.
UnitTestCase::$root protected property The app root. 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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 305
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function