Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RouteConfigFilesTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class RouteConfigFilesTest extends \PHPUnit\Framework\TestCase
11 {
17 
23  protected $_idAttributes = [
24  '/config/routers' => 'id',
25  '/config/routers/route' => 'id',
26  '/config/routers/route/module' => 'name',
27  ];
28 
34  protected $schemaFile;
35 
41  protected $mergedSchemaFile;
42 
43  protected function setUp()
44  {
45  $this->validationStateMock = $this->createMock(\Magento\Framework\Config\ValidationStateInterface::class);
46  $this->validationStateMock->method('isValidationRequired')
47  ->willReturn(true);
48  $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
49  $this->schemaFile = $urnResolver->getRealPath('urn:magento:framework:App/etc/routes.xsd');
50  $this->mergedSchemaFile = $urnResolver->getRealPath('urn:magento:framework:App/etc/routes_merged.xsd');
51  }
52 
53  public function testRouteConfigsValidation()
54  {
55  $invalidFiles = [];
56 
58  $files = [];
59  foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
60  $mask = $moduleDir . '/etc/*/routes.xml';
61  $files = array_merge($files, glob($mask));
62  }
63  $mergedConfig = new \Magento\Framework\Config\Dom(
64  '<config><router/></config>',
65  $this->validationStateMock,
66  $this->_idAttributes
67  );
68 
69  foreach ($files as $file) {
70  $content = file_get_contents($file);
71  try {
72  new \Magento\Framework\Config\Dom(
73  $content,
74  $this->validationStateMock,
75  $this->_idAttributes,
76  null,
77  $this->schemaFile
78  );
79 
80  //merge won't be performed if file is invalid because of exception thrown
81  $mergedConfig->merge($content);
82  } catch (\Magento\Framework\Config\Dom\ValidationException $e) {
83  $invalidFiles[] = $file;
84  }
85  }
86 
87  if (!empty($invalidFiles)) {
88  $this->fail('Found broken files: ' . implode("\n", $invalidFiles));
89  }
90 
91  try {
92  $errors = [];
93  $mergedConfig->validate($this->mergedSchemaFile, $errors);
94  } catch (\Exception $e) {
95  $this->fail('Merged routes config is invalid: ' . "\n" . implode("\n", $errors));
96  }
97  }
98 }
$componentRegistrar
Definition: bootstrap.php:23
$mask
Definition: bootstrap.php:36
foreach($appDirs as $dir) $files
$errors
Definition: overview.phtml:9