SetupUpgradeTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Developer\Console\Command;
  8. use Magento\Framework\Component\ComponentRegistrar;
  9. use Magento\TestFramework\Deploy\CliCommand;
  10. use Magento\TestFramework\Deploy\TestModuleManager;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. use Magento\TestFramework\TestCase\SetupTestCase;
  13. /**
  14. * Test for Upgrade command.
  15. */
  16. class SetupUpgradeTest extends SetupTestCase
  17. {
  18. /**
  19. * @var TestModuleManager
  20. */
  21. private $moduleManager;
  22. /**
  23. * @var CliCommand
  24. */
  25. private $cliCommand;
  26. /**
  27. * @var ComponentRegistrar
  28. */
  29. private $componentRegistrar;
  30. /**
  31. * @inheritdoc
  32. */
  33. public function setUp()
  34. {
  35. $objectManager = Bootstrap::getObjectManager();
  36. $this->cliCommand = $objectManager->get(CliCommand::class);
  37. $this->moduleManager = $objectManager->get(TestModuleManager::class);
  38. $this->componentRegistrar = $objectManager->create(
  39. ComponentRegistrar::class
  40. );
  41. }
  42. /**
  43. * @moduleName Magento_TestSetupDeclarationModule8
  44. * @moduleName Magento_TestSetupDeclarationModule9
  45. * @throws \Exception
  46. */
  47. public function testUpgradeWithConverting()
  48. {
  49. $modules = [
  50. 'Magento_TestSetupDeclarationModule8',
  51. 'Magento_TestSetupDeclarationModule9',
  52. ];
  53. foreach ($modules as $moduleName) {
  54. $this->moduleManager->updateRevision(
  55. $moduleName,
  56. 'setup_install_with_converting',
  57. 'InstallSchema.php',
  58. 'Setup'
  59. );
  60. }
  61. $this->cliCommand->install($modules, ['convert-old-scripts' => true]);
  62. foreach ($modules as $moduleName) {
  63. $this->assertInstallScriptChanges($moduleName);
  64. }
  65. foreach ($modules as $moduleName) {
  66. $this->moduleManager->updateRevision(
  67. $moduleName,
  68. 'setup_install_with_converting',
  69. 'UpgradeSchema.php',
  70. 'Setup'
  71. );
  72. $this->moduleManager->updateRevision(
  73. $moduleName,
  74. 'setup_install_with_converting',
  75. 'module.xml',
  76. 'etc'
  77. );
  78. }
  79. $this->cliCommand->upgrade(['convert-old-scripts' => true]);
  80. foreach ($modules as $moduleName) {
  81. $this->assertUpgradeScriptChanges($moduleName);
  82. }
  83. }
  84. /**
  85. * Convert file content in the DOM document.
  86. *
  87. * @param string $schemaFileName
  88. * @return \DOMDocument
  89. */
  90. private function getSchemaDocument(string $schemaFileName): \DOMDocument
  91. {
  92. $schemaDocument = new \DOMDocument();
  93. $schemaDocument->preserveWhiteSpace = false;
  94. $schemaDocument->formatOutput = true;
  95. $schemaDocument->loadXML(file_get_contents($schemaFileName));
  96. return $schemaDocument;
  97. }
  98. /**
  99. * @param string $moduleName
  100. */
  101. private function assertInstallScriptChanges(string $moduleName): void
  102. {
  103. $generatedSchema = $this->getGeneratedSchema($moduleName);
  104. $expectedSchema = $this->getSchemaDocument($this->getSchemaFixturePath($moduleName, 'install'));
  105. $this->assertEquals($expectedSchema->saveXML(), $generatedSchema->saveXML());
  106. }
  107. /**
  108. * @param string $moduleName
  109. */
  110. private function assertUpgradeScriptChanges(string $moduleName): void
  111. {
  112. $generatedSchema = $this->getGeneratedSchema($moduleName);
  113. $expectedSchema = $this->getSchemaDocument($this->getSchemaFixturePath($moduleName, 'upgrade'));
  114. $this->assertEquals($expectedSchema->saveXML(), $generatedSchema->saveXML());
  115. }
  116. /**
  117. * @param string $moduleName
  118. * @return \DOMDocument
  119. */
  120. private function getGeneratedSchema(string $moduleName): \DOMDocument
  121. {
  122. $modulePath = $this->componentRegistrar->getPath('module', $moduleName);
  123. $schemaFileName = $modulePath
  124. . DIRECTORY_SEPARATOR
  125. . \Magento\Framework\Module\Dir::MODULE_ETC_DIR
  126. . DIRECTORY_SEPARATOR
  127. . 'db_schema.xml';
  128. return $this->getSchemaDocument($schemaFileName);
  129. }
  130. /**
  131. * @param string $moduleName
  132. * @param string $suffix
  133. * @return string
  134. */
  135. private function getSchemaFixturePath(string $moduleName, string $suffix): string
  136. {
  137. $schemaFixturePath = dirname(__DIR__, 2)
  138. . DIRECTORY_SEPARATOR
  139. . implode(
  140. DIRECTORY_SEPARATOR,
  141. [
  142. '_files',
  143. 'SetupUpgrade',
  144. str_replace('Magento_', '', $moduleName),
  145. 'db_schema_' . $suffix . '.xml'
  146. ]
  147. );
  148. return $schemaFixturePath;
  149. }
  150. }