InstallUpgradeTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Legacy;
  7. use Magento\Framework\App\Utility\Files;
  8. use Magento\Framework\App\Utility\AggregateInvoker;
  9. use Magento\Framework\Component\ComponentRegistrar;
  10. /**
  11. * Tests to find obsolete install/upgrade schema/data scripts.
  12. */
  13. class InstallUpgradeTest extends \PHPUnit\Framework\TestCase
  14. {
  15. public function testForOldInstallUpgradeScripts()
  16. {
  17. $scriptPattern = [];
  18. $componentRegistrar = new ComponentRegistrar();
  19. foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
  20. $scriptPattern[] = $moduleDir . '/sql';
  21. $scriptPattern[] = $moduleDir . '/data';
  22. $scriptPattern[] = $moduleDir . '/Setup';
  23. }
  24. $invoker = new AggregateInvoker($this);
  25. $invoker(
  26. /**
  27. * @param string $file
  28. */
  29. function ($file) {
  30. $this->assertStringStartsNotWith(
  31. 'install-',
  32. basename($file),
  33. 'Install scripts are obsolete. '
  34. . 'Please use declarative schema approach in module\'s etc/db_schema.xml file'
  35. );
  36. $this->assertStringStartsNotWith(
  37. 'InstallSchema',
  38. basename($file),
  39. 'InstallSchema objects are obsolete. '
  40. . 'Please use declarative schema approach in module\'s etc/db_schema.xml file'
  41. );
  42. $this->assertStringStartsNotWith(
  43. 'InstallData',
  44. basename($file),
  45. 'InstallData objects are obsolete. '
  46. . 'Please use data patches approach in module\'s Setup/Patch/Data dir'
  47. );
  48. $this->assertStringStartsNotWith(
  49. 'data-install-',
  50. basename($file),
  51. 'Install scripts are obsolete. Please create class InstallData in module\'s Setup folder'
  52. );
  53. $this->assertStringStartsNotWith(
  54. 'upgrade-',
  55. basename($file),
  56. 'Upgrade scripts are obsolete. '
  57. . 'Please use declarative schema approach in module\'s etc/db_schema.xml file'
  58. );
  59. $this->assertStringStartsNotWith(
  60. 'UpgradeSchema',
  61. basename($file),
  62. 'UpgradeSchema scripts are obsolete. '
  63. . 'Please use declarative schema approach in module\'s etc/db_schema.xml file'
  64. );
  65. $this->assertStringStartsNotWith(
  66. 'UpgradeData',
  67. basename($file),
  68. 'UpgradeSchema scripts are obsolete. '
  69. . 'Please use data patches approach in module\'s Setup/Patch/Data dir'
  70. );
  71. $this->assertStringStartsNotWith(
  72. 'data-upgrade-',
  73. basename($file),
  74. 'Upgrade scripts are obsolete. '
  75. . 'Please use data patches approach in module\'s Setup/Patch/Data dir'
  76. );
  77. $this->assertStringStartsNotWith(
  78. 'recurring',
  79. basename($file),
  80. 'Recurring scripts are obsolete. Please create class Recurring in module\'s Setup folder'
  81. );
  82. if (preg_match('/.*\/(sql\/|data\/)/', dirname($file))) {
  83. $this->fail(
  84. "Invalid directory:\n"
  85. . "- Create an data patch within module's Setup/Patch/Data folder for data upgrades.\n"
  86. . "- Use declarative schema approach in module's etc/db_schema.xml file for schema changes."
  87. );
  88. }
  89. },
  90. $this->convertArray(
  91. Files::init()->getFiles($scriptPattern, '*.php')
  92. )
  93. );
  94. }
  95. /**
  96. * Converts from string array to array of arrays.
  97. *
  98. * @param array $stringArray
  99. * @return array
  100. */
  101. private function convertArray($stringArray)
  102. {
  103. $array = [];
  104. foreach ($stringArray as $item) {
  105. $array[] = [$item];
  106. }
  107. return $array;
  108. }
  109. }