BCMultiModuleTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Setup;
  7. use Magento\Framework\Module\DbVersionInfo;
  8. use Magento\Framework\Module\ModuleResource;
  9. use Magento\Framework\Setup\Declaration\Schema\Db\DbSchemaReaderInterface;
  10. use Magento\TestFramework\Deploy\CliCommand;
  11. use Magento\TestFramework\Deploy\TableData;
  12. use Magento\TestFramework\Deploy\TestModuleManager;
  13. use Magento\TestFramework\Helper\Bootstrap;
  14. use Magento\TestFramework\TestCase\SetupTestCase;
  15. /**
  16. * The purpose of this test is to check whether whole declarative installation is work
  17. * in mixed mode
  18. */
  19. class BCMultiModuleTest extends SetupTestCase
  20. {
  21. /**
  22. * @var TestModuleManager
  23. */
  24. private $moduleManager;
  25. /**
  26. * @var CliCommand
  27. */
  28. private $cliCommand;
  29. /**
  30. * @var DbVersionInfo
  31. */
  32. private $dbVersionInfo;
  33. /**
  34. * @var TableData
  35. */
  36. private $tableData;
  37. /**
  38. * @var ModuleResource
  39. */
  40. private $moduleResource;
  41. /**
  42. * @var DbSchemaReaderInterface
  43. */
  44. private $dbSchemaReader;
  45. public function setUp()
  46. {
  47. $objectManager = Bootstrap::getObjectManager();
  48. $this->moduleManager = $objectManager->get(TestModuleManager::class);
  49. $this->cliCommand = $objectManager->get(CliCommand::class);
  50. $this->dbVersionInfo = $objectManager->get(DbVersionInfo::class);
  51. $this->tableData = $objectManager->get(TableData::class);
  52. $this->moduleResource = $objectManager->get(ModuleResource::class);
  53. $this->dbSchemaReader = $objectManager->get(DbSchemaReaderInterface::class);
  54. }
  55. /**
  56. * @moduleName Magento_TestSetupDeclarationModule6
  57. * @moduleName Magento_TestSetupDeclarationModule7
  58. */
  59. public function testFirstCleanInstall()
  60. {
  61. $this->cliCommand->install([
  62. 'Magento_TestSetupDeclarationModule6',
  63. 'Magento_TestSetupDeclarationModule7'
  64. ]);
  65. //Check if declaration is applied
  66. $indexes = $this->dbSchemaReader->readIndexes('test_table', 'default');
  67. self::assertCount(1, $indexes);
  68. self::assertArrayHasKey('TEST_TABLE_TINYINT_BIGINT', $indexes);
  69. //Check UpgradeSchema old format, that modify declaration
  70. $columns = $this->dbSchemaReader->readColumns('test_table', 'default');
  71. $floatColumn = $columns['float'];
  72. self::assertEquals(29, $floatColumn['default']);
  73. }
  74. private function doUsToUsRevision()
  75. {
  76. $this->moduleManager->updateRevision(
  77. 'Magento_TestSetupDeclarationModule7',
  78. 'us_to_us',
  79. 'UpgradeSchema.php',
  80. 'Setup'
  81. );
  82. $this->moduleManager->updateRevision(
  83. 'Magento_TestSetupDeclarationModule7',
  84. 'us_to_us',
  85. 'module.xml',
  86. 'etc'
  87. );
  88. $this->moduleManager->updateRevision(
  89. 'Magento_TestSetupDeclarationModule7',
  90. 'us_to_us',
  91. 'UpgradeData.php',
  92. 'Setup'
  93. );
  94. }
  95. private function doUsToDsRevision()
  96. {
  97. $this->moduleManager->updateRevision(
  98. 'Magento_TestSetupDeclarationModule7',
  99. 'swap_with_declaration',
  100. 'db_schema.xml',
  101. 'etc'
  102. );
  103. $this->moduleManager->updateRevision(
  104. 'Magento_TestSetupDeclarationModule7',
  105. 'swap_with_declaration',
  106. 'SomePatch.php',
  107. 'Setup/Patch/Data'
  108. );
  109. $this->moduleManager->updateRevision(
  110. 'Magento_TestSetupDeclarationModule7',
  111. 'swap_with_declaration',
  112. 'SomeSkippedPatch.php',
  113. 'Setup/Patch/Data'
  114. );
  115. }
  116. /**
  117. * Assert that data and schema of 2 modules are installed successfully
  118. */
  119. private function assertUsToUsUpgrade()
  120. {
  121. $usToUsTables = $this->dbSchemaReader->readTables('default');
  122. self::assertContains('custom_table', $usToUsTables);
  123. self::assertTrue($this->dbVersionInfo->isDataUpToDate('Magento_TestSetupDeclarationModule7'));
  124. self::assertTrue($this->dbVersionInfo->isSchemaUpToDate('Magento_TestSetupDeclarationModule7'));
  125. self::assertEquals(
  126. [6,12],
  127. $this->tableData->describeTableData('reference_table', 'bigint_without_padding')
  128. );
  129. }
  130. /**
  131. * Assert that data and schema of 2 modules are installed successfully
  132. */
  133. private function assertUsToDsUpgrade()
  134. {
  135. //Check UpgradeSchema old format, that modify declaration
  136. $columns = $this->dbSchemaReader->readColumns('test_table', 'default');
  137. $floatColumn = $columns['float'];
  138. //Check whether declaration will be applied
  139. self::assertEquals(35, $floatColumn['default']);
  140. self::assertTrue($this->dbVersionInfo->isDataUpToDate('Magento_TestSetupDeclarationModule7'));
  141. self::assertTrue($this->dbVersionInfo->isSchemaUpToDate('Magento_TestSetupDeclarationModule7'));
  142. self::assertEquals(
  143. [6,12],
  144. $this->tableData->describeTableData('reference_table', 'bigint_without_padding')
  145. );
  146. self::assertEquals(
  147. ['_ref'],
  148. $this->tableData->describeTableData('test_table', 'varchar')
  149. );
  150. }
  151. /**
  152. * @moduleName Magento_TestSetupDeclarationModule6
  153. * @moduleName Magento_TestSetupDeclarationModule7
  154. */
  155. public function testDSFirstRelease()
  156. {
  157. $this->cliCommand->install([
  158. 'Magento_TestSetupDeclarationModule6',
  159. 'Magento_TestSetupDeclarationModule7'
  160. ]);
  161. //Check no change upgrade with US
  162. $this->cliCommand->upgrade();
  163. $this->doUsToUsRevision();
  164. //Check US to US upgrade
  165. $this->cliCommand->upgrade();
  166. $this->assertUsToUsUpgrade();
  167. $this->doUsToDsRevision();
  168. //Check US to declarative schema upgrade
  169. $this->cliCommand->upgrade();
  170. $this->assertUsToDsUpgrade();
  171. $this->moduleManager->updateRevision(
  172. 'Magento_TestSetupDeclarationModule7',
  173. 'wl_remove_table',
  174. 'db_schema_whitelist.json',
  175. 'etc'
  176. );
  177. //Check removal case, when we need to remove table with declaration and table was created in old scripts
  178. $this->cliCommand->upgrade();
  179. $tables = $this->dbSchemaReader->readTables('default');
  180. self::assertNotContains('custom_table', $tables);
  181. }
  182. /**
  183. * @moduleName Magento_TestSetupDeclarationModule1
  184. * @dataProvider firstCleanInstallOneModuleDataProvider
  185. * @param string $dbPrefix
  186. * @param string $tableName
  187. * @param string $indexName
  188. * @param string $constraintName
  189. * @param string $foreignKeyName
  190. * @throws \Exception
  191. */
  192. public function testFirstCleanInstallOneModule(
  193. string $dbPrefix,
  194. string $tableName,
  195. string $indexName,
  196. string $constraintName,
  197. string $foreignKeyName
  198. ) {
  199. $this->cliCommand->install(
  200. [
  201. 'Magento_TestSetupDeclarationModule1'
  202. ],
  203. [
  204. 'db-prefix' => $dbPrefix,
  205. ]
  206. );
  207. $indexes = $this->dbSchemaReader
  208. ->readIndexes($tableName, 'default');
  209. self::assertCount(1, $indexes);
  210. self::assertArrayHasKey($indexName, $indexes);
  211. $constraints = $this->dbSchemaReader
  212. ->readConstraints($tableName, 'default');
  213. self::assertCount(1, $constraints);
  214. self::assertArrayHasKey($constraintName, $constraints);
  215. $foreignKeys = $this->dbSchemaReader
  216. ->readReferences($tableName, 'default');
  217. self::assertCount(1, $foreignKeys);
  218. self::assertArrayHasKey($foreignKeyName, $foreignKeys);
  219. }
  220. /**
  221. * @return array
  222. */
  223. public function firstCleanInstallOneModuleDataProvider()
  224. {
  225. return [
  226. 'Installation without db prefix' => [
  227. 'dbPrefix' => '',
  228. 'tableName' => 'test_table',
  229. 'indexName' => 'TEST_TABLE_TINYINT_BIGINT',
  230. 'constraintName' => 'TEST_TABLE_SMALLINT_BIGINT',
  231. 'foreignKeyName' => 'TEST_TABLE_TINYINT_REFERENCE_TABLE_TINYINT_REF',
  232. ],
  233. 'Installation with db prefix' => [
  234. 'dbPrefix' => 'spec_',
  235. 'tableName' => 'spec_test_table',
  236. 'indexName' => 'SPEC_TEST_TABLE_TINYINT_BIGINT',
  237. 'constraintName' => 'SPEC_TEST_TABLE_SMALLINT_BIGINT',
  238. 'foreignKeyName' => 'SPEC_TEST_TABLE_TINYINT_SPEC_REFERENCE_TABLE_TINYINT_REF',
  239. ]
  240. ];
  241. }
  242. }