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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Simplexml\Test\Unit;
class ElementTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider xmlDataProvider
*/
public function testUnsetSelf($xmlData)
{
/** @var $xml \Magento\Framework\Simplexml\Element */
$xml = simplexml_load_file($xmlData[0], $xmlData[1]);
$this->assertTrue(isset($xml->node3->node4));
$xml->node3->unsetSelf();
$this->assertFalse(isset($xml->node3->node4));
$this->assertFalse(isset($xml->node3));
$this->assertTrue(isset($xml->node1));
}
/**
* @dataProvider xmlDataProvider
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Root node could not be unset.
*/
public function testGetParent($xmlData)
{
/** @var $xml \Magento\Framework\Simplexml\Element */
$xml = simplexml_load_file($xmlData[0], $xmlData[1]);
$this->assertTrue($xml->getName() == 'root');
$xml->unsetSelf();
}
/**
* Data Provider for testUnsetSelf and testUnsetSelfException
*/
public static function xmlDataProvider()
{
return [
[[__DIR__ . '/_files/data.xml', \Magento\Framework\Simplexml\Element::class]]
];
}
public function testAsNiceXmlMixedData()
{
$dataFile = file_get_contents(__DIR__ . '/_files/mixed_data.xml');
/** @var \Magento\Framework\Simplexml\Element $xml */
$xml = simplexml_load_string($dataFile, \Magento\Framework\Simplexml\Element::class);
$expected = <<<XML
<root>
<node_1 id="1">Value 1
<node_1_1>Value 1.1
<node_1_1_1>Value 1.1.1</node_1_1_1>
</node_1_1>
</node_1>
<node_2>
<node_2_1>Value 2.1</node_2_1>
</node_2>
</root>
XML;
$this->assertEquals($expected, $xml->asNiceXml());
}
public function testAppendChild()
{
/** @var \Magento\Framework\Simplexml\Element $baseXml */
$baseXml = simplexml_load_string('<root/>', \Magento\Framework\Simplexml\Element::class);
/** @var \Magento\Framework\Simplexml\Element $appendXml */
$appendXml = simplexml_load_string(
'<node_a attr="abc"><node_b innerAttribute="xyz">text</node_b></node_a>',
\Magento\Framework\Simplexml\Element::class
);
$baseXml->appendChild($appendXml);
$expectedXml = '<root><node_a attr="abc"><node_b innerAttribute="xyz">text</node_b></node_a></root>';
$this->assertXmlStringEqualsXmlString($expectedXml, $baseXml->asNiceXml());
}
public function testSetNode()
{
$path = '/node1/node2';
$value = 'value';
/** @var \Magento\Framework\Simplexml\Element $xml */
$xml = simplexml_load_string('<root/>', \Magento\Framework\Simplexml\Element::class);
$this->assertEmpty($xml->xpath('/root/node1/node2'));
$xml->setNode($path, $value);
$this->assertNotEmpty($xml->xpath('/root/node1/node2'));
$this->assertEquals($value, (string)$xml->xpath('/root/node1/node2')[0]);
}
/**
* @dataProvider setAttributeDataProvider
* @param string $name
* @param string $value
*/
public function testSetAttribute($name, $value)
{
/** @var \Magento\Framework\Simplexml\Element $xml */
$xml = simplexml_load_string('<root name="test2" data=""/>', \Magento\Framework\Simplexml\Element::class);
$this->assertEquals($xml->getAttribute('name'), 'test2');
$this->assertNull($xml->getAttribute('new'));
$xml->setAttribute($name, $value);
$this->assertEquals($xml->getAttribute($name), $value);
}
/**
* @return array
*/
public function setAttributeDataProvider()
{
return [
['name', 'test'],
['new', 'beard'],
['data', 'some-data']
];
}
}