View source
<?php
namespace Drupal\Tests\migrate\Unit;
use Drupal\Core\Database\Driver\sqlite\Connection;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\Tests\UnitTestCase;
abstract class MigrateTestCase extends UnitTestCase {
protected $migrationConfiguration = [];
protected $idMap;
protected $migrationStatus = MigrationInterface::STATUS_IDLE;
protected function getMigration() {
$this->migrationConfiguration += [
'migrationClass' => 'Drupal\\migrate\\Entity\\Migration',
];
$this->idMap = $this
->getMock('Drupal\\migrate\\Plugin\\MigrateIdMapInterface');
$this->idMap
->method('getQualifiedMapTableName')
->willReturn('test_map');
$migration = $this
->getMockBuilder($this->migrationConfiguration['migrationClass'])
->disableOriginalConstructor()
->getMock();
$migration
->method('checkRequirements')
->willReturn(TRUE);
$migration
->method('getIdMap')
->willReturn($this->idMap);
$migration
->expects($this
->any())
->method('getStatus')
->willReturnCallback(function () {
return $this->migrationStatus;
});
$migration
->expects($this
->any())
->method('setStatus')
->willReturnCallback(function ($status) {
$this->migrationStatus = $status;
});
$migration
->method('getMigrationDependencies')
->willReturn([
'required' => [],
'optional' => [],
]);
$configuration =& $this->migrationConfiguration;
$migration
->method('get')
->willReturnCallback(function ($argument) use (&$configuration) {
return isset($configuration[$argument]) ? $configuration[$argument] : '';
});
$migration
->method('set')
->willReturnCallback(function ($argument, $value) use (&$configuration) {
$configuration[$argument] = $value;
});
$migration
->method('id')
->willReturn($configuration['id']);
return $migration;
}
protected function getDatabase(array $database_contents, $connection_options = []) {
if (extension_loaded('pdo_sqlite')) {
$connection_options['database'] = ':memory:';
$pdo = Connection::open($connection_options);
$connection = new Connection($pdo, $connection_options);
}
else {
$this
->markTestSkipped('The pdo_sqlite extension is not available.');
}
$container = new ContainerBuilder();
$container
->set('module_handler', $this
->getMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface'));
\Drupal::setContainer($container);
foreach (array_filter($database_contents) as $table => $rows) {
$pilot_row = reset($rows);
$connection
->schema()
->createTable($table, $this
->createSchemaFromRow($pilot_row));
$insert = $connection
->insert($table)
->fields(array_keys($pilot_row));
array_walk($rows, [
$insert,
'values',
]);
$insert
->execute();
}
return $connection;
}
protected function createSchemaFromRow(array $row) {
$fields = array_map(function () {
return [
'type' => 'text',
];
}, $row);
return [
'fields' => $fields,
];
}
public function queryResultTest($iter, $expected_results) {
$this
->assertSame(count($expected_results), count($iter), 'Number of results match');
$count = 0;
foreach ($iter as $data_row) {
$expected_row = $expected_results[$count];
$count++;
foreach ($expected_row as $key => $expected_value) {
$this
->retrievalAssertHelper($expected_value, $this
->getValue($data_row, $key), sprintf('Value matches for key "%s"', $key));
}
}
$this
->assertSame(count($expected_results), $count);
}
protected function getValue($row, $key) {
return $row[$key];
}
protected function retrievalAssertHelper($expected_value, $actual_value, $message) {
if (is_array($expected_value)) {
foreach ($expected_value as $k => $v) {
$this
->retrievalAssertHelper($v, $actual_value[$k], $message . '[' . $k . ']');
}
}
else {
$this
->assertSame((string) $expected_value, (string) $actual_value, $message);
}
}
}