function QueryBatchTest::testQueryBatch

Same name in other branches
  1. 9 core/modules/migrate/tests/src/Kernel/QueryBatchTest.php \Drupal\Tests\migrate\Kernel\QueryBatchTest::testQueryBatch()
  2. 10 core/modules/migrate/tests/src/Kernel/QueryBatchTest.php \Drupal\Tests\migrate\Kernel\QueryBatchTest::testQueryBatch()
  3. 11.x core/modules/migrate/tests/src/Kernel/QueryBatchTest.php \Drupal\Tests\migrate\Kernel\QueryBatchTest::testQueryBatch()

Tests query batch size.

@dataProvider queryDataProvider

Parameters

array $source_data: The source data, keyed by table name. Each table is an array containing the rows in that table.

array $expected_data: The result rows the plugin is expected to return.

int $num_rows: How many rows the source plugin is expected to return.

array $configuration: Configuration for the source plugin specifying the batch size.

int $expected_batch_size: The expected batch size, will be set to zero for invalid batch sizes.

int $expected_batch_count: The total number of batches.

File

core/modules/migrate/tests/src/Kernel/QueryBatchTest.php, line 151

Class

QueryBatchTest
Tests query batching.

Namespace

Drupal\Tests\migrate\Kernel

Code

public function testQueryBatch($source_data, $expected_data, $num_rows, $configuration, $expected_batch_size, $expected_batch_count) {
    $plugin = $this->getPlugin($configuration);
    // Since we don't yet inject the database connection, we need to use a
    // reflection hack to set it in the plugin instance.
    $reflector = new \ReflectionObject($plugin);
    $property = $reflector->getProperty('database');
    $property->setAccessible(TRUE);
    $connection = $this->getDatabase($source_data);
    $property->setValue($plugin, $connection);
    // Test the results.
    $i = 0;
    
    /** @var \Drupal\migrate\Row $row */
    foreach ($plugin as $row) {
        $expected = $expected_data[$i++];
        $actual = $row->getSource();
        foreach ($expected as $key => $value) {
            $this->assertArrayHasKey($key, $actual);
            $this->assertSame((string) $value, (string) $actual[$key]);
        }
    }
    // Test that all rows were retrieved.
    self::assertSame($num_rows, $i);
    // Test the batch size.
    if (is_null($expected_batch_size)) {
        $expected_batch_size = $configuration['batch_size'];
    }
    $property = $reflector->getProperty('batchSize');
    $property->setAccessible(TRUE);
    self::assertSame($expected_batch_size, $property->getValue($plugin));
    // Test the batch count.
    if (is_null($expected_batch_count)) {
        $expected_batch_count = intdiv($num_rows, $expected_batch_size);
        if ($num_rows % $configuration['batch_size']) {
            $expected_batch_count++;
        }
    }
    $property = $reflector->getProperty('batch');
    $property->setAccessible(TRUE);
    self::assertSame($expected_batch_count, $property->getValue($plugin));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.