CircularDependencyTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Scan source code for incorrect or undeclared modules dependencies
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Test\Integrity;
  9. use Magento\Framework\App\Utility\Files;
  10. use Magento\Setup\Module\Dependency\Circular;
  11. class CircularDependencyTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * Modules dependencies map
  15. *
  16. * @var array
  17. */
  18. protected $moduleDependencies = [];
  19. /**
  20. * Circular dependencies
  21. *
  22. * @var array
  23. */
  24. protected $circularModuleDependencies = [];
  25. public function setUp()
  26. {
  27. $this->buildModulesDependencies();
  28. $this->buildCircularModulesDependencies();
  29. }
  30. /**
  31. * Build modules dependencies
  32. */
  33. protected function buildModulesDependencies()
  34. {
  35. $configFiles = Files::init()->getConfigFiles('module.xml', [], false);
  36. foreach ($configFiles as $configFile) {
  37. preg_match('#/([^/]+?/[^/]+?)/etc/module\.xml$#', $configFile, $moduleName);
  38. $moduleName = str_replace('/', '_', $moduleName[1]);
  39. $config = simplexml_load_file($configFile);
  40. $result = $config->xpath("/config/module/depends/module") ?: [];
  41. foreach ($result as $node) {
  42. /** @var \SimpleXMLElement $node */
  43. $this->moduleDependencies[$moduleName][] = (string) $node['name'];
  44. }
  45. }
  46. }
  47. /**
  48. * Build circular modules dependencies
  49. */
  50. protected function buildCircularModulesDependencies()
  51. {
  52. $this->circularModuleDependencies = (new Circular())->buildCircularDependencies($this->moduleDependencies);
  53. }
  54. /**
  55. * Check Magento modules structure for circular dependencies
  56. */
  57. public function testCircularDependencies()
  58. {
  59. $this->markTestSkipped('Skipped before circular dependencies will be fixed MAGETWO-10938');
  60. if ($this->circularModuleDependencies) {
  61. $result = '';
  62. foreach ($this->circularModuleDependencies as $module => $chains) {
  63. $result .= $module . ' dependencies:' . PHP_EOL;
  64. foreach ($chains as $chain) {
  65. $result .= 'Chain : ' . implode('->', $chain) . PHP_EOL;
  66. }
  67. $result .= PHP_EOL;
  68. }
  69. $this->fail('Circular dependencies:' . PHP_EOL . $result);
  70. }
  71. }
  72. }