Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ObjectTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\Convert\DataObject;
9 
10 class ObjectTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
17  protected function setUp()
18  {
19  $this->model = new DataObject();
20  }
21 
22  public function testToOptionArray()
23  {
24  $mockFirst = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getId', 'getCode']);
25  $mockFirst->expects($this->once())
26  ->method('getId')
27  ->will($this->returnValue(1));
28  $mockFirst->expects($this->once())
29  ->method('getCode')
30  ->will($this->returnValue('code1'));
31  $mockSecond = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getId', 'getCode']);
32  $mockSecond->expects($this->once())
33  ->method('getId')
34  ->will($this->returnValue(2));
35  $mockSecond->expects($this->once())
36  ->method('getCode')
37  ->will($this->returnValue('code2'));
38 
39  $callable = function ($item) {
40  return $item->getCode();
41  };
42 
43  $items = [
44  $mockFirst,
45  $mockSecond,
46  ];
47  $result = [
48  ['value' => 1, 'label' => 'code1'],
49  ['value' => 2, 'label' => 'code2'],
50  ];
51  $this->assertEquals($result, $this->model->toOptionArray($items, 'id', $callable));
52  }
53 
54  public function testToOptionHash()
55  {
56  $mockFirst = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getSome', 'getId']);
57  $mockFirst->expects($this->once())
58  ->method('getId')
59  ->will($this->returnValue(3));
60  $mockFirst->expects($this->once())
61  ->method('getSome')
62  ->will($this->returnValue('code3'));
63  $mockSecond = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getSome', 'getId']);
64  $mockSecond->expects($this->once())
65  ->method('getId')
66  ->will($this->returnValue(4));
67  $mockSecond->expects($this->once())
68  ->method('getSome')
69  ->will($this->returnValue('code4'));
70 
71  $callable = function ($item) {
72  return $item->getId();
73  };
74  $items = [
75  $mockFirst,
76  $mockSecond,
77  ];
78  $result = [
79  3 => 'code3',
80  4 => 'code4',
81  ];
82 
83  $this->assertEquals($result, $this->model->toOptionHash($items, $callable, 'some'));
84  }
85 
86  public function testConvertDataToArray()
87  {
88  $object = new \stdClass();
89  $object->a = [[1]];
90  $mockFirst = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getData']);
91  $mockSecond = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getData']);
92 
93  $mockFirst->expects($this->any())
94  ->method('getData')
95  ->will($this->returnValue([
96  'id' => 1,
97  'o' => $mockSecond,
98  ]));
99 
100  $mockSecond->expects($this->any())
101  ->method('getData')
102  ->will($this->returnValue([
103  'id' => 2,
104  'o' => $mockFirst,
105  ]));
106 
107  $data = [
108  'object' => $mockFirst,
109  'stdClass' => $object,
110  'test' => 'test',
111  ];
112  $result = [
113  'object' => [
114  'id' => 1,
115  'o' => [
116  'id' => 2,
117  'o' => '*** CYCLE DETECTED ***',
118  ],
119  ],
120  'stdClass' => [
121  'a' => [
122  [1],
123  ],
124  ],
125  'test' => 'test',
126  ];
127  $this->assertEquals($result, $this->model->convertDataToArray($data));
128  }
129 }
$items