1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Abstract class that helps in writing tests that validate config xml files
* are valid both individually and when merged.
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\TestCase;
use Magento\Framework\Component\ComponentRegistrar;
abstract class AbstractConfigFiles extends \PHPUnit\Framework\TestCase
{
/**
* @var string
*/
protected $_schemaFile;
/**
* @var \Magento\Framework\Config\Reader\Filesystem
*/
protected $_reader;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $_fileResolverMock;
/**
* @var \Magento\TestFramework\ObjectManager
*/
protected $_objectManager;
/**
* @var ComponentRegistrar
*/
protected $componentRegistrar;
public function setUp()
{
$this->componentRegistrar = new ComponentRegistrar();
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$xmlFiles = $this->getXmlConfigFiles();
if (!empty($xmlFiles)) {
$this->_fileResolverMock = $this->getMockBuilder(
\Magento\Framework\App\Arguments\FileResolver\Primary::class
)->disableOriginalConstructor()->getMock();
/* Enable Validation regardless of MAGE_MODE */
$validateStateMock = $this->getMockBuilder(
\Magento\Framework\Config\ValidationStateInterface::class
)->disableOriginalConstructor()->getMock();
$validateStateMock->expects($this->any())->method('isValidationRequired')->will($this->returnValue(true));
$this->_reader = $this->_objectManager->create(
$this->_getReaderClassName(),
[
'configFiles' => $xmlFiles,
'fileResolver' => $this->_fileResolverMock,
'validationState' => $validateStateMock
]
);
$this->_schemaFile = $this->_getXsdPath();
}
}
protected function tearDown()
{
$this->_objectManager->removeSharedInstance($this->_getReaderClassName());
}
/**
* @dataProvider xmlConfigFileProvider
*/
public function testXmlConfigFile($file, $skip = false)
{
if ($skip) {
$this->markTestSkipped('There are no xml files in the system for this test.');
}
$validationStateMock = $this->createMock(\Magento\Framework\Config\ValidationStateInterface::class);
$validationStateMock->method('isValidationRequired')
->willReturn(false);
$domConfig = new \Magento\Framework\Config\Dom($file, $validationStateMock);
$errors = [];
$result = $domConfig->validate($this->_schemaFile, $errors);
$message = "Invalid XML-file: {$file}\n";
foreach ($errors as $error) {
$message .= "{$error}\n";
}
$this->assertTrue($result, $message);
}
public function testMergedConfig()
{
$files = $this->getXmlConfigFiles();
if (empty($files)) {
$this->markTestSkipped('There are no xml files in the system for this test.');
}
// have the file resolver return all relevant xml files
$this->_fileResolverMock->expects($this->any())
->method('get')
->will($this->returnValue($this->getXmlConfigFiles()));
try {
// this will merge all xml files and validate them
$this->_reader->read('global');
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->fail($e->getMessage());
}
}
/**
* Returns an array of all the config xml files for this test.
*
* Handles the case where no files were found and notifies the test to skip.
* This is needed to avoid a fatal error caused by a provider returning an empty array.
*
* @return array
*/
public function xmlConfigFileProvider()
{
$fileList = $this->getXmlConfigFiles();
$result = [];
foreach ($fileList as $fileContent) {
$result[] = [$fileContent];
}
return $result;
}
/**
* Finds all config xml files based on a path glob.
*
* @return \Magento\Framework\Config\FileIterator
*/
public function getXmlConfigFiles()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var $moduleDirSearch \Magento\Framework\Component\DirSearch */
$moduleDirSearch = $objectManager->get(\Magento\Framework\Component\DirSearch::class);
return $objectManager->get(\Magento\Framework\Config\FileIteratorFactory::class)
->create($moduleDirSearch->collectFiles(ComponentRegistrar::MODULE, $this->_getConfigFilePathGlob()));
}
/**
* Returns the reader class name that will be instantiated via ObjectManager
*
* @return string reader class name
*/
abstract protected function _getReaderClassName();
/**
* Returns a string that represents the path to the config file, starting in the app directory.
*
* Format is glob, so * is allowed.
*
* @return string
*/
abstract protected function _getConfigFilePathGlob();
/**
* Returns an absolute path to the XSD file corresponding to the XML files specified in _getConfigFilePathGlob
*
* @return string
*/
abstract protected function _getXsdPath();
}