DeclarativeSchemaRule.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Rule for searching DB dependency
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\TestFramework\Dependency;
  9. /**
  10. * Rule for testing integrity within declarative schema.
  11. *
  12. * @package Magento\TestFramework\Dependency
  13. */
  14. class DeclarativeSchemaRule implements RuleInterface
  15. {
  16. /**
  17. * Map of tables and modules
  18. *
  19. * @var array
  20. */
  21. protected $_moduleTableMap;
  22. /**
  23. * Constructor
  24. *
  25. * @param array $tables
  26. */
  27. public function __construct(array $tables)
  28. {
  29. $this->_moduleTableMap = $tables;
  30. }
  31. /**
  32. * Gets external dependencies information for current module by analyzing db_schema.xml files contents.
  33. *
  34. * @param string $currentModule
  35. * @param string $fileType
  36. * @param string $file
  37. * @param string $contents
  38. * @return array
  39. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  40. * @SuppressWarnings(PHPMD.NPathComplexity)
  41. */
  42. public function getDependencyInfo($currentModule, $fileType, $file, &$contents)
  43. {
  44. if ('db_schema' != $fileType || !preg_match('#.*/db_schema\.xml$#', $file)) {
  45. return [];
  46. }
  47. $dependenciesInfo = [];
  48. $unKnowTables = [];
  49. $dom = new \DOMDocument();
  50. $dom->loadXML($contents);
  51. $tables = $dom->getElementsByTagName('table');
  52. $constraints = $dom->getElementsByTagName('constraint');
  53. $tableNames = [];
  54. $foreignKeyTables = [];
  55. $foreignKeyReferenceTables = [];
  56. /** @var \DOMElement $table */
  57. foreach ($tables as $table) {
  58. $tableNames[] = $table->getAttribute('name');
  59. }
  60. /** @var \DOMElement $constraint */
  61. foreach ($constraints as $constraint) {
  62. $xsiType = $constraint->getAttribute('xsi:type');
  63. if (strtolower($xsiType) == 'foreign' && $constraint->getAttribute('disabled') !== '1') {
  64. $foreignKeyTables[] = $constraint->getAttribute('table');
  65. $foreignKeyReferenceTables[] = $constraint->getAttribute('referenceTable');
  66. }
  67. }
  68. $tableNames = array_unique(array_merge($tableNames, $foreignKeyReferenceTables, $foreignKeyTables));
  69. /** @var string $table */
  70. foreach ($tableNames as $table) {
  71. if (!isset($this->_moduleTableMap[$table])) {
  72. $unKnowTables[$file][$table] = $table;
  73. continue;
  74. }
  75. if (strtolower($currentModule) !== strtolower($this->_moduleTableMap[$table])) {
  76. $dependenciesInfo[] = [
  77. 'module' => $this->_moduleTableMap[$table],
  78. 'type' => RuleInterface::TYPE_HARD,
  79. 'source' => $table,
  80. ];
  81. }
  82. }
  83. foreach ($unKnowTables as $tables) {
  84. foreach ($tables as $table) {
  85. $dependenciesInfo[] = ['module' => 'Unknown', 'source' => $table];
  86. }
  87. }
  88. return $dependenciesInfo;
  89. }
  90. }