Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataObjectTest.php
Go to the documentation of this file.
1 <?php
11 
12 class DataObjectTest extends \PHPUnit\Framework\TestCase
13 {
17  private $dataObject;
18 
22  protected function setUp()
23  {
24  parent::setUp();
25  $this->dataObject = new \Magento\Framework\DataObject();
26  }
27 
31  protected function tearDown()
32  {
33  $this->dataObject = null;
34  parent::tearDown();
35  }
36 
40  public function testConstruct()
41  {
42  $object = new \Magento\Framework\DataObject();
43  $this->assertEquals([], $object->getData());
44 
45  $data = ['test' => 'test'];
46  $object = new \Magento\Framework\DataObject($data);
47  $this->assertEquals($data, $object->getData());
48  }
49 
53  public function testAddData()
54  {
55  $this->dataObject->addData(['test' => 'value']);
56  $this->assertEquals('value', $this->dataObject->getData('test'));
57 
58  $this->dataObject->addData(['test' => 'value1']);
59  $this->assertEquals('value1', $this->dataObject->getData('test'));
60 
61  $this->dataObject->addData(['test2' => 'value2']);
62  $this->assertEquals(['test' => 'value1', 'test2' => 'value2'], $this->dataObject->getData());
63  }
64 
68  public function testSetData()
69  {
70  $data = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 3];
71  $this->dataObject->setData($data);
72  $this->assertEquals($data, $this->dataObject->getData());
73 
74  $data['key1'] = 1;
75  $this->dataObject->setData('key1', 1);
76  $this->assertEquals($data, $this->dataObject->getData());
77 
78  $this->dataObject->setData('key1');
79  $data['key1'] = null;
80  $this->assertEquals($data, $this->dataObject->getData());
81  }
82 
86  public function testUnsetData()
87  {
88  $data = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 3, 'key4' => 4];
89  $this->dataObject->setData($data);
90 
91  $this->dataObject->unsetData('key1');
92  unset($data['key1']);
93  $this->assertEquals($data, $this->dataObject->getData());
94 
95  $this->dataObject->unsetData(['key2', 'key3']);
96  unset($data['key2']);
97  unset($data['key3']);
98  $this->assertEquals($data, $this->dataObject->getData());
99 
100  $this->dataObject->unsetData();
101  $this->assertEquals([], $this->dataObject->getData());
102  }
103 
107  public function testGetData()
108  {
109  $data = [
110  'key1' => 'value1',
111  'key2' => [
112  'subkey2.1' => 'value2.1',
113  'subkey2.2' => 'multiline' . PHP_EOL . 'string',
114  'subkey2.3' => new \Magento\Framework\DataObject(['test_key' => 'test_value']),
115  ],
116  'key3' => 5,
117  ];
118  foreach ($data as $key => $value) {
119  $this->dataObject->setData($key, $value);
120  }
121  $this->assertEquals($data, $this->dataObject->getData());
122  $this->assertEquals('value1', $this->dataObject->getData('key1'));
123  $this->assertEquals('value2.1', $this->dataObject->getData('key2/subkey2.1'));
124  $this->assertEquals('value2.1', $this->dataObject->getData('key2', 'subkey2.1'));
125  $this->assertEquals('string', $this->dataObject->getData('key2/subkey2.2', 1));
126  $this->assertEquals('test_value', $this->dataObject->getData('key2/subkey2.3', 'test_key'));
127  $this->assertNull($this->dataObject->getData('key3', 'test_key'));
128  }
129 
130  public function testGetDataByPath()
131  {
132  $data = [
133  'key1' => 'value1',
134  'key2' => [
135  'subkey2.1' => 'value2.1',
136  'subkey2.2' => 'multiline
137 string',
138  'subkey2.3' => new \Magento\Framework\DataObject(['test_key' => 'test_value']),
139  ],
140  ];
141  foreach ($data as $key => $value) {
142  $this->dataObject->setData($key, $value);
143  }
144  $this->assertEquals('value1', $this->dataObject->getDataByPath('key1'));
145  $this->assertEquals('value2.1', $this->dataObject->getDataByPath('key2/subkey2.1'));
146  $this->assertEquals('test_value', $this->dataObject->getDataByPath('key2/subkey2.3/test_key'));
147  $this->assertNull($this->dataObject->getDataByPath('empty'));
148  $this->assertNull($this->dataObject->getDataByPath('empty/path'));
149  }
150 
151  public function testGetDataByKey()
152  {
153  $this->dataObject->setData('key', 'value');
154  $this->assertEquals('value', $this->dataObject->getDataByKey('key'));
155  $this->assertNull($this->dataObject->getDataByKey('empty'));
156  }
157 
161  public function testSetGetDataUsingMethod()
162  {
163  $mock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['setTestData', 'getTestData']);
164  $mock->expects($this->once())->method('setTestData')->with($this->equalTo('data'));
165  $mock->expects($this->once())->method('getTestData');
166 
167  $mock->setDataUsingMethod('test_data', 'data');
168  $mock->getDataUsingMethod('test_data');
169  }
170 
176  {
177  $this->dataObject->setData('key_1', 'value1');
178  $this->assertTrue($this->dataObject->hasData('key_1'));
179  $this->assertEquals('value1', $this->dataObject->getDataUsingMethod('key_1'));
180 
181  $this->dataObject->setData('key2', 'value2');
182  $this->assertEquals('value2', $this->dataObject->getData('key2'));
183  $this->assertEquals(null, $this->dataObject->getKey2());
184  $this->assertEquals(null, $this->dataObject->getDataUsingMethod('key2'));
185  }
186 
190  public function testHasData()
191  {
192  $this->assertFalse($this->dataObject->hasData());
193  $this->assertFalse($this->dataObject->hasData('key'));
194  $this->dataObject->setData('key', 'value');
195  $this->assertTrue($this->dataObject->hasData('key'));
196  }
197 
201  public function testToArray()
202  {
203  $this->assertEquals([], $this->dataObject->toArray());
204  $this->assertEquals(['key' => null], $this->dataObject->toArray(['key']));
205  $this->dataObject->setData('key1', 'value1');
206  $this->dataObject->setData('key2', 'value2');
207  $this->assertEquals(['key1' => 'value1'], $this->dataObject->toArray(['key1']));
208  $this->assertEquals(['key2' => 'value2'], $this->dataObject->convertToArray(['key2']));
209  }
210 
214  public function testToXml()
215  {
216  $this->dataObject->setData('key1', 'value1');
217  $this->dataObject->setData('key2', 'value2');
218  $xml = '<item>
219 <key1><![CDATA[value1]]></key1>
220 <key2><![CDATA[value2]]></key2>
221 </item>
222 ';
223  $this->assertEquals($xml, $this->dataObject->toXml());
224 
225  $xml = '<item>
226 <key2><![CDATA[value2]]></key2>
227 </item>
228 ';
229  $this->assertEquals($xml, $this->dataObject->toXml(['key2']));
230 
231  $xml = '<my_item>
232 <key1><![CDATA[value1]]></key1>
233 <key2><![CDATA[value2]]></key2>
234 </my_item>
235 ';
236  $this->assertEquals($xml, $this->dataObject->toXml([], 'my_item'));
237 
238  $xml = '<key1><![CDATA[value1]]></key1>
239 <key2><![CDATA[value2]]></key2>
240 ';
241  $this->assertEquals($xml, $this->dataObject->toXml([], false));
242 
243  $xml = '<?xml version="1.0" encoding="UTF-8"?>
244 <item>
245 <key1><![CDATA[value1]]></key1>
246 <key2><![CDATA[value2]]></key2>
247 </item>
248 ';
249  $this->assertEquals($xml, $this->dataObject->toXml([], 'item', true));
250 
251  $xml = '<?xml version="1.0" encoding="UTF-8"?>
252 <item>
253 <key1>value1</key1>
254 <key2>value2</key2>
255 </item>
256 ';
257  $this->assertEquals($xml, $this->dataObject->convertToXml([], 'item', true, false));
258  }
259 
263  public function testToJson()
264  {
265  $this->dataObject->setData('key1', 'value1');
266  $this->dataObject->setData('key2', 'value2');
267  $this->assertEquals('{"key1":"value1","key2":"value2"}', $this->dataObject->toJson());
268  $this->assertEquals('{"key1":"value1"}', $this->dataObject->toJson(['key1']));
269  $this->assertEquals('{"key1":"value1","key":null}', $this->dataObject->convertToJson(['key1', 'key']));
270  }
271 
275  public function testToString()
276  {
277  $this->dataObject->setData('key1', 'value1');
278  $this->dataObject->setData('key2', 'value2');
279  $this->assertEquals('value1, value2', $this->dataObject->toString());
280  $this->assertEquals('test value1 with value2', $this->dataObject->toString('test {{key1}} with {{key2}}'));
281  }
282 
288  public function testCall()
289  {
290  $this->dataObject->setData('key', 'value');
291  $this->dataObject->setTest('test');
292  $this->assertEquals('test', $this->dataObject->getData('test'));
293 
294  $this->assertEquals($this->dataObject->getData('test'), $this->dataObject->getTest());
295 
296  $this->assertTrue($this->dataObject->hasTest());
297  $this->dataObject->unsTest();
298  $this->assertNull($this->dataObject->getData('test'));
299 
300  $this->dataObject->testTest();
301  }
302 
306  public function testGetSet()
307  {
309  $this->dataObject->test = 'test';
311  $this->assertEquals('test', $this->dataObject->test);
312 
314  $this->dataObject->testTest = 'test';
316  $this->assertEquals('test', $this->dataObject->testTest);
317  }
318 
322  public function testIsEmpty()
323  {
324  $this->assertTrue($this->dataObject->isEmpty());
325  $this->dataObject->setData('test', 'test');
326  $this->assertFalse($this->dataObject->isEmpty());
327  }
328 
332  public function testSerialize()
333  {
334  $this->dataObject->setData('key1', 'value1');
335  $this->dataObject->setData('key2', 'value2');
336  $this->assertEquals('key1="value1" key2="value2"', $this->dataObject->serialize());
337  $this->assertEquals(
338  'key1:\'value1\'_key2:\'value2\'',
339  $this->dataObject->serialize(['key', 'key1', 'key2'], ':', '_', '\'')
340  );
341  }
342 
346  public function testDebug()
347  {
348  $data = ['key1' => 'value1', 'key2' => ['test'], 'key3' => $this->dataObject];
349  foreach ($data as $key => $value) {
350  $this->dataObject->setData($key, $value);
351  }
352  $debug = $data;
353  unset($debug['key3']);
354  $debug['key3 (Magento\Framework\DataObject)'] = '*** RECURSION ***';
355  $this->assertEquals($debug, $this->dataObject->debug());
356  }
357 
361  public function testOffset()
362  {
363  $this->dataObject->offsetSet('key1', 'value1');
364  $this->assertTrue($this->dataObject->offsetExists('key1'));
365  $this->assertFalse($this->dataObject->offsetExists('key2'));
366 
367  $this->assertEquals('value1', $this->dataObject->offsetGet('key1'));
368  $this->assertNull($this->dataObject->offsetGet('key2'));
369  $this->dataObject->offsetUnset('key1');
370  $this->assertFalse($this->dataObject->offsetExists('key1'));
371  }
372 
378  public function testUnderscore($input, $expectedOutput)
379  {
380  $refObject = new \ReflectionObject($this->dataObject);
381  $refMethod = $refObject->getMethod('_underscore');
382  $refMethod->setAccessible(true);
383  $output = $refMethod->invoke($this->dataObject, $input);
384  $this->assertEquals($expectedOutput, $output);
385  }
386 
390  public function underscoreDataProvider()
391  {
392  return [
393  'Test 1' => ['Stone1Color', 'stone_1_color'],
394  'Test 2' => ['StoneColor', 'stone_color'],
395  'Test 3' => ['StoneToXml', 'stone_to_xml'],
396  'Test 4' => ['1StoneColor', '1_stone_color'],
397  'Test 5' => ['getCcLast4', 'get_cc_last_4'],
398  'Test 6' => ['99Bottles', '99_bottles'],
399  'Test 7' => ['XApiLogin', 'x_api_login']
400  ];
401  }
402 }
$value
Definition: gender.phtml:16
$debug
Definition: router.php:22