Commit bbd1baf2 authored by alexpott's avatar alexpott

Issue #2621486 by Lars Toomre: Fixes to migrate/tests/src/Unit/*.php files

parent 5421fdee
......@@ -45,7 +45,9 @@ class MigrateExecutableMemoryExceededTest extends MigrateTestCase {
);
/**
* php.init memory_limit value.
* The php.ini memory_limit value.
*
* @var int
*/
protected $memoryLimit = 10000000;
......@@ -68,12 +70,13 @@ protected function setUp() {
* The second message to assert.
* @param bool $memory_exceeded
* Whether to test the memory exceeded case.
* @param int $memory_usage_first
* (optional) The first memory usage value.
* @param int $memory_usage_second
* @param int|null $memory_usage_first
* (optional) The first memory usage value. Defaults to NULL.
* @param int|null $memory_usage_second
* (optional) The fake amount of memory usage reported after memory reclaim.
* @param int $memory_limit
* (optional) The memory limit.
* Defaults to NULL.
* @param int|null $memory_limit
* (optional) The memory limit. Defaults to NULL.
*/
protected function runMemoryExceededTest($message, $memory_exceeded, $memory_usage_first = NULL, $memory_usage_second = NULL, $memory_limit = NULL) {
$this->executable->setMemoryLimit($memory_limit ?: $this->memoryLimit);
......
......@@ -40,6 +40,11 @@ class MigrateExecutableTest extends MigrateTestCase {
*/
protected $executable;
/**
* The migration's configuration values.
*
* @var array
*/
protected $migrationConfiguration = array(
'id' => 'test',
);
......@@ -224,8 +229,9 @@ public function testImportWithValidRowNoDestinationValues() {
}
/**
* Tests the import method with a MigrateException being thrown from the
* destination.
* Tests the import method with a thrown MigrateException.
*
* The MigrationException in this case is being thrown from the destination.
*/
public function testImportWithValidRowWithDestinationMigrateException() {
$exception_message = $this->getRandomGenerator()->string();
......@@ -275,8 +281,9 @@ public function testImportWithValidRowWithDestinationMigrateException() {
}
/**
* Tests the import method with a MigrateException being thrown from a process
* plugin.
* Tests the import method with a thrown MigrateException.
*
* The MigrationException in this case is being thrown from a process plugin.
*/
public function testImportWithValidRowWithProcesMigrateException() {
$exception_message = $this->getRandomGenerator()->string();
......@@ -421,6 +428,7 @@ public function testProcessRowEmptyPipeline() {
* Returns a mock migration source instance.
*
* @return \Drupal\migrate\Plugin\MigrateSourceInterface|\PHPUnit_Framework_MockObject_MockObject
* The mocked migration source.
*/
protected function getMockSource() {
$iterator = $this->getMock('\Iterator');
......
......@@ -61,14 +61,16 @@ class MigrateSourceTest extends MigrateTestCase {
protected $executable;
/**
* Get the source plugin to test.
* Gets the source plugin to test.
*
* @param array $configuration
* The source configuration.
* (optional) The source configuration. Defaults to an empty array.
* @param array $migrate_config
* The migration configuration to be used in parent::getMigration().
* (optional) The migration configuration to be used in
* parent::getMigration(). Defaults to an empty array.
* @param int $status
* The default status for the new rows to be imported.
* (optional) The default status for the new rows to be imported. Defaults
* to MigrateIdMapInterface::STATUS_NEEDS_UPDATE.
*
* @return \Drupal\migrate\Plugin\MigrateSourceInterface
* A mocked source plugin.
......@@ -367,7 +369,7 @@ public function testPrepareRowPrepareException() {
}
/**
* Get a mock executable for the test.
* Gets a mock executable for the test.
*
* @param \Drupal\migrate\Entity\MigrationInterface $migration
* The migration entity.
......@@ -394,8 +396,9 @@ class StubSourcePlugin extends SourcePluginBase {
* Helper for setting internal module handler implementation.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
function setModuleHandler(ModuleHandlerInterface $module_handler) {
public function setModuleHandler(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
......@@ -426,4 +429,5 @@ public function getIds() {
protected function initializeIterator() {
return [];
}
}
......@@ -29,12 +29,22 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
'id' => 'sql_idmap_test',
];
/**
* The source IDs.
*
* @var array
*/
protected $sourceIds = [
'source_id_property' => [
'type' => 'string',
],
];
/**
* The destination IDs.
*
* @var array
*/
protected $destinationIds = [
'destination_id_property' => [
'type' => 'string',
......@@ -48,6 +58,9 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
*/
protected $database;
/**
* {@inheritdoc}
*/
public function setUp() {
$this->database = $this->getDatabase([]);
}
......@@ -357,6 +370,9 @@ public function testGetRowBySource() {
* - Multi-value source ID to multi-value destination ID.
* - Single-value source ID to multi-value destination ID.
* - Multi-value source ID to single-value destination ID.
*
* @return array
* An array of data values.
*/
public function lookupDestinationIdMappingDataProvider() {
return [
......@@ -374,6 +390,7 @@ public function lookupDestinationIdMappingDataProvider() {
* Number of source fields to test.
* @param int $num_destination_fields
* Number of destination fields to test.
*
* @dataProvider lookupDestinationIdMappingDataProvider
*/
public function testLookupDestinationIdMapping($num_source_fields, $num_destination_fields) {
......@@ -441,6 +458,9 @@ public function testGetRowByDestination() {
* - Multi-value destination ID to multi-value source ID.
* - Single-value destination ID to multi-value source ID.
* - Multi-value destination ID to single-value source ID.
*
* @return array
* An array of data values.
*/
public function lookupSourceIDMappingDataProvider() {
return [
......@@ -458,6 +478,7 @@ public function lookupSourceIDMappingDataProvider() {
* Number of source fields to test.
* @param int $num_destination_fields
* Number of destination fields to test.
*
* @dataProvider lookupSourceIDMappingDataProvider
*/
public function testLookupSourceIDMapping($num_source_fields, $num_destination_fields) {
......@@ -562,6 +583,9 @@ public function testProcessedCount() {
* - No updates.
* - One update.
* - Multiple updates.
*
* @return array
* An array of data values.
*/
public function updateCountDataProvider() {
return [
......@@ -576,6 +600,7 @@ public function updateCountDataProvider() {
*
* @param int $num_update_rows
* The number of update rows to test.
*
* @dataProvider updateCountDataProvider
*/
public function testUpdateCount($num_update_rows) {
......@@ -604,6 +629,9 @@ public function testUpdateCount($num_update_rows) {
* - No errors.
* - One error.
* - Multiple errors.
*
* @return array
* An array of data values.
*/
public function errorCountDataProvider() {
return [
......@@ -618,6 +646,7 @@ public function errorCountDataProvider() {
*
* @param int $num_error_rows
* Number of error rows to test.
*
* @dataProvider errorCountDataProvider
*/
public function testErrorCount($num_error_rows) {
......@@ -795,6 +824,12 @@ public function testIterators() {
$this->assertSame(iterator_to_array($this->getIdMap()), $expected_results);
}
/**
* Retrieves the contents of an ID map.
*
* @return array
* The contents of an ID map.
*/
private function getIdMapContents() {
$result = $this->database
->select('migrate_map_sql_idmap_test', 't')
......
......@@ -111,7 +111,7 @@ protected function setUp() {
}
/**
* Test the source returns the same rows as expected.
* Tests that the source returns the same rows as expected.
*/
public function testRetrieval() {
$this->assertInstanceOf(SelectInterface::class, $this->source->query());
......@@ -119,7 +119,7 @@ public function testRetrieval() {
}
/**
* Test the source returns the row count expected.
* Tests that the source returns the row count expected.
*/
public function testSourceCount() {
$count = $this->source->count();
......@@ -128,16 +128,22 @@ public function testSourceCount() {
}
/**
* Test the source defines a valid ID.
* Tests the source defines a valid ID.
*/
public function testSourceId() {
$this->assertNotEmpty($this->source->getIds());
}
/**
* Gets the value on a row for a given key.
*
* @param \Drupal\migrate\Row $row
* The row identifier.
* @param string $key
* The key identifier.
*
* @return mixed
* The value on a row for a given key.
*/
protected function getValue($row, $key) {
return $row->getSourceProperty($key);
......
......@@ -17,9 +17,16 @@
*/
abstract class MigrateTestCase extends UnitTestCase {
/**
* An array of migration configuration values.
*
* @var array
*/
protected $migrationConfiguration = [];
/**
* The migration ID map.
*
* @var \Drupal\migrate\Plugin\MigrateIdMapInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $idMap;
......@@ -32,7 +39,7 @@ abstract class MigrateTestCase extends UnitTestCase {
protected $migrationStatus = MigrationInterface::STATUS_IDLE;
/**
* Retrieve a mocked migration.
* Retrieves a mocked migration.
*
* @return \Drupal\migrate\Entity\MigrationInterface|\PHPUnit_Framework_MockObject_MockObject
* The mocked migration.
......@@ -83,8 +90,8 @@ protected function getMigration() {
$migration->method('set')
->willReturnCallback(function ($argument, $value) use (&$configuration) {
$configuration[$argument] = $value;
});
$configuration[$argument] = $value;
});
$migration->method('id')
->willReturn($configuration['id']);
......@@ -93,13 +100,14 @@ protected function getMigration() {
}
/**
* Get an SQLite database connection object for use in tests.
* Gets an SQLite database connection object for use in tests.
*
* @param array $database_contents
* The database contents faked as an array. Each key is a table name, each
* value is a list of table rows, an associative array of field => value.
* @param array $connection_options
* (optional) Options for the database connection.
* (optional) Options for the database connection. Defaults to an empty
* array.
*
* @return \Drupal\Core\Database\Driver\sqlite\Connection
* The database connection.
......@@ -136,23 +144,25 @@ protected function getDatabase(array $database_contents, $connection_options = [
* Generates a table schema from a row.
*
* @param array $row
* The reference row on which to base the schema.
* The reference row on which to base the schema.
*
* @return array
* The Schema API-ready table schema.
* The Schema API-ready table schema.
*/
protected function createSchemaFromRow(array $row) {
// SQLite uses loose ("affinity") typing, so it's OK for every column
// to be a text field.
// SQLite uses loose ("affinity") typing, so it is OK for every column to be
// a text field.
$fields = array_map(function() { return ['type' => 'text']; }, $row);
return ['fields' => $fields];
}
/**
* Tests a query
* Tests a query.
*
* @param array|\Traversable
* @param array|\Traversable $iter
* The countable. foreach-able actual results if a query is being run.
* @param array $expected_results
* An array of expected results.
*/
public function queryResultTest($iter, $expected_results) {
$this->assertSame(count($expected_results), count($iter), 'Number of results match');
......@@ -168,9 +178,15 @@ public function queryResultTest($iter, $expected_results) {
}
/**
* Gets the value on a row for a given key.
*
* @param array $row
* The row information.
* @param string $key
* The key identifier.
*
* @return mixed
* The value on a row for a given key.
*/
protected function getValue($row, $key) {
return $row[$key];
......
......@@ -23,11 +23,15 @@
class MigrationStorageTest extends UnitTestCase {
/**
* The migration storage.
*
* @var \Drupal\Tests\migrate\Unit\TestMigrationStorage
*/
protected $storage;
/**
* The storage query.
*
* @var \Drupal\Core\Entity\Query\QueryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $query;
......@@ -80,7 +84,9 @@ public function testGetVariantIdsNoVariants() {
}
/**
* Tests getVariantIds() when no variants exist and there are no static
* Tests getVariantIds().
*
* This tests getVariantIds() when no variants exist and there are no static
* (non-variant) dependencies.
*
* @covers ::getVariantIds
......@@ -98,7 +104,7 @@ public function testGetVariantIdsNoVariantsOrStaticDependencies() {
/**
* Test version of \Drupal\migrate\MigrationStorage.
*
* Exposes protected methods for testing.
* This class exposes protected methods for testing.
*/
class TestMigrationStorage extends MigrationStorage {
......
......@@ -120,29 +120,65 @@ public function testRequirementsForMigrations() {
}
/**
* Defines the TestMigration class.
*/
class TestMigration extends Migration {
/**
* Constructs an instance of TestMigration object.
*/
public function __construct() {
}
/**
* Sets the requirements values.
*
* @param array $requirements
* The array of requirement values.
*/
public function setRequirements(array $requirements) {
$this->requirements = $requirements;
}
/**
* Sets the source Plugin.
*
* @param \Drupal\migrate\Plugin\MigrateSourceInterface $source_plugin
* The source Plugin.
*/
public function setSourcePlugin(MigrateSourceInterface $source_plugin) {
$this->sourcePlugin = $source_plugin;
}
/**
* Sets the destination Plugin.
*
* @param \Drupal\migrate\Plugin\MigrateDestinationInterface $destination_plugin
* The destination Plugin.
*/
public function setDestinationPlugin(MigrateDestinationInterface $destination_plugin) {
$this->destinationPlugin = $destination_plugin;
}
/**
* Sets the entity manager service.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
*/
public function setEntityManager(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
}
/**
* Defines the RequirementsAwareSourceInterface.
*/
interface RequirementsAwareSourceInterface extends MigrateSourceInterface, RequirementsInterface {}
/**
* Defines the RequirementsAwareDestinationInterface.
*/
interface RequirementsAwareDestinationInterface extends MigrateDestinationInterface, RequirementsInterface {}
......@@ -18,6 +18,8 @@
class SqlBaseTest extends UnitTestCase {
/**
* Tests that the ID map is joinable.
*
* @param bool $expected_result
* The expected result.
* @param bool $id_map_is_sql
......@@ -28,8 +30,8 @@ class SqlBaseTest extends UnitTestCase {
* (optional) An array of connection options for the source connection.
* Defaults to an empty array.
* @param array $idmap_options
* (optional) An array of connection options for the ID map connection.
* Defaults to an empty array.
* (optional) An array of connection options for the ID map connection.
* Defaults to an empty array.
*
* @dataProvider sqlBaseTestProvider
*/
......@@ -87,23 +89,57 @@ public function testMapJoinable($expected_result, $id_map_is_sql, $with_id_map,
*/
public function sqlBaseTestProvider() {
return [
// Source IDs are empty so mapJoinable() is false.
[FALSE, FALSE, FALSE],
// 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
[
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']],
[
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'],
],
];
}
}
/**
* Creates a base source class for SQL migration testing.
*/
class TestSqlBase extends SqlBase {
/**
* The database object.
*
* @var object
*/
protected $database;
/**
* The migration IDs.
*
* @var array
*/
protected $ids;
/**
......
......@@ -53,7 +53,7 @@ public function setSource($source) {
* Allows access to protected sourceIdValues property.
*
* @param array $source_id_values
* The value to set.
* The values to set.
*/
public function setSourceIdValues($source_id_values) {
$this->sourceIdValues = $source_id_values;
......@@ -100,7 +100,7 @@ protected function getMemoryUsage() {
* @param int $memory_usage
* The fake memory usage value.
* @param int $cleared_memory_usage
* (optional) The fake cleared memory value.
* (optional) The fake cleared memory value. Defaults to NULL.
*/
public function setMemoryUsage($memory_usage, $cleared_memory_usage = NULL) {
$this->memoryUsage = $memory_usage;
......
......@@ -31,6 +31,8 @@ class TestSqlIdMap extends Sql implements \Iterator {
* The configuration for the plugin.
* @param \Drupal\migrate\Entity\MigrationInterface $migration
* The migration to do.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher service.
*/
public function __construct(Connection $database, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher) {
$this->database = $database;
......@@ -44,6 +46,18 @@ public function getDatabase() {
return parent::getDatabase();
}
/**
* Gets the field schema.
*
* @param array $id_definition
* An array defining the field, with a key 'type'.
*
* @return array
* A field schema depending on value of key 'type'. An empty array is
* returned if 'type' is not defined.
*
* @throws \Drupal\migrate\MigrateException
*/
protected function getFieldSchema(array $id_definition) {
if (!isset($id_definition['type'])) {
return array();
......@@ -54,14 +68,17 @@ protected function getFieldSchema(array $id_definition) {
'type' => 'int',
'not null' => TRUE,
);
case 'string':
return array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
);
default:
throw new MigrateException($id_definition['type'] . ' not supported');
}
}