Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ElementTest.php
Go to the documentation of this file.
1 <?php
7 
8 class ElementTest extends \PHPUnit\Framework\TestCase
9 {
13  public function testUnsetSelf($xmlData)
14  {
16  $xml = simplexml_load_file($xmlData[0], $xmlData[1]);
17  $this->assertTrue(isset($xml->node3->node4));
18  $xml->node3->unsetSelf();
19  $this->assertFalse(isset($xml->node3->node4));
20  $this->assertFalse(isset($xml->node3));
21  $this->assertTrue(isset($xml->node1));
22  }
23 
29  public function testGetParent($xmlData)
30  {
32  $xml = simplexml_load_file($xmlData[0], $xmlData[1]);
33  $this->assertTrue($xml->getName() == 'root');
34  $xml->unsetSelf();
35  }
36 
40  public static function xmlDataProvider()
41  {
42  return [
43  [[__DIR__ . '/_files/data.xml', \Magento\Framework\Simplexml\Element::class]]
44  ];
45  }
46 
47  public function testAsNiceXmlMixedData()
48  {
49  $dataFile = file_get_contents(__DIR__ . '/_files/mixed_data.xml');
51  $xml = simplexml_load_string($dataFile, \Magento\Framework\Simplexml\Element::class);
52 
53  $expected = <<<XML
54 <root>
55  <node_1 id="1">Value 1
56  <node_1_1>Value 1.1
57  <node_1_1_1>Value 1.1.1</node_1_1_1>
58  </node_1_1>
59  </node_1>
60  <node_2>
61  <node_2_1>Value 2.1</node_2_1>
62  </node_2>
63 </root>
64 
65 XML;
66  $this->assertEquals($expected, $xml->asNiceXml());
67  }
68 
69  public function testAppendChild()
70  {
72  $baseXml = simplexml_load_string('<root/>', \Magento\Framework\Simplexml\Element::class);
74  $appendXml = simplexml_load_string(
75  '<node_a attr="abc"><node_b innerAttribute="xyz">text</node_b></node_a>',
76  \Magento\Framework\Simplexml\Element::class
77  );
78  $baseXml->appendChild($appendXml);
79 
80  $expectedXml = '<root><node_a attr="abc"><node_b innerAttribute="xyz">text</node_b></node_a></root>';
81  $this->assertXmlStringEqualsXmlString($expectedXml, $baseXml->asNiceXml());
82  }
83 
84  public function testSetNode()
85  {
86  $path = '/node1/node2';
87  $value = 'value';
89  $xml = simplexml_load_string('<root/>', \Magento\Framework\Simplexml\Element::class);
90  $this->assertEmpty($xml->xpath('/root/node1/node2'));
91  $xml->setNode($path, $value);
92  $this->assertNotEmpty($xml->xpath('/root/node1/node2'));
93  $this->assertEquals($value, (string)$xml->xpath('/root/node1/node2')[0]);
94  }
95 
101  public function testSetAttribute($name, $value)
102  {
104  $xml = simplexml_load_string('<root name="test2" data=""/>', \Magento\Framework\Simplexml\Element::class);
105  $this->assertEquals($xml->getAttribute('name'), 'test2');
106  $this->assertNull($xml->getAttribute('new'));
107  $xml->setAttribute($name, $value);
108  $this->assertEquals($xml->getAttribute($name), $value);
109  }
110 
114  public function setAttributeDataProvider()
115  {
116  return [
117  ['name', 'test'],
118  ['new', 'beard'],
119  ['data', 'some-data']
120  ];
121  }
122 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14