CopyTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DataObject\Test\Unit;
  7. class CopyTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\DataObject\Copy
  11. */
  12. protected $copy;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $fieldsetConfigMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $eventManagerMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $targetMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $sourceMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $extensionAttributesFactoryMock;
  33. protected function setUp()
  34. {
  35. $this->fieldsetConfigMock = $this->createMock(\Magento\Framework\DataObject\Copy\Config::class);
  36. $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  37. $this->sourceMock = $this->createMock(\Magento\Framework\DataObject::class);
  38. $this->targetMock = $this->createMock(\Magento\Framework\DataObject::class);
  39. $this->extensionAttributesFactoryMock =
  40. $this->createMock(\Magento\Framework\Api\ExtensionAttributesFactory::class);
  41. $this->copy = new \Magento\Framework\DataObject\Copy(
  42. $this->eventManagerMock,
  43. $this->fieldsetConfigMock,
  44. $this->extensionAttributesFactoryMock
  45. );
  46. }
  47. public function testCopyFieldsetToTargetWhenFieldsetInputInvalid()
  48. {
  49. $this->fieldsetConfigMock->expects($this->never())->method('getFieldset');
  50. $this->assertEquals(
  51. null,
  52. $this->copy->copyFieldsetToTarget('fieldset', 'aspect', [], 'target')
  53. );
  54. }
  55. public function testCopyFieldsetToTargetWhenFieldIsNotExists()
  56. {
  57. $this->fieldsetConfigMock
  58. ->expects($this->once())
  59. ->method('getFieldset')
  60. ->with('fieldset', 'global')
  61. ->will($this->returnValue(null));
  62. $this->eventManagerMock->expects($this->never())->method('dispatch');
  63. $this->assertEquals(
  64. [$this->targetMock],
  65. $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $this->sourceMock, [$this->targetMock])
  66. );
  67. }
  68. public function testCopyFieldsetToTargetWhenFieldExists()
  69. {
  70. $fields['code']['node']['aspect'] = [];
  71. $this->fieldsetConfigMock
  72. ->expects($this->once())
  73. ->method('getFieldset')
  74. ->with('fieldset', 'global')
  75. ->will($this->returnValue($fields));
  76. $eventName = sprintf('core_copy_fieldset_%s_%s', 'fieldset', 'aspect');
  77. $data = [
  78. 'target' => new \Magento\Framework\DataObject([$this->targetMock]),
  79. 'source' => $this->sourceMock,
  80. 'root' => 'global',
  81. ];
  82. $this->eventManagerMock->expects($this->once())->method('dispatch')->with($eventName, $data);
  83. $this->assertEquals(
  84. [$this->targetMock],
  85. $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $this->sourceMock, [$this->targetMock])
  86. );
  87. }
  88. public function testCopyFieldsetToTargetWhenTargetNotArray()
  89. {
  90. $fields['code']['aspect'] = 'value';
  91. $this->fieldsetConfigMock
  92. ->expects($this->once())
  93. ->method('getFieldset')
  94. ->with('fieldset', 'global')
  95. ->will($this->returnValue($fields));
  96. $this->sourceMock
  97. ->expects($this->once())
  98. ->method('getDataUsingMethod')
  99. ->with('code')
  100. ->will($this->returnValue('value'));
  101. $this->targetMock
  102. ->expects($this->once())
  103. ->method('setDataUsingMethod')
  104. ->with('value')
  105. ->will($this->returnSelf());
  106. $eventName = sprintf('core_copy_fieldset_%s_%s', 'fieldset', 'aspect');
  107. $data = [
  108. 'target' => $this->targetMock,
  109. 'source' => $this->sourceMock,
  110. 'root' => 'global',
  111. ];
  112. $this->eventManagerMock->expects($this->once())->method('dispatch')->with($eventName, $data);
  113. $this->assertEquals(
  114. $this->targetMock,
  115. $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $this->sourceMock, $this->targetMock)
  116. );
  117. }
  118. public function testGetCopyFieldsetToTargetWhenTargetIsArray()
  119. {
  120. $fields['code']['aspect'] = 'value';
  121. $target['code'] = [];
  122. $this->fieldsetConfigMock
  123. ->expects($this->once())
  124. ->method('getFieldset')
  125. ->with('fieldset', 'global')
  126. ->will($this->returnValue($fields));
  127. $this->sourceMock
  128. ->expects($this->once())
  129. ->method('getDataUsingMethod')
  130. ->with('code')
  131. ->will($this->returnValue('value'));
  132. $this->targetMock
  133. ->expects($this->never())
  134. ->method('setDataUsingMethod');
  135. $eventName = sprintf('core_copy_fieldset_%s_%s', 'fieldset', 'aspect');
  136. $newTarget = [
  137. 'code' => [],
  138. 'value' => 'value',
  139. ];
  140. $data = [
  141. 'target' => new \Magento\Framework\DataObject($newTarget),
  142. 'source' => $this->sourceMock,
  143. 'root' => 'global',
  144. ];
  145. $this->eventManagerMock->expects($this->once())->method('dispatch')->with($eventName, $data);
  146. $this->assertEquals(
  147. $newTarget,
  148. $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $this->sourceMock, $target)
  149. );
  150. }
  151. public function testGetCopyFieldsetToTargetWhenTargetIsExtensibleDataInterface()
  152. {
  153. $fields['code']['aspect'] = '*';
  154. $this->fieldsetConfigMock
  155. ->expects($this->once())
  156. ->method('getFieldset')
  157. ->with('fieldset', 'global')
  158. ->will($this->returnValue($fields));
  159. $sourceMock = $this->createPartialMock(\Magento\Framework\Api\ExtensibleDataInterface::class, [
  160. 'getExtensionAttributes', 'getCode'
  161. ]);
  162. $targetMock = $this->createPartialMock(\Magento\Framework\Api\ExtensibleDataInterface::class, [
  163. 'getExtensionAttributes',
  164. 'setCode',
  165. 'setExtensionAttributes'
  166. ]);
  167. $sourceMock
  168. ->expects($this->any())
  169. ->method('getExtensionAttributes')
  170. ->willReturnSelf();
  171. $sourceMock
  172. ->expects($this->once())
  173. ->method('getCode')
  174. ->willReturn('code');
  175. $targetMock
  176. ->expects($this->any())
  177. ->method('getExtensionAttributes')
  178. ->willReturnSelf();
  179. $targetMock
  180. ->expects($this->any())
  181. ->method('setExtensionAttributes')
  182. ->willReturnSelf();
  183. $targetMock
  184. ->expects($this->once())
  185. ->method('setCode')
  186. ->with('code');
  187. $this->eventManagerMock->expects($this->once())->method('dispatch');
  188. $result = $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $sourceMock, $targetMock);
  189. $this->assertEquals($result, $targetMock);
  190. }
  191. public function testGetCopyFieldsetToTargetWhenTargetIsAbstractSimpleObject()
  192. {
  193. $fields['code']['aspect'] = '*';
  194. $source['code'] = 'code';
  195. $this->fieldsetConfigMock
  196. ->expects($this->once())
  197. ->method('getFieldset')
  198. ->with('fieldset', 'global')
  199. ->will($this->returnValue($fields));
  200. $sourceMock = $this->createPartialMock(\Magento\Framework\Api\AbstractSimpleObject::class, [
  201. '__toArray'
  202. ]);
  203. $targetMock = $this->createPartialMock(\Magento\Framework\Api\AbstractSimpleObject::class, [
  204. 'setData'
  205. ]);
  206. $sourceMock
  207. ->expects($this->once())
  208. ->method('__toArray')
  209. ->willReturn($source);
  210. $targetMock
  211. ->expects($this->once())
  212. ->method('setData')
  213. ->with('code', 'code');
  214. $this->eventManagerMock->expects($this->once())->method('dispatch');
  215. $result = $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $sourceMock, $targetMock);
  216. $this->assertEquals($result, $targetMock);
  217. }
  218. public function testGetDataFromFieldsetWhenSourceIsInvalid()
  219. {
  220. $this->fieldsetConfigMock->expects($this->never())->method('getFieldset');
  221. $this->assertNull($this->copy->getDataFromFieldset('fieldset', 'aspect', 'source'));
  222. }
  223. public function testGetDataFromFieldsetWhenFieldsetDoesNotExist()
  224. {
  225. $this->fieldsetConfigMock
  226. ->expects($this->once())
  227. ->method('getFieldset')
  228. ->with('fieldset', 'global')
  229. ->will($this->returnValue(null));
  230. $this->sourceMock
  231. ->expects($this->never())
  232. ->method('getDataUsingMethod');
  233. $this->assertNull($this->copy->getDataFromFieldset('fieldset', 'aspect', $this->sourceMock));
  234. }
  235. public function testGetDataFromFieldsetWhenFieldExists()
  236. {
  237. $fields['code']['aspect'] = 'value';
  238. $this->fieldsetConfigMock
  239. ->expects($this->once())
  240. ->method('getFieldset')
  241. ->with('fieldset', 'global')
  242. ->will($this->returnValue($fields));
  243. $this->sourceMock
  244. ->expects($this->once())
  245. ->method('getDataUsingMethod')
  246. ->with('code')
  247. ->will($this->returnValue('value'));
  248. $this->assertEquals(
  249. ['value' => 'value'],
  250. $this->copy->getDataFromFieldset('fieldset', 'aspect', $this->sourceMock)
  251. );
  252. }
  253. public function testGetDataFromFieldsetWhenFieldDoesNotExists()
  254. {
  255. $fields['code']['aspect'] = [];
  256. $this->fieldsetConfigMock
  257. ->expects($this->once())
  258. ->method('getFieldset')
  259. ->with('fieldset', 'global')
  260. ->will($this->returnValue($fields));
  261. $this->sourceMock
  262. ->expects($this->never())
  263. ->method('getDataUsingMethod');
  264. $this->assertEquals(
  265. [],
  266. $this->copy->getDataFromFieldset('fieldset', 'aspect', $this->sourceMock)
  267. );
  268. }
  269. }