Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
AbstractConfigFiles.php
Go to the documentation of this file.
1 <?php
10 
12 
13 abstract class AbstractConfigFiles extends \PHPUnit\Framework\TestCase
14 {
18  protected $_schemaFile;
19 
23  protected $_reader;
24 
28  protected $_fileResolverMock;
29 
33  protected $_objectManager;
34 
39 
40  public function setUp()
41  {
42  $this->componentRegistrar = new ComponentRegistrar();
44  $xmlFiles = $this->getXmlConfigFiles();
45  if (!empty($xmlFiles)) {
46  $this->_fileResolverMock = $this->getMockBuilder(
47  \Magento\Framework\App\Arguments\FileResolver\Primary::class
48  )->disableOriginalConstructor()->getMock();
49 
50  /* Enable Validation regardless of MAGE_MODE */
51  $validateStateMock = $this->getMockBuilder(
52  \Magento\Framework\Config\ValidationStateInterface::class
53  )->disableOriginalConstructor()->getMock();
54  $validateStateMock->expects($this->any())->method('isValidationRequired')->will($this->returnValue(true));
55 
56  $this->_reader = $this->_objectManager->create(
57  $this->_getReaderClassName(),
58  [
59  'configFiles' => $xmlFiles,
60  'fileResolver' => $this->_fileResolverMock,
61  'validationState' => $validateStateMock
62  ]
63  );
64 
65  $this->_schemaFile = $this->_getXsdPath();
66  }
67  }
68 
69  protected function tearDown()
70  {
71  $this->_objectManager->removeSharedInstance($this->_getReaderClassName());
72  }
73 
77  public function testXmlConfigFile($file, $skip = false)
78  {
79  if ($skip) {
80  $this->markTestSkipped('There are no xml files in the system for this test.');
81  }
82  $validationStateMock = $this->createMock(\Magento\Framework\Config\ValidationStateInterface::class);
83  $validationStateMock->method('isValidationRequired')
84  ->willReturn(false);
85  $domConfig = new \Magento\Framework\Config\Dom($file, $validationStateMock);
86  $errors = [];
87  $result = $domConfig->validate($this->_schemaFile, $errors);
88  $message = "Invalid XML-file: {$file}\n";
89  foreach ($errors as $error) {
90  $message .= "{$error}\n";
91  }
92 
93  $this->assertTrue($result, $message);
94  }
95 
96  public function testMergedConfig()
97  {
98  $files = $this->getXmlConfigFiles();
99  if (empty($files)) {
100  $this->markTestSkipped('There are no xml files in the system for this test.');
101  }
102  // have the file resolver return all relevant xml files
103  $this->_fileResolverMock->expects($this->any())
104  ->method('get')
105  ->will($this->returnValue($this->getXmlConfigFiles()));
106 
107  try {
108  // this will merge all xml files and validate them
109  $this->_reader->read('global');
110  } catch (\Magento\Framework\Exception\LocalizedException $e) {
111  $this->fail($e->getMessage());
112  }
113  }
114 
123  public function xmlConfigFileProvider()
124  {
125  $fileList = $this->getXmlConfigFiles();
126  $result = [];
127  foreach ($fileList as $fileContent) {
128  $result[] = [$fileContent];
129  }
130  return $result;
131  }
132 
138  public function getXmlConfigFiles()
139  {
142  $moduleDirSearch = $objectManager->get(\Magento\Framework\Component\DirSearch::class);
143 
144  return $objectManager->get(\Magento\Framework\Config\FileIteratorFactory::class)
145  ->create($moduleDirSearch->collectFiles(ComponentRegistrar::MODULE, $this->_getConfigFilePathGlob()));
146  }
147 
153  abstract protected function _getReaderClassName();
154 
162  abstract protected function _getConfigFilePathGlob();
163 
169  abstract protected function _getXsdPath();
170 }
$objectManager
Definition: bootstrap.php:17
$fileList
Definition: export.php:13
$message
foreach($appDirs as $dir) $files
$errors
Definition: overview.phtml:9