Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DomTest.php
Go to the documentation of this file.
1 <?php
7 
8 class DomTest extends \PHPUnit\Framework\TestCase
9 {
14 
15  protected function setUp()
16  {
17  $this->validationStateMock = $this->getMockForAbstractClass(
18  \Magento\Framework\Config\ValidationStateInterface::class
19  );
20  $this->validationStateMock->method('isValidationRequired')
21  ->willReturn(true);
22  }
23 
32  public function testMerge($xmlFile, $newXmlFile, $ids, $typeAttributeName, $expectedXmlFile)
33  {
34  $xml = file_get_contents(__DIR__ . "/_files/dom/{$xmlFile}");
35  $newXml = file_get_contents(__DIR__ . "/_files/dom/{$newXmlFile}");
36  $config = new \Magento\Framework\Config\Dom($xml, $this->validationStateMock, $ids, $typeAttributeName);
37  $config->merge($newXml);
38  $this->assertXmlStringEqualsXmlFile(__DIR__ . "/_files/dom/{$expectedXmlFile}", $config->getDom()->saveXML());
39  }
40 
44  public function mergeDataProvider()
45  {
46  // note differences of XML declaration in fixture files: sometimes encoding is specified, sometimes isn't
47  return [
48  [
49  'ids.xml',
50  'ids_new.xml',
51  [
52  '/root/node/subnode' => 'id',
53  '/root/other_node' => 'id',
54  '/root/other_node/child' => 'identifier',
55  ],
56  null,
57  'ids_merged.xml',
58  ],
59  ['no_ids.xml', 'no_ids_new.xml', [], null, 'no_ids_merged.xml'],
60  ['ambiguous_one.xml', 'ambiguous_new_two.xml', [], null, 'ambiguous_merged.xml'],
61  ['namespaced.xml', 'namespaced_new.xml', ['/root/node' => 'id'], null, 'namespaced_merged.xml'],
62  ['override_node.xml', 'override_node_new.xml', [], null, 'override_node_merged.xml'],
63  ['override_node_new.xml', 'override_node.xml', [], null, 'override_node_merged.xml'],
64  ['text_node.xml', 'text_node_new.xml', [], null, 'text_node_merged.xml'],
65  [
66  'recursive.xml',
67  'recursive_new.xml',
68  ['/root/(node|another_node)(/param)?' => 'name', '/root/node/param(/complex/item)+' => 'key'],
69  null,
70  'recursive_merged.xml',
71  ],
72  [
73  'recursive_deep.xml',
74  'recursive_deep_new.xml',
75  ['/root(/node)+' => 'name'],
76  null,
77  'recursive_deep_merged.xml',
78  ],
79  [
80  'types.xml',
81  'types_new.xml',
82  ['/root/item' => 'id', '/root/item/subitem' => 'id'],
83  'xsi:type',
84  'types_merged.xml',
85  ],
86  [
87  'attributes.xml',
88  'attributes_new.xml',
89  ['/root/item' => 'id', '/root/item/subitem' => 'id'],
90  'xsi:type',
91  'attributes_merged.xml',
92  ],
93  ];
94  }
95 
100  public function testMergeException()
101  {
102  $xml = file_get_contents(__DIR__ . "/_files/dom/ambiguous_two.xml");
103  $newXml = file_get_contents(__DIR__ . "/_files/dom/ambiguous_new_one.xml");
104  $config = new \Magento\Framework\Config\Dom($xml, $this->validationStateMock);
105  $config->merge($newXml);
106  }
107 
113  public function testValidate($xml, array $expectedErrors)
114  {
115  if (!function_exists('libxml_set_external_entity_loader')) {
116  $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
117  }
118  $dom = new \Magento\Framework\Config\Dom($xml, $this->validationStateMock);
119  $actualResult = $dom->validate(__DIR__ . '/_files/sample.xsd', $actualErrors);
120  $this->assertEquals(empty($expectedErrors), $actualResult);
121  $this->assertEquals($expectedErrors, $actualErrors);
122  }
123 
127  public function validateDataProvider()
128  {
129  return [
130  'valid' => ['<root><node id="id1"/><node id="id2"/></root>', []],
131  'invalid' => [
132  '<root><node id="id1"/><unknown_node/></root>',
133  ["Element 'unknown_node': This element is not expected. Expected is ( node ).\nLine: 1\n"],
134  ],
135  ];
136  }
137 
139  {
140  $xml = '<root><unknown_node/></root>';
141  $errorFormat = 'Error: `%message%`';
142  $expectedErrors = [
143  "Error: `Element 'unknown_node': This element is not expected. Expected is ( node ).`",
144  ];
145  $dom = new \Magento\Framework\Config\Dom($xml, $this->validationStateMock, [], null, null, $errorFormat);
146  $actualResult = $dom->validate(__DIR__ . '/_files/sample.xsd', $actualErrors);
147  $this->assertFalse($actualResult);
148  $this->assertEquals($expectedErrors, $actualErrors);
149  }
150 
156  {
157  $xml = '<root><unknown_node/></root>';
158  $errorFormat = '%message%,%unknown%';
159  $dom = new \Magento\Framework\Config\Dom($xml, $this->validationStateMock, [], null, null, $errorFormat);
160  $dom->validate(__DIR__ . '/_files/sample.xsd');
161  }
162 
163  public function testValidateUnknownError()
164  {
165  if (!function_exists('libxml_set_external_entity_loader')) {
166  $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
167  }
168  $xml = '<root><node id="id1"/><node id="id2"/></root>';
169  $schemaFile = __DIR__ . '/_files/sample.xsd';
170  $dom = new \Magento\Framework\Config\Dom($xml, $this->validationStateMock);
171  $domMock = $this->createPartialMock(\DOMDocument::class, ['schemaValidate']);
172  $domMock->expects($this->once())
173  ->method('schemaValidate')
174  ->with($schemaFile)
175  ->willReturn(false);
176  $this->assertEquals(
177  ["Element 'unknown_node': This element is not expected. Expected is ( node ).\nLine: 1\n"],
178  $dom->validateDomDocument($domMock, $schemaFile)
179  );
180  }
181 
186  {
187  if (!function_exists('libxml_set_external_entity_loader')) {
188  $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
189  }
190  $xml = '<root><node id="id1"/><node id="id2"/></root>';
191  $schemaFile = __DIR__ . '/_files/sample.xsd';
192  $dom = new \Magento\Framework\Config\Dom($xml, $this->validationStateMock);
193  $domMock = $this->createPartialMock(\DOMDocument::class, ['schemaValidate']);
194  $domMock->expects($this->once())
195  ->method('schemaValidate')
196  ->with($schemaFile)
197  ->willThrowException(new \Exception());
198  $dom->validateDomDocument($domMock, $schemaFile);
199  }
200 }
testMerge($xmlFile, $newXmlFile, $ids, $typeAttributeName, $expectedXmlFile)
Definition: DomTest.php:32
$config
Definition: fraud_order.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
testValidate($xml, array $expectedErrors)
Definition: DomTest.php:113