You are here

class SqlBaseTest in Zircon Profile 8

Same name in this branch
  1. 8 core/modules/migrate/src/Tests/SqlBaseTest.php \Drupal\migrate\Tests\SqlBaseTest
  2. 8 core/modules/migrate/tests/src/Unit/SqlBaseTest.php \Drupal\Tests\migrate\Unit\SqlBaseTest
Same name and namespace in other branches
  1. 8.0 core/modules/migrate/tests/src/Unit/SqlBaseTest.php \Drupal\Tests\migrate\Unit\SqlBaseTest

Tests the SqlBase class.

@group migrate

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase

Expanded class hierarchy of SqlBaseTest

File

core/modules/migrate/tests/src/Unit/SqlBaseTest.php, line 18
Contains \Drupal\Tests\migrate\Unit\SqlBaseTest.

Namespace

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

  /**
   * @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
   *   An array of connection options for the source connection.
   * @param array $idmap_options
   *   An array of connection options for the id map connection.
   *
   * @dataProvider sqlBaseTestProvider
   */
  public function testMapJoinable($expected_result, $id_map_is_sql, $with_id_map, $source_options = [], $idmap_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.
    $idmap_connection = $this
      ->getMockBuilder('Drupal\\Core\\Database\\Connection')
      ->disableOriginalConstructor()
      ->getMock();
    $idmap_connection
      ->expects($id_map_is_sql && $with_id_map ? $this
      ->once() : $this
      ->never())
      ->method('getConnectionOptions')
      ->willReturn($idmap_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($idmap_connection);

    // Setup a migration entity.
    $migration = $this
      ->getMock('Drupal\\migrate\\Entity\\MigrationInterface');
    $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 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,
        [
          'username' => 'different_from_map',
          'password' => 'different_from_map',
        ],
        [
          'username' => 'different_from_source',
          'password' => 'different_from_source',
        ],
      ],
      // Returns true because source and id map connection options are the same.
      [
        TRUE,
        TRUE,
        TRUE,
        [
          'username' => 'same_value',
          'password' => 'same_value',
        ],
        [
          'username' => 'same_value',
          'password' => 'same_value',
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SqlBaseTest::sqlBaseTestProvider public function The data provider for SqlBase.
SqlBaseTest::testMapJoinable public function @dataProvider sqlBaseTestProvider
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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::setUp protected function 259