DiffOldSchemaTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\Setup\Declaration\Schema\Diff\DiffFactory;
  8. use Magento\Framework\Setup\Declaration\Schema\Diff\SchemaDiff;
  9. use Magento\Framework\Setup\Declaration\Schema\SchemaConfigInterface;
  10. use Magento\TestFramework\Deploy\CliCommand;
  11. use Magento\TestFramework\Deploy\TestModuleManager;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\TestFramework\TestCase\SetupTestCase;
  14. /**
  15. * The purpose of this test is verifying initial InstallSchema, InstallData scripts.
  16. */
  17. class DiffOldSchemaTest extends SetupTestCase
  18. {
  19. /**
  20. * @var TestModuleManager
  21. */
  22. private $moduleManager;
  23. /**
  24. * @var CliCommand
  25. */
  26. private $cliCommad;
  27. /**
  28. * @var SchemaDiff
  29. */
  30. private $schemaDiff;
  31. /**
  32. * @var DiffFactory
  33. */
  34. private $changeRegistryFactory;
  35. /**
  36. * @var SchemaConfigInterface
  37. */
  38. private $schemaConfig;
  39. public function setUp()
  40. {
  41. $objectManager = Bootstrap::getObjectManager();
  42. $this->moduleManager = $objectManager->get(TestModuleManager::class);
  43. $this->cliCommad = $objectManager->get(CliCommand::class);
  44. $this->schemaConfig = $objectManager->get(SchemaConfigInterface::class);
  45. $this->schemaDiff = $objectManager->get(SchemaDiff::class);
  46. $this->changeRegistryFactory = $objectManager->get(DiffFactory::class);
  47. }
  48. /**
  49. * @moduleName Magento_TestSetupDeclarationModule1
  50. */
  51. public function testOldDiff()
  52. {
  53. //Move db_schema.xml
  54. $this->moduleManager->updateRevision(
  55. 'Magento_TestSetupDeclarationModule1',
  56. 'old_diff_before',
  57. 'db_schema.xml',
  58. 'etc'
  59. );
  60. //Move InstallSchema file and tried to install
  61. $this->moduleManager->updateRevision(
  62. 'Magento_TestSetupDeclarationModule1',
  63. 'old_diff',
  64. 'InstallSchema.php',
  65. 'Setup'
  66. );
  67. $this->cliCommad->install(['Magento_TestSetupDeclarationModule1']);
  68. //Move db_schema.xml
  69. $this->moduleManager->updateRevision(
  70. 'Magento_TestSetupDeclarationModule1',
  71. 'old_diff',
  72. 'db_schema.xml',
  73. 'etc'
  74. );
  75. $declarativeSchema = $this->schemaConfig->getDeclarationConfig();
  76. $generatedSchema = $this->schemaConfig->getDbConfig();
  77. $diff = $this->schemaDiff->diff($declarativeSchema, $generatedSchema);
  78. $allChanges = $diff->getAll();
  79. self::assertCount(1, $allChanges);
  80. self::assertEquals(
  81. $this->getBigIntKeyXmlSensitiveData(),
  82. reset($allChanges)['modify_column'][0]->getNew()->getDiffSensitiveParams()
  83. );
  84. self::assertEquals(
  85. $this->getBigIntKeyDbSensitiveData(),
  86. reset($allChanges)['modify_column'][0]->getOld()->getDiffSensitiveParams()
  87. );
  88. }
  89. /**
  90. * @moduleName Magento_TestSetupDeclarationModule1
  91. * @param string $dbPrefix
  92. * @throws \Exception
  93. * @dataProvider oldSchemaUpgradeDataProvider
  94. */
  95. public function testOldSchemaUpgrade(string $dbPrefix)
  96. {
  97. $this->moduleManager->updateRevision(
  98. 'Magento_TestSetupDeclarationModule1',
  99. 'old_diff_before',
  100. 'db_schema.xml',
  101. 'etc'
  102. );
  103. $this->moduleManager->updateRevision(
  104. 'Magento_TestSetupDeclarationModule1',
  105. 'base_update',
  106. 'InstallSchema.php',
  107. 'Setup'
  108. );
  109. $this->cliCommad->install(
  110. ['Magento_TestSetupDeclarationModule1'],
  111. ['db-prefix' => $dbPrefix]
  112. );
  113. //Move db_schema.xml
  114. $this->moduleManager->updateRevision(
  115. 'Magento_TestSetupDeclarationModule1',
  116. 'base_update',
  117. 'db_schema.xml',
  118. 'etc'
  119. );
  120. $declarativeSchema = $this->schemaConfig->getDeclarationConfig();
  121. $generatedSchema = $this->schemaConfig->getDbConfig();
  122. $diff = $this->schemaDiff->diff($declarativeSchema, $generatedSchema);
  123. self::assertEmpty($diff->getAll());
  124. }
  125. /**
  126. * @return array
  127. */
  128. public function oldSchemaUpgradeDataProvider(): array
  129. {
  130. return [
  131. 'Without db prefix' => [
  132. 'dbPrefix' => '',
  133. ],
  134. 'With db prefix' => [
  135. 'dbPrefix' => 'spec_',
  136. ],
  137. ];
  138. }
  139. /**
  140. * @return array
  141. */
  142. private function getBigIntKeyDbSensitiveData()
  143. {
  144. return [
  145. 'type' => 'bigint',
  146. 'nullable' => true,
  147. 'padding' => 20,
  148. 'unsigned' => false,
  149. 'identity' => false,
  150. 'default' => 0,
  151. 'comment' => 'Bigint'
  152. ];
  153. }
  154. /**
  155. * @return array
  156. */
  157. private function getBigIntKeyXmlSensitiveData()
  158. {
  159. return [
  160. 'type' => 'bigint',
  161. 'nullable' => true,
  162. 'padding' => 20,
  163. 'unsigned' => false,
  164. 'identity' => false,
  165. 'default' => 1,
  166. 'comment' => 'Bigint',
  167. ];
  168. }
  169. }