5 * Contains \Drupal\Tests\migrate\Unit\SqlBaseTest.
8 namespace Drupal\Tests\migrate\Unit;
10 use Drupal\migrate\Plugin\MigrationInterface;
11 use Drupal\migrate\Plugin\migrate\source\SqlBase;
12 use Drupal\Tests\UnitTestCase;
15 * Tests the SqlBase class.
19 class SqlBaseTest extends UnitTestCase {
22 * Tests that the ID map is joinable.
24 * @param bool $expected_result
25 * The expected result.
26 * @param bool $id_map_is_sql
27 * TRUE if we want getIdMap() to return an instance of Sql.
28 * @param bool $with_id_map
29 * TRUE if we want the ID map to have a valid map of IDs.
30 * @param array $source_options
31 * (optional) An array of connection options for the source connection.
32 * Defaults to an empty array.
33 * @param array $idmap_options
34 * (optional) An array of connection options for the ID map connection.
35 * Defaults to an empty array.
37 * @dataProvider sqlBaseTestProvider
39 public function testMapJoinable($expected_result, $id_map_is_sql, $with_id_map, $source_options = [], $idmap_options = []) {
40 // Setup a connection object.
41 $source_connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
42 ->disableOriginalConstructor()
44 $source_connection->expects($id_map_is_sql && $with_id_map ? $this->once() : $this->never())
45 ->method('getConnectionOptions')
46 ->willReturn($source_options);
48 // Setup the ID map connection.
49 $idmap_connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
50 ->disableOriginalConstructor()
52 $idmap_connection->expects($id_map_is_sql && $with_id_map ? $this->once() : $this->never())
53 ->method('getConnectionOptions')
54 ->willReturn($idmap_options);
56 // Setup the Sql object.
57 $sql = $this->getMockBuilder('Drupal\migrate\Plugin\migrate\id_map\Sql')
58 ->disableOriginalConstructor()
60 $sql->expects($id_map_is_sql && $with_id_map ? $this->once() : $this->never())
61 ->method('getDatabase')
62 ->willReturn($idmap_connection);
64 // Setup a migration entity.
65 $migration = $this->getMock(MigrationInterface::class);
66 $migration->expects($with_id_map ? $this->once() : $this->never())
68 ->willReturn($id_map_is_sql ? $sql : NULL);
70 // Create our SqlBase test class.
71 $sql_base = new TestSqlBase();
72 $sql_base->setMigration($migration);
73 $sql_base->setDatabase($source_connection);
75 // Configure the idMap to make the check in mapJoinable() pass.
78 'uid' => ['type' => 'integer', 'alias' => 'u'],
82 $this->assertEquals($expected_result, $sql_base->mapJoinable());
86 * The data provider for SqlBase.
89 * An array of data per test run.
91 public function sqlBaseTestProvider() {
93 // Source ids are empty so mapJoinable() is false.
99 // Still false because getIdMap() is not a subclass of Sql.
105 // Test mapJoinable() returns false when source and id connection options
111 ['driver' => 'mysql', 'username' => 'different_from_map', 'password' => 'different_from_map'],
112 ['driver' => 'mysql', 'username' => 'different_from_source', 'password' => 'different_from_source'],
114 // Returns false because driver is pgsql and the databases are not the
120 ['driver' => 'pgsql', 'database' => '1.pgsql', 'username' => 'same_value', 'password' => 'same_value'],
121 ['driver' => 'pgsql', 'database' => '2.pgsql', 'username' => 'same_value', 'password' => 'same_value'],
123 // Returns false because driver is sqlite and the databases are not the
129 ['driver' => 'sqlite', 'database' => '1.sqlite', 'username' => '', 'password' => ''],
130 ['driver' => 'sqlite', 'database' => '2.sqlite', 'username' => '', 'password' => ''],
132 // Returns false because driver is not the same.
137 ['driver' => 'pgsql', 'username' => 'same_value', 'password' => 'same_value'],
138 ['driver' => 'mysql', 'username' => 'same_value', 'password' => 'same_value'],
146 * Creates a base source class for SQL migration testing.
148 class TestSqlBase extends SqlBase {
151 * The database object.
165 * Override the constructor so we can create one easily.
167 public function __construct() {}
170 * Allows us to set the database during tests.
172 * @param mixed $database
173 * The database mock object.
175 public function setDatabase($database) {
176 $this->database = $database;
182 public function getDatabase() {
183 return $this->database;
187 * Allows us to set the migration during the test.
189 * @param mixed $migration
190 * The migration mock.
192 public function setMigration($migration) {
193 $this->migration = $migration;
199 public function mapJoinable() {
200 return parent::mapJoinable();
206 public function getIds() {
211 * Allows us to set the IDs during a test.
214 * An array of identifiers.
216 public function setIds($ids) {
223 public function fields() {}
228 public function query() {}
233 public function calculateDependencies() {