View source
<?php
namespace Drupal\Tests\migrate\Kernel\Plugin\id_map;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Tests\migrate\Kernel\MigrateTestBase;
use Drupal\Tests\migrate\Unit\TestSqlIdMap;
use Drupal\migrate\MigrateException;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class SqlTest extends MigrateTestBase {
protected $database;
protected $eventDispatcher;
protected $migrationDefinition;
protected $migrationPluginManager;
public function setUp() : void {
parent::setUp();
$this->database = \Drupal::database();
$this->eventDispatcher = $this
->prophesize(EventDispatcherInterface::class)
->reveal();
$this->migrationPluginManager = \Drupal::service('plugin.manager.migration');
$this->migrationDefinition = [
'id' => 'test',
'source' => [
'plugin' => 'embedded_data',
'data_rows' => [
[
'alpha' => '1',
'bravo' => '2',
'charlie' => '3',
'delta' => '4',
'echo' => '5',
],
],
'ids' => [],
],
'process' => [],
'destination' => [
'plugin' => 'null',
],
];
}
public function testEnsureTables($ids) {
$this->migrationDefinition['source']['ids'] = $ids;
$migration = $this->migrationPluginManager
->createStubMigration($this->migrationDefinition);
$map = new TestSqlIdMap($this->database, [], 'test', [], $migration, $this->eventDispatcher);
$map
->ensureTables();
$exists = $this->database
->schema()
->tableExists('migrate_map_test');
$this
->assertTrue($exists);
}
public function providerTestEnsureTables() {
return [
'no ids' => [
[],
],
'one id' => [
[
'alpha' => [
'type' => 'string',
],
],
],
'too many' => [
[
'alpha' => [
'type' => 'string',
],
'bravo' => [
'type' => 'string',
],
'charlie' => [
'type' => 'string',
],
'delta' => [
'type' => 'string',
],
'echo ' => [
'type' => 'string',
],
],
],
];
}
public function testFailEnsureTables($ids) {
if (Database::getConnection()
->databaseType() !== 'mysql') {
$this
->markTestSkipped("This test only runs for MySQL");
}
$this->migrationDefinition['source']['ids'] = $ids;
$migration = $this->container
->get('plugin.manager.migration')
->createStubMigration($this->migrationDefinition);
$map = new SqlIdMapTest($this->database, [], 'test', [], $migration, $this->eventDispatcher);
$this
->expectException(DatabaseExceptionWrapper::class);
$this
->expectExceptionMessage("Syntax error or access violation: 1074 Column length too big for column 'sourceid1' (max = 16383); use BLOB or TEXT instead:");
$map
->ensureTables();
}
public function providerTestFailEnsureTables() {
return [
'one id' => [
[
'alpha' => [
'type' => 'string',
],
],
],
];
}
}
class SqlIdMapTest extends TestSqlIdMap implements \Iterator {
protected function getFieldSchema(array $id_definition) {
if (!isset($id_definition['type'])) {
return [];
}
switch ($id_definition['type']) {
case 'integer':
return [
'type' => 'int',
'not null' => TRUE,
];
case 'string':
return [
'type' => 'varchar',
'length' => 65536,
'not null' => FALSE,
];
default:
throw new MigrateException($id_definition['type'] . ' not supported');
}
}
}