DataPatchInstallationTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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\ModuleResource;
  8. use Magento\Framework\Setup\Patch\PatchHistory;
  9. use Magento\TestFramework\Deploy\CliCommand;
  10. use Magento\TestFramework\Deploy\TableData;
  11. use Magento\TestFramework\Deploy\TestModuleManager;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\TestFramework\TestCase\SetupTestCase;
  14. use Magento\TestSetupDeclarationModule3\Setup\Patch\Data\IncrementalSomeIntegerPatch;
  15. use Magento\TestSetupDeclarationModule3\Setup\Patch\Data\ReferenceIncrementalSomeIntegerPatch;
  16. use Magento\TestSetupDeclarationModule3\Setup\Patch\Data\ZFirstPatch;
  17. /**
  18. * The purpose of this test is validating schema reader operations.
  19. */
  20. class DataPatchInstallationTest extends SetupTestCase
  21. {
  22. /**
  23. * @var TestModuleManager
  24. */
  25. private $moduleManager;
  26. /**
  27. * @var CliCommand
  28. */
  29. private $cliCommad;
  30. /**
  31. * @var ModuleResource
  32. */
  33. private $moduleResource;
  34. /**
  35. * @var PatchHistory
  36. */
  37. private $patchList;
  38. /**
  39. * @var TableData
  40. */
  41. private $tableData;
  42. public function setUp()
  43. {
  44. $objectManager = Bootstrap::getObjectManager();
  45. $this->moduleManager = $objectManager->get(TestModuleManager::class);
  46. $this->cliCommad = $objectManager->get(CliCommand::class);
  47. $this->moduleResource = $objectManager->get(ModuleResource::class);
  48. $this->patchList = $objectManager->get(PatchHistory::class);
  49. $this->tableData = $objectManager->get(TableData::class);
  50. }
  51. /**
  52. * @moduleName Magento_TestSetupDeclarationModule3
  53. */
  54. public function testDataPatchesInstallation()
  55. {
  56. $this->cliCommad->install(
  57. ['Magento_TestSetupDeclarationModule3']
  58. );
  59. self::assertEquals(
  60. '0.0.1',
  61. $this->moduleResource->getDataVersion('Magento_TestSetupDeclarationModule3')
  62. );
  63. $this->moduleManager->updateRevision(
  64. 'Magento_TestSetupDeclarationModule3',
  65. 'first_patch_revision',
  66. 'module.xml',
  67. 'etc'
  68. );
  69. $this->movePatches();
  70. ModuleResource::flush();
  71. $this->cliCommad->upgrade();
  72. self::assertEquals(
  73. '0.0.3',
  74. $this->moduleResource->getDataVersion('Magento_TestSetupDeclarationModule3')
  75. );
  76. self::assertTrue($this->patchList->isApplied(IncrementalSomeIntegerPatch::class));
  77. self::assertTrue($this->patchList->isApplied(ReferenceIncrementalSomeIntegerPatch::class));
  78. self::assertTrue($this->patchList->isApplied(ZFirstPatch::class));
  79. $tableData = $this->tableData->describeTableData('test_table');
  80. self::assertEquals($this->getTestTableData(), $tableData);
  81. }
  82. /**
  83. * @moduleName Magento_TestSetupDeclarationModule3
  84. * @expectedException \Magento\Framework\Exception\LocalizedException
  85. */
  86. public function testCyclomaticDependency()
  87. {
  88. $this->moduleManager->updateRevision(
  89. 'Magento_TestSetupDeclarationModule3',
  90. 'cyclomatic_and_bic_revision',
  91. 'module.xml',
  92. 'etc'
  93. );
  94. $this->movePatches();
  95. /**
  96. * Test whether installation give the same result as upgrade
  97. */
  98. $this->cliCommad->install(
  99. ['Magento_TestSetupDeclarationModule3']
  100. );
  101. $tableData = $this->tableData->describeTableData('test_table');
  102. self::assertEquals($this->getTestTableData(), $tableData);
  103. $this->moduleManager->updateRevision(
  104. 'Magento_TestSetupDeclarationModule3',
  105. 'cyclomatic_and_bic_revision',
  106. 'BicPatch.php',
  107. 'Setup/Patch/Data'
  108. );
  109. $this->moduleManager->updateRevision(
  110. 'Magento_TestSetupDeclarationModule3',
  111. 'cyclomatic_and_bic_revision',
  112. 'RefBicPatch.php',
  113. 'Setup/Patch/Data'
  114. );
  115. $this->cliCommad->upgrade();
  116. }
  117. /**
  118. * Move patches
  119. */
  120. private function movePatches()
  121. {
  122. //Install with patches
  123. $this->moduleManager->addRevision(
  124. 'Magento_TestSetupDeclarationModule3',
  125. 'patches_revision',
  126. 'Setup/Patch/Data'
  127. );
  128. //Upgrade with UpgradeData
  129. $this->moduleManager->updateRevision(
  130. 'Magento_TestSetupDeclarationModule3',
  131. 'first_patch_revision',
  132. 'UpgradeData.php',
  133. 'Setup'
  134. );
  135. //Upgrade with UpgradeData
  136. $this->moduleManager->updateRevision(
  137. 'Magento_TestSetupDeclarationModule3',
  138. 'first_patch_revision',
  139. 'module.xml',
  140. 'etc'
  141. );
  142. }
  143. /**
  144. * @moduleName Magento_TestSetupDeclarationModule3
  145. */
  146. public function testPatchesRevert()
  147. {
  148. $this->movePatches();
  149. $this->cliCommad->install(['Magento_TestSetupDeclarationModule3']);
  150. $this->cliCommad->uninstallModule('Magento_TestSetupDeclarationModule3');
  151. $testTableData = $this->tableData->describeTableData('test_table');
  152. $patchListTableData = $this->tableData->describeTableData('patch_list');
  153. self::assertEmpty($patchListTableData);
  154. self::assertEmpty($testTableData);
  155. $refTableData = $this->tableData->describeTableData('reference_table');
  156. self::assertEquals($this->getRefTableData(), $refTableData);
  157. }
  158. /**
  159. * @return array
  160. */
  161. private function getTestTableData()
  162. {
  163. return [
  164. [
  165. 'smallint' => '1',
  166. 'tinyint' => null,
  167. 'varchar' => 'Ololo123',
  168. 'varbinary' => '33288',
  169. ],
  170. [
  171. 'smallint' => '2',
  172. 'tinyint' => null,
  173. 'varchar' => 'Ololo123_ref',
  174. 'varbinary' => '33288',
  175. ],
  176. [
  177. 'smallint' => '3',
  178. 'tinyint' => null,
  179. 'varchar' => 'changed__very_secret_string',
  180. 'varbinary' => '0',
  181. ],
  182. ];
  183. }
  184. /**
  185. * Retrieve reference table data
  186. *
  187. * @return array
  188. */
  189. private function getRefTableData()
  190. {
  191. return [
  192. [
  193. 'tinyint_ref' => '2',
  194. 'some_integer' => '2',
  195. 'for_patch_testing' => null,
  196. ],
  197. [
  198. 'tinyint_ref' => '3',
  199. 'some_integer' => '3',
  200. 'for_patch_testing' => null,
  201. ],
  202. [
  203. 'tinyint_ref' => '4',
  204. 'some_integer' => '5',
  205. 'for_patch_testing' => null,
  206. ],
  207. [
  208. 'tinyint_ref' => '5',
  209. 'some_integer' => '6',
  210. 'for_patch_testing' => null,
  211. ],
  212. [
  213. 'tinyint_ref' => '6',
  214. 'some_integer' => '12',
  215. 'for_patch_testing' => null,
  216. ],
  217. ];
  218. }
  219. }