ObsoleteConnectionTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\Component\ComponentRegistrar;
  8. /**
  9. * Temporary test
  10. * Test verifies obsolete usages in modules that were refactored to work with getConnection.
  11. */
  12. class ObsoleteConnectionTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var array
  16. */
  17. protected $obsoleteMethods = [];
  18. /**
  19. * @var array
  20. */
  21. protected $obsoleteRegexp = [];
  22. /**
  23. * @var array
  24. */
  25. protected $filesBlackList = [];
  26. /**
  27. * @var string
  28. */
  29. protected $appPath;
  30. protected function setUp()
  31. {
  32. $this->obsoleteMethods = [
  33. '_getReadConnection',
  34. '_getWriteConnection',
  35. '_getReadAdapter',
  36. '_getWriteAdapter',
  37. 'getReadConnection',
  38. 'getWriteConnection',
  39. 'getReadAdapter',
  40. 'getWriteAdapter',
  41. ];
  42. $this->obsoleteRegexp = [
  43. // 'getConnection\\(\'\\w*_*(read|write)',
  44. '\\$_?(read|write)(Connection|Adapter)',
  45. '\\$write([A-Z]\\w*|\\s)',
  46. ];
  47. $this->filesBlackList = $this->getBlackList();
  48. }
  49. /**
  50. * Test verify that obsolete regexps do not appear in refactored folders
  51. */
  52. public function testObsoleteRegexp()
  53. {
  54. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  55. $invoker(
  56. function ($file) {
  57. $content = file_get_contents($file);
  58. foreach ($this->obsoleteRegexp as $regexp) {
  59. $this->assertSame(
  60. 0,
  61. preg_match('/' . $regexp . '/iS', $content),
  62. "File: $file\nContains obsolete regexp: $regexp. "
  63. );
  64. }
  65. },
  66. $this->modulesFilesDataProvider()
  67. );
  68. }
  69. /**
  70. * Test verify that obsolete methods do not appear in refactored folders
  71. */
  72. public function testObsoleteResponseMethods()
  73. {
  74. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  75. $invoker(
  76. function ($file) {
  77. $content = file_get_contents($file);
  78. foreach ($this->obsoleteMethods as $method) {
  79. $quotedMethod = preg_quote($method, '/');
  80. $this->assertSame(
  81. 0,
  82. preg_match('/(?<=[a-z\\d_:]|->|function\\s)' . $quotedMethod . '\\s*\\(/iS', $content),
  83. "File: $file\nContains obsolete method: $method . "
  84. );
  85. }
  86. },
  87. $this->modulesFilesDataProvider()
  88. );
  89. }
  90. /**
  91. * Return refactored files
  92. *
  93. * @return array
  94. */
  95. public function modulesFilesDataProvider()
  96. {
  97. $filesList = [];
  98. $componentRegistrar = new ComponentRegistrar();
  99. foreach ($this->getFilesData('whitelist/refactored_modules*') as $refactoredModule) {
  100. if ($componentRegistrar->getPath(ComponentRegistrar::MODULE, $refactoredModule)) {
  101. $files = \Magento\Framework\App\Utility\Files::init()->getFiles(
  102. [$componentRegistrar->getPath(ComponentRegistrar::MODULE, $refactoredModule)],
  103. '*.php'
  104. );
  105. $filesList = array_merge($filesList, $files);
  106. }
  107. }
  108. $result = array_map('realpath', $filesList);
  109. $result = array_diff($result, $this->filesBlackList);
  110. return \Magento\Framework\App\Utility\Files::composeDataSets($result);
  111. }
  112. /**
  113. * @return array
  114. */
  115. protected function getBlackList()
  116. {
  117. $blackListFiles = [];
  118. $componentRegistrar = new ComponentRegistrar();
  119. foreach ($this->getFilesData('blacklist/files_list*') as $fileInfo) {
  120. $blackListFiles[] = $componentRegistrar->getPath(ComponentRegistrar::MODULE, $fileInfo[0])
  121. . DIRECTORY_SEPARATOR . $fileInfo[1];
  122. }
  123. return $blackListFiles;
  124. }
  125. /**
  126. * @param string $filePattern
  127. * @return array
  128. */
  129. protected function getFilesData($filePattern)
  130. {
  131. $result = [];
  132. foreach (glob(__DIR__ . '/_files/connection/' . $filePattern) as $file) {
  133. $fileData = include $file;
  134. $result = array_merge($result, $fileData);
  135. }
  136. return $result;
  137. }
  138. }