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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Simplexml\Test\Unit;
use Magento\Framework\Simplexml\Config;
class ConfigTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Config
*/
protected $config;
protected function setUp()
{
$this->config = new Config();
}
public function testConstruct()
{
$xml = '<root><node1><node2/></node1><node3><node4/></node3></root>';
$file = __DIR__ . '/_files/data.xml';
$config = new Config($xml);
$this->assertXmlStringEqualsXmlString($xml, $config->getXmlString());
$config = new Config($file);
$this->assertXmlStringEqualsXmlString($xml, $config->getXmlString());
/** @var Element $simpleXml */
$simpleXml = simplexml_load_string(file_get_contents($file), \Magento\Framework\Simplexml\Element::class);
$config = new Config($simpleXml);
$this->assertXmlStringEqualsXmlString($xml, $config->getXmlString());
}
public function testLoadString()
{
$xml = '<?xml version="1.0"?><config><node>1</node></config>';
$this->assertFalse($this->config->loadString(''));
$this->assertTrue($this->config->loadString($xml));
$this->assertXmlStringEqualsXmlString($xml, $this->config->getXmlString());
}
public function testLoadDom()
{
$dom = new \DOMDocument();
$dom->loadXML('<?xml version="1.0"?><config><node>1</node></config>');
$this->assertTrue($this->config->loadDom($dom));
}
public function testGetNode()
{
$this->assertFalse($this->config->getNode());
$config = new Config(__DIR__ . '/_files/mixed_data.xml');
$this->assertSame('Value 2.1', $config->getNode('node_2/node_2_1')->asArray());
}
public function testGetXpath()
{
$this->assertFalse($this->config->getXpath('wrong_xpath'));
$config = new Config(__DIR__ . '/_files/mixed_data.xml');
$this->assertFalse($config->getXpath('wrong_xpath'));
$element = $config->getXpath('/root/node_2/node_2_1');
$this->assertArrayHasKey(0, $element);
$this->assertInstanceOf(\Magento\Framework\Simplexml\Element::class, $element[0]);
$this->assertSame('Value 2.1', $element[0]->asArray());
}
public function testLoadWrongFile()
{
$this->assertFalse($this->config->loadFile('wrong_file'));
}
public function testSetNode()
{
$config = new Config(__DIR__ . '/_files/mixed_data.xml');
$config->setNode('node_2', 'new_value');
$this->assertSame('new_value', $config->getNode('node_2')->asArray());
}
public function testApplyExtends()
{
$config = new Config(__DIR__ . '/_files/extend_data.xml');
$config->applyExtends();
$this->assertEquals(
$config->getNode('node_1/node_1_1')->asArray(),
$config->getNode('node_3/node_1_1')->asArray()
);
$config = new Config(__DIR__ . '/_files/data.xml');
$config->applyExtends();
}
public function testExtendNode()
{
$config = new Config(__DIR__ . '/_files/data.xml');
$config->extend(new Config('<config><node>1</node></config>'));
$this->assertSame('1', $config->getNode('node')->asArray());
}
}