ElementTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Simplexml\Test\Unit;
  7. class ElementTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @dataProvider xmlDataProvider
  11. */
  12. public function testUnsetSelf($xmlData)
  13. {
  14. /** @var $xml \Magento\Framework\Simplexml\Element */
  15. $xml = simplexml_load_file($xmlData[0], $xmlData[1]);
  16. $this->assertTrue(isset($xml->node3->node4));
  17. $xml->node3->unsetSelf();
  18. $this->assertFalse(isset($xml->node3->node4));
  19. $this->assertFalse(isset($xml->node3));
  20. $this->assertTrue(isset($xml->node1));
  21. }
  22. /**
  23. * @dataProvider xmlDataProvider
  24. * @expectedException \InvalidArgumentException
  25. * @expectedExceptionMessage Root node could not be unset.
  26. */
  27. public function testGetParent($xmlData)
  28. {
  29. /** @var $xml \Magento\Framework\Simplexml\Element */
  30. $xml = simplexml_load_file($xmlData[0], $xmlData[1]);
  31. $this->assertTrue($xml->getName() == 'root');
  32. $xml->unsetSelf();
  33. }
  34. /**
  35. * Data Provider for testUnsetSelf and testUnsetSelfException
  36. */
  37. public static function xmlDataProvider()
  38. {
  39. return [
  40. [[__DIR__ . '/_files/data.xml', \Magento\Framework\Simplexml\Element::class]]
  41. ];
  42. }
  43. public function testAsNiceXmlMixedData()
  44. {
  45. $dataFile = file_get_contents(__DIR__ . '/_files/mixed_data.xml');
  46. /** @var \Magento\Framework\Simplexml\Element $xml */
  47. $xml = simplexml_load_string($dataFile, \Magento\Framework\Simplexml\Element::class);
  48. $expected = <<<XML
  49. <root>
  50. <node_1 id="1">Value 1
  51. <node_1_1>Value 1.1
  52. <node_1_1_1>Value 1.1.1</node_1_1_1>
  53. </node_1_1>
  54. </node_1>
  55. <node_2>
  56. <node_2_1>Value 2.1</node_2_1>
  57. </node_2>
  58. </root>
  59. XML;
  60. $this->assertEquals($expected, $xml->asNiceXml());
  61. }
  62. public function testAppendChild()
  63. {
  64. /** @var \Magento\Framework\Simplexml\Element $baseXml */
  65. $baseXml = simplexml_load_string('<root/>', \Magento\Framework\Simplexml\Element::class);
  66. /** @var \Magento\Framework\Simplexml\Element $appendXml */
  67. $appendXml = simplexml_load_string(
  68. '<node_a attr="abc"><node_b innerAttribute="xyz">text</node_b></node_a>',
  69. \Magento\Framework\Simplexml\Element::class
  70. );
  71. $baseXml->appendChild($appendXml);
  72. $expectedXml = '<root><node_a attr="abc"><node_b innerAttribute="xyz">text</node_b></node_a></root>';
  73. $this->assertXmlStringEqualsXmlString($expectedXml, $baseXml->asNiceXml());
  74. }
  75. public function testSetNode()
  76. {
  77. $path = '/node1/node2';
  78. $value = 'value';
  79. /** @var \Magento\Framework\Simplexml\Element $xml */
  80. $xml = simplexml_load_string('<root/>', \Magento\Framework\Simplexml\Element::class);
  81. $this->assertEmpty($xml->xpath('/root/node1/node2'));
  82. $xml->setNode($path, $value);
  83. $this->assertNotEmpty($xml->xpath('/root/node1/node2'));
  84. $this->assertEquals($value, (string)$xml->xpath('/root/node1/node2')[0]);
  85. }
  86. /**
  87. * @dataProvider setAttributeDataProvider
  88. * @param string $name
  89. * @param string $value
  90. */
  91. public function testSetAttribute($name, $value)
  92. {
  93. /** @var \Magento\Framework\Simplexml\Element $xml */
  94. $xml = simplexml_load_string('<root name="test2" data=""/>', \Magento\Framework\Simplexml\Element::class);
  95. $this->assertEquals($xml->getAttribute('name'), 'test2');
  96. $this->assertNull($xml->getAttribute('new'));
  97. $xml->setAttribute($name, $value);
  98. $this->assertEquals($xml->getAttribute($name), $value);
  99. }
  100. /**
  101. * @return array
  102. */
  103. public function setAttributeDataProvider()
  104. {
  105. return [
  106. ['name', 'test'],
  107. ['new', 'beard'],
  108. ['data', 'some-data']
  109. ];
  110. }
  111. }