RouteConfigFilesTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Integrity\Modular;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. class RouteConfigFilesTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\Config\ValidationStateInterface
  12. */
  13. protected $validationStateMock;
  14. /**
  15. * attributes represent merging rules
  16. * copied from original class \Magento\Framework\App\Route\Config\Reader
  17. * @var array
  18. */
  19. protected $_idAttributes = [
  20. '/config/routers' => 'id',
  21. '/config/routers/route' => 'id',
  22. '/config/routers/route/module' => 'name',
  23. ];
  24. /**
  25. * Path to loose XSD for per file validation
  26. *
  27. * @var string
  28. */
  29. protected $schemaFile;
  30. /**
  31. * Path to tough XSD for merged file validation
  32. *
  33. * @var string
  34. */
  35. protected $mergedSchemaFile;
  36. protected function setUp()
  37. {
  38. $this->validationStateMock = $this->createMock(\Magento\Framework\Config\ValidationStateInterface::class);
  39. $this->validationStateMock->method('isValidationRequired')
  40. ->willReturn(true);
  41. $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
  42. $this->schemaFile = $urnResolver->getRealPath('urn:magento:framework:App/etc/routes.xsd');
  43. $this->mergedSchemaFile = $urnResolver->getRealPath('urn:magento:framework:App/etc/routes_merged.xsd');
  44. }
  45. public function testRouteConfigsValidation()
  46. {
  47. $invalidFiles = [];
  48. $componentRegistrar = new ComponentRegistrar();
  49. $files = [];
  50. foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
  51. $mask = $moduleDir . '/etc/*/routes.xml';
  52. $files = array_merge($files, glob($mask));
  53. }
  54. $mergedConfig = new \Magento\Framework\Config\Dom(
  55. '<config><router/></config>',
  56. $this->validationStateMock,
  57. $this->_idAttributes
  58. );
  59. foreach ($files as $file) {
  60. $content = file_get_contents($file);
  61. try {
  62. new \Magento\Framework\Config\Dom(
  63. $content,
  64. $this->validationStateMock,
  65. $this->_idAttributes,
  66. null,
  67. $this->schemaFile
  68. );
  69. //merge won't be performed if file is invalid because of exception thrown
  70. $mergedConfig->merge($content);
  71. } catch (\Magento\Framework\Config\Dom\ValidationException $e) {
  72. $invalidFiles[] = $file;
  73. }
  74. }
  75. if (!empty($invalidFiles)) {
  76. $this->fail('Found broken files: ' . implode("\n", $invalidFiles));
  77. }
  78. try {
  79. $errors = [];
  80. $mergedConfig->validate($this->mergedSchemaFile, $errors);
  81. } catch (\Exception $e) {
  82. $this->fail('Merged routes config is invalid: ' . "\n" . implode("\n", $errors));
  83. }
  84. }
  85. }