PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/static/testsuite/Magento/Test/Legacy/TableTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 222 lines | 165 code | 17 blank | 40 comment | 4 complexity | ad1851b59f210d6a5e7b65b74341fa5f MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Coverage of obsolete table names usage
  8. */
  9. namespace Magento\Test\Legacy;
  10. use Magento\Framework\App\Utility\Files;
  11. class TableTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testTableName()
  14. {
  15. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  16. $invoker(
  17. /**
  18. * @param string $filePath
  19. */
  20. function ($filePath) {
  21. $tables = self::extractTables($filePath);
  22. $legacyTables = [];
  23. foreach ($tables as $table) {
  24. $tableName = $table['name'];
  25. if (strpos($tableName, '/') === false) {
  26. continue;
  27. }
  28. $legacyTables[] = $table;
  29. }
  30. $message = $this->_composeFoundsMessage($legacyTables);
  31. $this->assertEmpty($message, $message);
  32. },
  33. Files::init()->getPhpFiles(
  34. Files::INCLUDE_APP_CODE
  35. | Files::INCLUDE_PUB_CODE
  36. | Files::INCLUDE_LIBS
  37. | Files::INCLUDE_TEMPLATES
  38. | Files::AS_DATA_SET
  39. | Files::INCLUDE_NON_CLASSES
  40. )
  41. );
  42. }
  43. /**
  44. * Returns found table names in a file
  45. *
  46. * @param string $filePath
  47. * @return array
  48. */
  49. public static function extractTables($filePath)
  50. {
  51. $regexpMethods = ['_getRegexpTableInMethods', '_getRegexpTableInArrays', '_getRegexpTableInProperties'];
  52. $result = [];
  53. $content = file_get_contents($filePath);
  54. foreach ($regexpMethods as $method) {
  55. $regexp = self::$method($filePath);
  56. if (!preg_match_all($regexp, $content, $matches, PREG_SET_ORDER)) {
  57. continue;
  58. }
  59. $iterationResult = self::_matchesToInformation($content, $matches);
  60. $result = array_merge($result, $iterationResult);
  61. }
  62. return $result;
  63. }
  64. /**
  65. * Returns regexp to find table names in method calls in a file
  66. *
  67. * @param string $filePath
  68. * @return string
  69. */
  70. protected static function _getRegexpTableInMethods($filePath)
  71. {
  72. $methods = [
  73. 'getTableName',
  74. '_setMainTable',
  75. 'setMainTable',
  76. 'getTable',
  77. 'setTable',
  78. 'getTableRow',
  79. 'deleteTableRow',
  80. 'updateTableRow',
  81. 'updateTable',
  82. 'tableExists',
  83. ['name' => 'joinField', 'param_index' => 1],
  84. 'joinTable',
  85. 'getFkName',
  86. ['name' => 'getFkName', 'param_index' => 2],
  87. 'getIdxName',
  88. ['name' => 'addVirtualGridColumn', 'param_index' => 1],
  89. ];
  90. if (self::_isResourceButNotCollection($filePath)) {
  91. $methods[] = '_init';
  92. }
  93. $regexps = [];
  94. foreach ($methods as $method) {
  95. $regexps[] = self::_composeRegexpForMethod($method);
  96. }
  97. $result = '#->\s*(' . implode('|', $regexps) . ')#';
  98. return $result;
  99. }
  100. /**
  101. * @param string $filePath
  102. * @return bool
  103. */
  104. protected static function _isResourceButNotCollection($filePath)
  105. {
  106. $filePath = str_replace('\\', '/', $filePath);
  107. $parts = explode('/', $filePath);
  108. return array_search('Resource', $parts) !== false && array_search('Collection.php', $parts) === false;
  109. }
  110. /**
  111. * Returns regular expression to find legacy method calls with table in it
  112. *
  113. * @param string|array $method Method name, or array with method name and index of table parameter in signature
  114. * @return string
  115. */
  116. protected static function _composeRegexpForMethod($method)
  117. {
  118. if (!is_array($method)) {
  119. $method = ['name' => $method, 'param_index' => 0];
  120. }
  121. if ($method['param_index']) {
  122. $skipParamsRegexp = '\s*[[:alnum:]$_\'"]+\s*,';
  123. $skipParamsRegexp = str_repeat($skipParamsRegexp, $method['param_index']);
  124. } else {
  125. $skipParamsRegexp = '';
  126. }
  127. $result = $method['name'] . '\(' . $skipParamsRegexp . '\s*[\'"]([^\'"]+)';
  128. return $result;
  129. }
  130. /**
  131. * Returns regexp to find table names in array definitions
  132. *
  133. * @param string $filePath
  134. * @return string
  135. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  136. */
  137. protected static function _getRegexpTableInArrays($filePath)
  138. {
  139. return '/[\'"](?:[a-z\d_]+_)?table[\'"]\s*=>\s*[\'"]([^\'"]+)/';
  140. }
  141. /**
  142. * Returns regexp to find table names in property assignments
  143. *
  144. * @param string $filePath
  145. * @return string
  146. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  147. */
  148. protected static function _getRegexpTableInProperties($filePath)
  149. {
  150. $properties = ['_aggregationTable'];
  151. $regexps = [];
  152. foreach ($properties as $property) {
  153. $regexps[] = $property . '\s*=\s*[\'"]([^\'"]+)';
  154. }
  155. $result = '#' . implode('|', $regexps) . '#';
  156. return $result;
  157. }
  158. /**
  159. * Converts regexp matches to information, understandable by human: extracts legacy table name and line,
  160. * where it was found
  161. *
  162. * @param string $content
  163. * @param array $matches
  164. * @return array
  165. */
  166. protected static function _matchesToInformation($content, $matches)
  167. {
  168. $result = [];
  169. $fromPos = 0;
  170. foreach ($matches as $match) {
  171. $pos = strpos($content, $match[0], $fromPos);
  172. $lineNum = substr_count($content, "\n", 0, $pos) + 1;
  173. $result[] = ['name' => $match[count($match) - 1], 'line' => $lineNum];
  174. $fromPos = $pos + 1;
  175. }
  176. return $result;
  177. }
  178. /**
  179. * Composes information message based on list of legacy tables, found in a file
  180. *
  181. * @param array $legacyTables
  182. * @return null|string
  183. */
  184. protected function _composeFoundsMessage($legacyTables)
  185. {
  186. if (!$legacyTables) {
  187. return null;
  188. }
  189. $descriptions = [];
  190. foreach ($legacyTables as $legacyTable) {
  191. $descriptions[] = "{$legacyTable['name']} (line {$legacyTable['line']})";
  192. }
  193. $result = 'Legacy table names with slash must be fixed to direct table names. Found: ' . implode(
  194. ', ',
  195. $descriptions
  196. ) . '.';
  197. return $result;
  198. }
  199. }