Chris@0: migrationConfiguration += ['migrationClass' => 'Drupal\migrate\Plugin\Migration']; Chris@0: $this->idMap = $this->getMock('Drupal\migrate\Plugin\MigrateIdMapInterface'); Chris@0: Chris@0: $this->idMap Chris@0: ->method('getQualifiedMapTableName') Chris@0: ->willReturn('test_map'); Chris@0: Chris@0: $migration = $this->getMockBuilder($this->migrationConfiguration['migrationClass']) Chris@0: ->disableOriginalConstructor() Chris@0: ->getMock(); Chris@0: Chris@0: $migration->method('checkRequirements') Chris@0: ->willReturn(TRUE); Chris@0: Chris@0: $migration->method('getIdMap') Chris@0: ->willReturn($this->idMap); Chris@0: Chris@0: // We need the state to be toggled throughout the test so we store the value Chris@0: // on the test class and use a return callback. Chris@0: $migration->expects($this->any()) Chris@0: ->method('getStatus') Chris@0: ->willReturnCallback(function () { Chris@0: return $this->migrationStatus; Chris@0: }); Chris@0: $migration->expects($this->any()) Chris@0: ->method('setStatus') Chris@0: ->willReturnCallback(function ($status) { Chris@0: $this->migrationStatus = $status; Chris@0: }); Chris@0: Chris@0: $migration->method('getMigrationDependencies') Chris@0: ->willReturn([ Chris@0: 'required' => [], Chris@0: 'optional' => [], Chris@0: ]); Chris@0: Chris@0: $configuration = &$this->migrationConfiguration; Chris@0: Chris@0: $migration->method('set') Chris@0: ->willReturnCallback(function ($argument, $value) use (&$configuration) { Chris@0: $configuration[$argument] = $value; Chris@0: }); Chris@0: Chris@0: $migration->method('id') Chris@0: ->willReturn($configuration['id']); Chris@0: Chris@0: return $migration; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets an SQLite database connection object for use in tests. Chris@0: * Chris@0: * @param array $database_contents Chris@0: * The database contents faked as an array. Each key is a table name, each Chris@0: * value is a list of table rows, an associative array of field => value. Chris@0: * @param array $connection_options Chris@0: * (optional) Options for the database connection. Defaults to an empty Chris@0: * array. Chris@0: * Chris@0: * @return \Drupal\Core\Database\Driver\sqlite\Connection Chris@0: * The database connection. Chris@0: */ Chris@0: protected function getDatabase(array $database_contents, $connection_options = []) { Chris@0: if (extension_loaded('pdo_sqlite')) { Chris@0: $connection_options['database'] = ':memory:'; Chris@0: $pdo = Connection::open($connection_options); Chris@0: $connection = new Connection($pdo, $connection_options); Chris@0: } Chris@0: else { Chris@0: $this->markTestSkipped('The pdo_sqlite extension is not available.'); Chris@0: } Chris@0: Chris@0: // Initialize the DIC with a fake module handler for alterable queries. Chris@0: $container = new ContainerBuilder(); Chris@0: $container->set('module_handler', $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface')); Chris@0: \Drupal::setContainer($container); Chris@0: Chris@0: // Create the tables and load them up with data, skipping empty ones. Chris@0: foreach (array_filter($database_contents) as $table => $rows) { Chris@0: $pilot_row = reset($rows); Chris@0: $connection->schema()->createTable($table, $this->createSchemaFromRow($pilot_row)); Chris@0: Chris@0: $insert = $connection->insert($table)->fields(array_keys($pilot_row)); Chris@0: array_walk($rows, [$insert, 'values']); Chris@0: $insert->execute(); Chris@0: } Chris@0: Chris@0: return $connection; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a table schema from a row. Chris@0: * Chris@0: * @param array $row Chris@0: * The reference row on which to base the schema. Chris@0: * Chris@0: * @return array Chris@0: * The Schema API-ready table schema. Chris@0: */ Chris@0: protected function createSchemaFromRow(array $row) { Chris@0: // SQLite uses loose ("affinity") typing, so it is OK for every column to be Chris@0: // a text field. Chris@0: $fields = array_map(function () { Chris@0: return ['type' => 'text']; Chris@0: }, $row); Chris@0: return ['fields' => $fields]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests a query. Chris@0: * Chris@0: * @param array|\Traversable $iter Chris@0: * The countable. foreach-able actual results if a query is being run. Chris@0: * @param array $expected_results Chris@0: * An array of expected results. Chris@0: */ Chris@0: public function queryResultTest($iter, $expected_results) { Chris@0: $this->assertSame(count($expected_results), count($iter), 'Number of results match'); Chris@0: $count = 0; Chris@0: foreach ($iter as $data_row) { Chris@0: $expected_row = $expected_results[$count]; Chris@0: $count++; Chris@0: foreach ($expected_row as $key => $expected_value) { Chris@0: $this->retrievalAssertHelper($expected_value, $this->getValue($data_row, $key), sprintf('Value matches for key "%s"', $key)); Chris@0: } Chris@0: } Chris@0: $this->assertSame(count($expected_results), $count); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the value on a row for a given key. Chris@0: * Chris@0: * @param array $row Chris@0: * The row information. Chris@0: * @param string $key Chris@0: * The key identifier. Chris@0: * Chris@0: * @return mixed Chris@0: * The value on a row for a given key. Chris@0: */ Chris@0: protected function getValue($row, $key) { Chris@0: return $row[$key]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts tested values during test retrieval. Chris@0: * Chris@0: * @param mixed $expected_value Chris@0: * The incoming expected value to test. Chris@0: * @param mixed $actual_value Chris@0: * The incoming value itself. Chris@0: * @param string $message Chris@0: * The tested result as a formatted string. Chris@0: */ Chris@0: protected function retrievalAssertHelper($expected_value, $actual_value, $message) { Chris@0: if (is_array($expected_value)) { Chris@0: // If the expected and actual values are empty, no need to array compare. Chris@0: if (empty($expected_value && $actual_value)) { Chris@0: return; Chris@0: } Chris@0: $this->assertArrayEquals($expected_value, $actual_value, $message); Chris@0: } Chris@0: else { Chris@0: $this->assertSame((string) $expected_value, (string) $actual_value, $message); Chris@0: } Chris@0: } Chris@0: Chris@0: }