MigrateSqlSourceTestCase.php in Drupal 8
File
core/modules/migrate/tests/src/Unit/MigrateSqlSourceTestCase.php
View source
<?php
namespace Drupal\Tests\migrate\Unit;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
abstract class MigrateSqlSourceTestCase extends MigrateTestCase {
protected $source;
protected $databaseContents = [];
const PLUGIN_CLASS = '';
const ORIGINAL_HIGH_WATER = NULL;
protected $expectedResults = [];
protected $expectedCount = 0;
protected $plugin;
protected function setUp() {
$module_handler = $this
->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$state = $this
->createMock('Drupal\\Core\\State\\StateInterface');
$entity_manager = $this
->createMock('Drupal\\Core\\Entity\\EntityManagerInterface');
$key_value = $this
->createMock(KeyValueStoreInterface::class);
$key_value_factory = $this
->createMock(KeyValueFactoryInterface::class);
$key_value_factory
->method('get')
->with('migrate:high_water')
->willReturn($key_value);
try {
$container = \Drupal::getContainer();
} catch (ContainerNotInitializedException $e) {
$container = new ContainerBuilder();
}
$container
->set('keyvalue', $key_value_factory);
\Drupal::setContainer($container);
$migration = $this
->getMigration();
\Drupal::keyValue('migrate:high_water')
->expects($this
->any())
->method('get')
->willReturn(static::ORIGINAL_HIGH_WATER);
$plugin_class = static::PLUGIN_CLASS;
$plugin = new $plugin_class($this->migrationConfiguration['source'], $this->migrationConfiguration['source']['plugin'], [], $migration, $state, $entity_manager);
$plugin_reflection = new \ReflectionClass($plugin);
$database_property = $plugin_reflection
->getProperty('database');
$database_property
->setAccessible(TRUE);
$module_handler_property = $plugin_reflection
->getProperty('moduleHandler');
$module_handler_property
->setAccessible(TRUE);
$database_property
->setValue($plugin, $this
->getDatabase($this->databaseContents + [
'test_map' => [],
]));
$module_handler_property
->setValue($plugin, $module_handler);
$plugin
->setStringTranslation($this
->getStringTranslationStub());
$migration
->expects($this
->any())
->method('getSourcePlugin')
->will($this
->returnValue($plugin));
$this->source = $plugin;
$this->expectedCount = count($this->expectedResults);
}
public function testRetrieval() {
$this
->assertInstanceOf(SelectInterface::class, $this->source
->query());
$this
->queryResultTest($this->source, $this->expectedResults);
}
public function testSourceCount() {
$count = $this->source
->count();
$this
->assertTrue(is_numeric($count));
$this
->assertEquals($this->expectedCount, $count);
}
public function testSourceId() {
$this
->assertNotEmpty($this->source
->getIds());
}
protected function getValue($row, $key) {
return $row
->getSourceProperty($key);
}
}