Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConvertArrayTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\Convert\ConvertArray;
10 
11 class ConvertArrayTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $_model;
17 
18  protected function setUp()
19  {
20  $this->_model = new ConvertArray();
21  }
22 
23  public function testAssocToXml()
24  {
25  $data = ['one' => 1, 'two' => ['three' => 3, 'four' => '4']];
26  $result = $this->_model->assocToXml($data);
27  $expectedResult = <<<XML
28 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
29 <_><one>1</one><two><three>3</three><four>4</four></two></_>
30 
31 XML;
32  $this->assertInstanceOf('SimpleXMLElement', $result);
33  $this->assertEquals($expectedResult, $result->asXML());
34  }
35 
40  public function testAssocToXmlExceptionByKey()
41  {
42  $data = [
43  'one' => [
44  100,
45  'two' => 'three',
46  ],
47  ];
48  $this->_model->assocToXml($data);
49  }
50 
57  public function testAssocToXmlException($array, $rootName = '_')
58  {
59  $this->_model->assocToXml($array, $rootName);
60  }
61 
62  public function testToFlatArray()
63  {
64  $input = [
65  'key1' => 'value1',
66  'key2' => ['key21' => 'value21', 'key22' => 'value22', 'key23' => ['key231' => 'value231']],
67  'key3' => ['key31' => 'value31', 'key3' => 'value3'],
68  'key4' => ['key4' => 'value4'],
69  ];
70  $expectedOutput = [
71  'key1' => 'value1',
72  'key21' => 'value21',
73  'key22' => 'value22',
74  'key231' => 'value231',
75  'key31' => 'value31',
76  'key3' => 'value3',
77  'key4' => 'value4',
78  ];
80  $this->assertEquals($expectedOutput, $output, 'Array is converted to flat structure incorrectly.');
81  }
82 
87  {
88  return [[[], ''], [[], 0], [[1, 2, 3]], [['root' => 1], 'root']];
89  }
90 }