Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeclarativeInstallerTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup;
8 
18 
23 {
27  private $moduleManager;
28 
32  private $cliCommad;
33 
37  private $schemaDiff;
38 
42  private $schemaConfig;
43 
47  private $resourceConnection;
48 
52  private $describeTable;
53 
54  public function setUp()
55  {
57  $this->moduleManager = $objectManager->get(TestModuleManager::class);
58  $this->cliCommad = $objectManager->get(CliCommand::class);
59  $this->describeTable = $objectManager->get(DescribeTable::class);
60  $this->schemaDiff = $objectManager->get(SchemaDiff::class);
61  $this->schemaConfig = $objectManager->get(SchemaConfigInterface::class);
62  $this->resourceConnection = $objectManager->get(ResourceConnection::class);
63  }
64 
69  public function testInstallation()
70  {
71  $this->cliCommad->install(
72  ['Magento_TestSetupDeclarationModule1']
73  );
74 
75  $diff = $this->schemaDiff->diff(
76  $this->schemaConfig->getDeclarationConfig(),
77  $this->schemaConfig->getDbConfig()
78  );
79 
80  //Second time installation should not find anything as we do not change anything
81  self::assertNull($diff->getAll());
82  $this->compareStructures();
83  }
84 
88  private function compareStructures()
89  {
90  $shardData = $this->describeTable->describeShard(Sharding::DEFAULT_CONNECTION);
91  foreach ($this->getTrimmedData() as $tableName => $sql) {
92  $this->assertArrayHasKey($tableName, $shardData);
97  if (preg_match('#ON DELETE\s+NO ACTION#i', $shardData[$tableName] === 1)) {
98  preg_replace('#ON DELETE\s+NO ACTION#i', '', $sql);
99  self::assertEquals($sql, $shardData[$tableName]);
100  }
101  }
102  }
103 
109  {
110  $this->cliCommad->install(
111  ['Magento_TestSetupDeclarationModule1']
112  );
113 
114  //Move InstallSchema file and tried to install
115  $this->moduleManager->updateRevision(
116  'Magento_TestSetupDeclarationModule1',
117  'column_modifications',
118  'db_schema.xml',
119  'etc'
120  );
121 
122  $this->cliCommad->install(
123  ['Magento_TestSetupDeclarationModule1']
124  );
125 
126  $diff = $this->schemaDiff->diff(
127  $this->schemaConfig->getDeclarationConfig(),
128  $this->schemaConfig->getDbConfig()
129  );
130  self::assertNull($diff->getAll());
131  $this->compareStructures();
132  }
133 
139  private function updateDbSchemaRevision($revisionName)
140  {
141  //Move InstallSchema file and tried to install
142  $this->moduleManager->updateRevision(
143  'Magento_TestSetupDeclarationModule1',
144  $revisionName,
145  'db_schema.xml',
146  'etc'
147  );
148  //Move InstallSchema file and tried to install
149  $this->moduleManager->updateRevision(
150  'Magento_TestSetupDeclarationModule1',
151  $revisionName,
152  'db_schema_whitelist.json',
153  'etc'
154  );
155  }
156 
162  {
163  $this->cliCommad->install(
164  ['Magento_TestSetupDeclarationModule1']
165  );
166  $this->updateDbSchemaRevision('column_removals');
167  $this->cliCommad->install(
168  ['Magento_TestSetupDeclarationModule1']
169  );
170 
171  $diff = $this->schemaDiff->diff(
172  $this->schemaConfig->getDeclarationConfig(),
173  $this->schemaConfig->getDbConfig()
174  );
175  self::assertNull($diff->getAll());
176  $this->compareStructures();
177  }
178 
185  private function getTrimmedData()
186  {
187  $data = [];
188  foreach ($this->getData() as $key => $createTable) {
189  $data[$key] = preg_replace('/(\s)\n/', '$1', $createTable);
190  }
191 
192  return $data;
193  }
194 
200  {
201  $this->cliCommad->install(
202  ['Magento_TestSetupDeclarationModule1']
203  );
204  $this->updateDbSchemaRevision('constraint_modifications');
205  $this->cliCommad->upgrade();
206 
207  $diff = $this->schemaDiff->diff(
208  $this->schemaConfig->getDeclarationConfig(),
209  $this->schemaConfig->getDbConfig()
210  );
211  self::assertNull($diff->getAll());
212  $shardData = $this->describeTable->describeShard(Sharding::DEFAULT_CONNECTION);
213  self::assertEquals($this->getTrimmedData(), $shardData);
214  }
215 
221  {
222  $this->cliCommad->install(
223  ['Magento_TestSetupDeclarationModule1']
224  );
225 
226  //Move db_schema.xml file and tried to install
227  $this->moduleManager->updateRevision(
228  'Magento_TestSetupDeclarationModule1',
229  'drop_table',
230  'db_schema.xml',
231  'etc'
232  );
233 
234  $this->cliCommad->upgrade();
235 
236  $diff = $this->schemaDiff->diff(
237  $this->schemaConfig->getDeclarationConfig(),
238  $this->schemaConfig->getDbConfig()
239  );
240  self::assertNull($diff->getAll());
241  $shardData = $this->describeTable->describeShard(Sharding::DEFAULT_CONNECTION);
242  self::assertEquals($this->getData(), $shardData);
243  }
244 
250  {
251  //Move db_schema.xml file and tried to install
252  $this->moduleManager->updateRevision(
253  'Magento_TestSetupDeclarationModule1',
254  'before_rollback',
255  'db_schema.xml',
256  'etc'
257  );
258  $this->cliCommad->install(
259  ['Magento_TestSetupDeclarationModule1']
260  );
261  $beforeRollback = $this->describeTable->describeShard('default');
262  self::assertEquals($this->getTrimmedData()['before'], $beforeRollback);
263  //Move db_schema.xml file and tried to install
264  $this->moduleManager->updateRevision(
265  'Magento_TestSetupDeclarationModule1',
266  'after_rollback',
267  'db_schema.xml',
268  'etc'
269  );
270 
271  $this->cliCommad->upgrade();
272  $afterRollback = $this->describeTable->describeShard('default');
273  self::assertEquals($this->getData()['after'], $afterRollback);
274  }
275 
280  public function testTableRename()
281  {
282  $dataToMigrate = ['some_column' => 'Some Value'];
283  //Move db_schema.xml file and tried to install
284  $this->moduleManager->updateRevision(
285  'Magento_TestSetupDeclarationModule1',
286  'table_rename',
287  'db_schema.xml',
288  'etc'
289  );
290  $this->cliCommad->install(
291  ['Magento_TestSetupDeclarationModule1']
292  );
293  $before = $this->describeTable->describeShard('default');
294  $adapter = $this->resourceConnection->getConnection('default');
295  $adapter->insert(
296  $this->resourceConnection->getTableName('some_table'),
297  $dataToMigrate
298  );
299  self::assertEquals($this->getData()['before'], $before['some_table']);
300  //Move db_schema.xml file and tried to install
301  $this->moduleManager->updateRevision(
302  'Magento_TestSetupDeclarationModule1',
303  'table_rename_after',
304  'db_schema.xml',
305  'etc'
306  );
307 
308  $this->cliCommad->upgrade();
309  $after = $this->describeTable->describeShard('default');
310  self::assertEquals($this->getData()['after'], $after['some_table_renamed']);
311  $select = $adapter->select()
312  ->from($this->resourceConnection->getTableName('some_table_renamed'));
313  self::assertEquals([$dataToMigrate], $adapter->fetchAll($select));
314  }
315 
319  public function testForeignKeyReferenceId()
320  {
321  $this->cliCommad->install(
322  ['Magento_TestSetupDeclarationModule8']
323  );
324  $this->moduleManager->updateRevision(
325  'Magento_TestSetupDeclarationModule8',
326  'unpatterned_fk_name',
327  'db_schema.xml',
328  'etc'
329  );
330  $this->cliCommad->upgrade();
331  $tableStatements = $this->describeTable->describeShard('default');
332  $tableSql = $tableStatements['dependent'];
333  $this->assertRegExp('/CONSTRAINT\s`DEPENDENT_PAGE_ID_ON_TEST_TABLE_PAGE_ID`/', $tableSql);
334  $this->assertRegExp('/CONSTRAINT\s`DEPENDENT_SCOPE_ID_ON_TEST_SCOPE_TABLE_SCOPE_ID`/', $tableSql);
335  }
336 
342  {
343  $this->cliCommad->install(
344  ['Magento_TestSetupDeclarationModule1', 'Magento_TestSetupDeclarationModule8']
345  );
346  $this->moduleManager->updateRevision(
347  'Magento_TestSetupDeclarationModule1',
348  'index_to_disable',
349  'db_schema.xml',
350  'etc'
351  );
352  $this->moduleManager->updateRevision(
353  'Magento_TestSetupDeclarationModule8',
354  'disable_index_by_external_module',
355  'db_schema.xml',
356  'etc'
357  );
358  $this->moduleManager->updateRevision(
359  'Magento_TestSetupDeclarationModule8',
360  'disable_index_by_external_module',
361  'db_schema_whitelist.json',
362  'etc'
363  );
364  $this->moduleManager->updateRevision(
365  'Magento_TestSetupDeclarationModule8',
366  'disable_index_by_external_module',
367  'module.xml',
368  'etc'
369  );
370  $this->cliCommad->upgrade();
371  $tableStatements = $this->describeTable->describeShard('default');
372  $tableSql = $tableStatements['test_table'];
373  $this->assertNotRegExp(
374  '/KEY\s+`TEST_TABLE_VARCHAR`\s+\(`varchar`\)/',
375  $tableSql,
376  'Index is not being disabled by external module'
377  );
378  }
379 }
$tableName
Definition: trigger.php:13
$objectManager
Definition: bootstrap.php:17
$adapter
Definition: webapi_user.php:16