PageRenderTime 30ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/internal/Magento/Framework/Test/Unit/ObjectTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 381 lines | 262 code | 45 blank | 74 comment | 0 complexity | cbc73d921ebb739096c956a953597e0c MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * \Magento\Framework\DataObject test case.
  8. */
  9. namespace Magento\Framework\Test\Unit;
  10. use PHPUnit_Framework_TestCase;
  11. class ObjectTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\DataObject
  15. */
  16. private $_object;
  17. /**
  18. * Prepares the environment before running a test.
  19. */
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $this->_object = new \Magento\Framework\DataObject();
  24. }
  25. /**
  26. * Cleans up the environment after running a test.
  27. */
  28. protected function tearDown()
  29. {
  30. $this->_object = null;
  31. parent::tearDown();
  32. }
  33. /**
  34. * Tests \Magento\Framework\DataObject->__construct()
  35. */
  36. public function testConstruct()
  37. {
  38. $object = new \Magento\Framework\DataObject();
  39. $this->assertEquals([], $object->getData());
  40. $data = ['test' => 'test'];
  41. $object = new \Magento\Framework\DataObject($data);
  42. $this->assertEquals($data, $object->getData());
  43. }
  44. /**
  45. * Tests \Magento\Framework\DataObject->addData()
  46. */
  47. public function testAddData()
  48. {
  49. $this->_object->addData(['test' => 'value']);
  50. $this->assertEquals('value', $this->_object->getData('test'));
  51. $this->_object->addData(['test' => 'value1']);
  52. $this->assertEquals('value1', $this->_object->getData('test'));
  53. $this->_object->addData(['test2' => 'value2']);
  54. $this->assertEquals(['test' => 'value1', 'test2' => 'value2'], $this->_object->getData());
  55. }
  56. /**
  57. * Tests \Magento\Framework\DataObject->setData()
  58. */
  59. public function testSetData()
  60. {
  61. $data = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 3];
  62. $this->_object->setData($data);
  63. $this->assertEquals($data, $this->_object->getData());
  64. $data['key1'] = 1;
  65. $this->_object->setData('key1', 1);
  66. $this->assertEquals($data, $this->_object->getData());
  67. $this->_object->setData('key1');
  68. $data['key1'] = null;
  69. $this->assertEquals($data, $this->_object->getData());
  70. }
  71. /**
  72. * Tests \Magento\Framework\DataObject->unsetData()
  73. */
  74. public function testUnsetData()
  75. {
  76. $data = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 3, 'key4' => 4];
  77. $this->_object->setData($data);
  78. $this->_object->unsetData('key1');
  79. unset($data['key1']);
  80. $this->assertEquals($data, $this->_object->getData());
  81. $this->_object->unsetData(['key2', 'key3']);
  82. unset($data['key2']);
  83. unset($data['key3']);
  84. $this->assertEquals($data, $this->_object->getData());
  85. $this->_object->unsetData();
  86. $this->assertEquals([], $this->_object->getData());
  87. }
  88. /**
  89. * Tests \Magento\Framework\DataObject->getData()
  90. */
  91. public function testGetData()
  92. {
  93. $data = [
  94. 'key1' => 'value1',
  95. 'key2' => [
  96. 'subkey2.1' => 'value2.1',
  97. 'subkey2.2' => 'multiline'. PHP_EOL .'string',
  98. 'subkey2.3' => new \Magento\Framework\DataObject(['test_key' => 'test_value']),
  99. ],
  100. 'key3' => 5,
  101. ];
  102. foreach ($data as $key => $value) {
  103. $this->_object->setData($key, $value);
  104. }
  105. $this->assertEquals($data, $this->_object->getData());
  106. $this->assertEquals('value1', $this->_object->getData('key1'));
  107. $this->assertEquals('value2.1', $this->_object->getData('key2/subkey2.1'));
  108. $this->assertEquals('value2.1', $this->_object->getData('key2', 'subkey2.1'));
  109. $this->assertEquals('string', $this->_object->getData('key2/subkey2.2', 1));
  110. $this->assertEquals('test_value', $this->_object->getData('key2/subkey2.3', 'test_key'));
  111. $this->assertNull($this->_object->getData('key3', 'test_key'));
  112. }
  113. public function testGetDataByPath()
  114. {
  115. $data = [
  116. 'key1' => 'value1',
  117. 'key2' => [
  118. 'subkey2.1' => 'value2.1',
  119. 'subkey2.2' => 'multiline
  120. string',
  121. 'subkey2.3' => new \Magento\Framework\DataObject(['test_key' => 'test_value']),
  122. ],
  123. ];
  124. foreach ($data as $key => $value) {
  125. $this->_object->setData($key, $value);
  126. }
  127. $this->assertEquals('value1', $this->_object->getDataByPath('key1'));
  128. $this->assertEquals('value2.1', $this->_object->getDataByPath('key2/subkey2.1'));
  129. $this->assertEquals('test_value', $this->_object->getDataByPath('key2/subkey2.3/test_key'));
  130. $this->assertNull($this->_object->getDataByPath('empty'));
  131. $this->assertNull($this->_object->getDataByPath('empty/path'));
  132. }
  133. public function testGetDataByKey()
  134. {
  135. $this->_object->setData('key', 'value');
  136. $this->assertEquals('value', $this->_object->getDataByKey('key'));
  137. $this->assertNull($this->_object->getDataByKey('empty'));
  138. }
  139. /**
  140. * Tests \Magento\Framework\DataObject->setDataUsingMethod()
  141. */
  142. public function testSetGetDataUsingMethod()
  143. {
  144. $mock = $this->getMock(\Magento\Framework\DataObject::class, ['setTestData', 'getTestData']);
  145. $mock->expects($this->once())->method('setTestData')->with($this->equalTo('data'));
  146. $mock->expects($this->once())->method('getTestData');
  147. $mock->setDataUsingMethod('test_data', 'data');
  148. $mock->getDataUsingMethod('test_data');
  149. }
  150. /**
  151. * Tests \Magento\Framework\DataObject->hasData()
  152. */
  153. public function testHasData()
  154. {
  155. $this->assertFalse($this->_object->hasData());
  156. $this->assertFalse($this->_object->hasData('key'));
  157. $this->_object->setData('key', 'value');
  158. $this->assertTrue($this->_object->hasData('key'));
  159. }
  160. /**
  161. * Tests \Magento\Framework\DataObject->toArray()
  162. */
  163. public function testToArray()
  164. {
  165. $this->assertEquals([], $this->_object->toArray());
  166. $this->assertEquals(['key' => null], $this->_object->toArray(['key']));
  167. $this->_object->setData('key1', 'value1');
  168. $this->_object->setData('key2', 'value2');
  169. $this->assertEquals(['key1' => 'value1'], $this->_object->toArray(['key1']));
  170. $this->assertEquals(['key2' => 'value2'], $this->_object->convertToArray(['key2']));
  171. }
  172. /**
  173. * Tests \Magento\Framework\DataObject->toXml()
  174. */
  175. public function testToXml()
  176. {
  177. $this->_object->setData('key1', 'value1');
  178. $this->_object->setData('key2', 'value2');
  179. $xml = '<item>
  180. <key1><![CDATA[value1]]></key1>
  181. <key2><![CDATA[value2]]></key2>
  182. </item>
  183. ';
  184. $this->assertEquals($xml, $this->_object->toXml());
  185. $xml = '<item>
  186. <key2><![CDATA[value2]]></key2>
  187. </item>
  188. ';
  189. $this->assertEquals($xml, $this->_object->toXml(['key2']));
  190. $xml = '<my_item>
  191. <key1><![CDATA[value1]]></key1>
  192. <key2><![CDATA[value2]]></key2>
  193. </my_item>
  194. ';
  195. $this->assertEquals($xml, $this->_object->toXml([], 'my_item'));
  196. $xml = '<key1><![CDATA[value1]]></key1>
  197. <key2><![CDATA[value2]]></key2>
  198. ';
  199. $this->assertEquals($xml, $this->_object->toXml([], false));
  200. $xml = '<?xml version="1.0" encoding="UTF-8"?>
  201. <item>
  202. <key1><![CDATA[value1]]></key1>
  203. <key2><![CDATA[value2]]></key2>
  204. </item>
  205. ';
  206. $this->assertEquals($xml, $this->_object->toXml([], 'item', true));
  207. $xml = '<?xml version="1.0" encoding="UTF-8"?>
  208. <item>
  209. <key1>value1</key1>
  210. <key2>value2</key2>
  211. </item>
  212. ';
  213. $this->assertEquals($xml, $this->_object->convertToXml([], 'item', true, false));
  214. }
  215. /**
  216. * Tests \Magento\Framework\DataObject->toJson()
  217. */
  218. public function testToJson()
  219. {
  220. $this->_object->setData('key1', 'value1');
  221. $this->_object->setData('key2', 'value2');
  222. $this->assertEquals('{"key1":"value1","key2":"value2"}', $this->_object->toJson());
  223. $this->assertEquals('{"key1":"value1"}', $this->_object->toJson(['key1']));
  224. $this->assertEquals('{"key1":"value1","key":null}', $this->_object->convertToJson(['key1', 'key']));
  225. }
  226. /**
  227. * Tests \Magento\Framework\DataObject->toString()
  228. */
  229. public function testToString()
  230. {
  231. $this->_object->setData('key1', 'value1');
  232. $this->_object->setData('key2', 'value2');
  233. $this->assertEquals('value1, value2', $this->_object->toString());
  234. $this->assertEquals('test value1 with value2', $this->_object->toString('test {{key1}} with {{key2}}'));
  235. }
  236. /**
  237. * Tests \Magento\Framework\DataObject->__call()
  238. *
  239. * @expectedException \Magento\Framework\Exception\LocalizedException
  240. */
  241. public function testCall()
  242. {
  243. $this->_object->setData('key', 'value');
  244. $this->_object->setTest('test');
  245. $this->assertEquals('test', $this->_object->getData('test'));
  246. $this->assertEquals($this->_object->getData('test'), $this->_object->getTest());
  247. $this->assertTrue($this->_object->hasTest());
  248. $this->_object->unsTest();
  249. $this->assertNull($this->_object->getData('test'));
  250. $this->_object->testTest();
  251. }
  252. /**
  253. * Tests \Magento\Framework\DataObject->__get()
  254. */
  255. public function testGetSet()
  256. {
  257. $this->_object->test = 'test';
  258. $this->assertEquals('test', $this->_object->test);
  259. $this->_object->testTest = 'test';
  260. $this->assertEquals('test', $this->_object->testTest);
  261. }
  262. /**
  263. * Tests \Magento\Framework\DataObject->isEmpty()
  264. */
  265. public function testIsEmpty()
  266. {
  267. $this->assertTrue($this->_object->isEmpty());
  268. $this->_object->setData('test', 'test');
  269. $this->assertFalse($this->_object->isEmpty());
  270. }
  271. /**
  272. * Tests \Magento\Framework\DataObject->serialize()
  273. */
  274. public function testSerialize()
  275. {
  276. $this->_object->setData('key1', 'value1');
  277. $this->_object->setData('key2', 'value2');
  278. $this->assertEquals('key1="value1" key2="value2"', $this->_object->serialize());
  279. $this->assertEquals(
  280. 'key1:\'value1\'_key2:\'value2\'',
  281. $this->_object->serialize(['key', 'key1', 'key2'], ':', '_', '\'')
  282. );
  283. }
  284. /**
  285. * Tests \Magento\Framework\DataObject->debug()
  286. */
  287. public function testDebug()
  288. {
  289. $data = ['key1' => 'value1', 'key2' => ['test'], 'key3' => $this->_object];
  290. foreach ($data as $key => $value) {
  291. $this->_object->setData($key, $value);
  292. }
  293. $debug = $data;
  294. unset($debug['key3']);
  295. $debug['key3 (Magento\Framework\DataObject)'] = '*** RECURSION ***';
  296. $this->assertEquals($debug, $this->_object->debug());
  297. }
  298. /**
  299. * Tests \Magento\Framework\DataObject->offsetSet()
  300. */
  301. public function testOffset()
  302. {
  303. $this->_object->offsetSet('key1', 'value1');
  304. $this->assertTrue($this->_object->offsetExists('key1'));
  305. $this->assertFalse($this->_object->offsetExists('key2'));
  306. $this->assertEquals('value1', $this->_object->offsetGet('key1'));
  307. $this->assertNull($this->_object->offsetGet('key2'));
  308. $this->_object->offsetUnset('key1');
  309. $this->assertFalse($this->_object->offsetExists('key1'));
  310. }
  311. /**
  312. * Tests _underscore method directly
  313. *
  314. * @dataProvider underscoreDataProvider
  315. */
  316. public function testUnderscore($input, $expectedOutput)
  317. {
  318. $refObject = new \ReflectionObject($this->_object);
  319. $refMethod = $refObject->getMethod('_underscore');
  320. $refMethod->setAccessible(true);
  321. $output = $refMethod->invoke($this->_object, $input);
  322. $this->assertEquals($expectedOutput, $output);
  323. }
  324. public function underscoreDataProvider()
  325. {
  326. return [
  327. 'Test 1' => ['Stone1Color', 'stone_1_color'],
  328. 'Test 2' => ['StoneColor', 'stone_color'],
  329. 'Test 3' => ['StoneToXml', 'stone_to_xml'],
  330. 'Test 4' => ['1StoneColor', '1_stone_color'],
  331. 'Test 5' => ['getCcLast4', 'get_cc_last_4'],
  332. 'Test 6' => ['99Bottles', '99_bottles'],
  333. 'Test 7' => ['XApiLogin', 'x_api_login']
  334. ];
  335. }
  336. }