Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SchemaTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class SchemaTest extends \PHPUnit\Framework\TestCase
12 {
13  public function testXmlFiles()
14  {
15  $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
16  $invoker(
20  function ($filename) {
21  $dom = new \DOMDocument();
22  $xmlFile = file_get_contents($filename);
23  $dom->loadXML($xmlFile);
24  $errors = libxml_get_errors();
25  libxml_clear_errors();
26  $this->assertEmpty($errors, print_r($errors, true));
27 
28  $schemaLocations = [];
29  preg_match('/xsi:noNamespaceSchemaLocation=\s*"(urn:[^"]+)"/s', $xmlFile, $schemaLocations);
30  $this->assertEquals(
31  2,
32  count($schemaLocations),
33  'The XML file at ' . $filename . ' does not have a schema properly defined. It should '
34  . 'have a xsi:noNamespaceSchemaLocation attribute defined with a URN path. E.g. '
35  . 'xsi:noNamespaceSchemaLocation="urn:magento:framework:Relative_Path/something.xsd"'
36  );
37 
38  try {
40  } catch (\Exception $exception) {
41  $errors = [$exception->__toString()];
42  }
43  $this->assertEmpty(
44  $errors,
45  "Error validating $filename against {$schemaLocations[1]}\n" . print_r($errors, true)
46  );
47  },
48  $this->getXmlFiles()
49  );
50  }
51 
52  public function getSchemas()
53  {
55  $codeSchemas = [];
56  foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $modulePath) {
57  $codeSchemas = array_merge($codeSchemas, $this->_getFiles($modulePath, '*.xsd'));
58  }
59  $libSchemas = [];
61  $libSchemas = array_merge($libSchemas, $this->_getFiles($libraryPath, '*.xsd'));
62  }
63  return $this->_dataSet(array_merge($codeSchemas, $libSchemas));
64  }
65 
66  public function getXmlFiles()
67  {
69  $codeXml = [];
70  foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $modulePath) {
71  $codeXml = array_merge($codeXml, $this->_getFiles($modulePath, '*.xml', '/.\/Test\/./'));
72  }
73  $this->_filterSpecialCases($codeXml);
74  $designXml = [];
75  foreach ($componentRegistrar->getPaths(ComponentRegistrar::THEME) as $themePath) {
76  $designXml = array_merge($designXml, $this->_getFiles($themePath, '*.xml'));
77  }
78  $libXml = [];
80  $libXml = array_merge($libXml, $this->_getFiles($libraryPath, '*.xml', '/.\/Test\/./'));
81  }
82  return $this->_dataSet(array_merge($codeXml, $designXml, $libXml));
83  }
84 
85  protected function _getFiles($dir, $pattern, $skipDirPattern = '')
86  {
87  $files = glob($dir . '/' . $pattern, GLOB_NOSORT);
88 
89  if (empty($skipDirPattern) || !preg_match($skipDirPattern, $dir)) {
90  foreach (glob($dir . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $newDir) {
91  $files = array_merge($files, $this->_getFiles($newDir, $pattern, $skipDirPattern));
92  }
93  }
94 
95  return $files;
96  }
97 
103  private function _filterSpecialCases(&$files)
104  {
105  $list = [
106  '#etc/countries.xml$#',
107  '#conf/schema.xml$#',
108  '#layout/swagger_index_index.xml$#',
109  '#Doc/etc/doc/vars.xml$#',
110  '#phpunit.xml$#',
111  '#etc/db_schema.xml$#',
112  '#Test/Mftf#',
113  ];
114  foreach ($list as $pattern) {
115  foreach ($files as $key => $value) {
116  if (preg_match($pattern, $value)) {
117  unset($files[$key]);
118  }
119  }
120  }
121  }
122 
123  protected function _dataSet($files)
124  {
125  $data = [];
126  foreach ($files as $file) {
127  $data[substr($file, strlen(BP))] = [$file];
128  }
129  return $data;
130  }
131 }
$componentRegistrar
Definition: bootstrap.php:23
static validateDomDocument(\DOMDocument $dom, $schema, $errorFormat=self::ERROR_FORMAT_DEFAULT)
Definition: Dom.php:301
$pattern
Definition: website.php:22
$value
Definition: gender.phtml:16
const BP
Definition: autoload.php:14
if(isset($opts->l)) $libraryPath
_getFiles($dir, $pattern, $skipDirPattern='')
Definition: SchemaTest.php:85
foreach($appDirs as $dir) $files
$errors
Definition: overview.phtml:9