PageRenderTime 266ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

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

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