Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CopyTest.php
Go to the documentation of this file.
1 <?php
8 
9 class CopyTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $copy;
15 
20 
24  protected $eventManagerMock;
25 
29  protected $targetMock;
30 
34  protected $sourceMock;
35 
40 
41  protected function setUp()
42  {
43  $this->fieldsetConfigMock = $this->createMock(\Magento\Framework\DataObject\Copy\Config::class);
44  $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
45  $this->sourceMock = $this->createMock(\Magento\Framework\DataObject::class);
46  $this->targetMock = $this->createMock(\Magento\Framework\DataObject::class);
47  $this->extensionAttributesFactoryMock =
48  $this->createMock(\Magento\Framework\Api\ExtensionAttributesFactory::class);
49  $this->copy = new \Magento\Framework\DataObject\Copy(
50  $this->eventManagerMock,
51  $this->fieldsetConfigMock,
52  $this->extensionAttributesFactoryMock
53  );
54  }
55 
57  {
58  $this->fieldsetConfigMock->expects($this->never())->method('getFieldset');
59  $this->assertEquals(
60  null,
61  $this->copy->copyFieldsetToTarget('fieldset', 'aspect', [], 'target')
62  );
63  }
64 
66  {
67  $this->fieldsetConfigMock
68  ->expects($this->once())
69  ->method('getFieldset')
70  ->with('fieldset', 'global')
71  ->will($this->returnValue(null));
72  $this->eventManagerMock->expects($this->never())->method('dispatch');
73  $this->assertEquals(
74  [$this->targetMock],
75  $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $this->sourceMock, [$this->targetMock])
76  );
77  }
78 
80  {
81  $fields['code']['node']['aspect'] = [];
82  $this->fieldsetConfigMock
83  ->expects($this->once())
84  ->method('getFieldset')
85  ->with('fieldset', 'global')
86  ->will($this->returnValue($fields));
87 
88  $eventName = sprintf('core_copy_fieldset_%s_%s', 'fieldset', 'aspect');
89  $data = [
90  'target' => new \Magento\Framework\DataObject([$this->targetMock]),
91  'source' => $this->sourceMock,
92  'root' => 'global',
93  ];
94  $this->eventManagerMock->expects($this->once())->method('dispatch')->with($eventName, $data);
95  $this->assertEquals(
96  [$this->targetMock],
97  $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $this->sourceMock, [$this->targetMock])
98  );
99  }
100 
102  {
103  $fields['code']['aspect'] = 'value';
104  $this->fieldsetConfigMock
105  ->expects($this->once())
106  ->method('getFieldset')
107  ->with('fieldset', 'global')
108  ->will($this->returnValue($fields));
109 
110  $this->sourceMock
111  ->expects($this->once())
112  ->method('getDataUsingMethod')
113  ->with('code')
114  ->will($this->returnValue('value'));
115 
116  $this->targetMock
117  ->expects($this->once())
118  ->method('setDataUsingMethod')
119  ->with('value')
120  ->will($this->returnSelf());
121  $eventName = sprintf('core_copy_fieldset_%s_%s', 'fieldset', 'aspect');
122  $data = [
123  'target' => $this->targetMock,
124  'source' => $this->sourceMock,
125  'root' => 'global',
126  ];
127  $this->eventManagerMock->expects($this->once())->method('dispatch')->with($eventName, $data);
128  $this->assertEquals(
129  $this->targetMock,
130  $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $this->sourceMock, $this->targetMock)
131  );
132  }
133 
135  {
136  $fields['code']['aspect'] = 'value';
137  $target['code'] = [];
138  $this->fieldsetConfigMock
139  ->expects($this->once())
140  ->method('getFieldset')
141  ->with('fieldset', 'global')
142  ->will($this->returnValue($fields));
143 
144  $this->sourceMock
145  ->expects($this->once())
146  ->method('getDataUsingMethod')
147  ->with('code')
148  ->will($this->returnValue('value'));
149 
150  $this->targetMock
151  ->expects($this->never())
152  ->method('setDataUsingMethod');
153  $eventName = sprintf('core_copy_fieldset_%s_%s', 'fieldset', 'aspect');
154  $newTarget = [
155  'code' => [],
156  'value' => 'value',
157  ];
158  $data = [
159  'target' => new \Magento\Framework\DataObject($newTarget),
160  'source' => $this->sourceMock,
161  'root' => 'global',
162  ];
163  $this->eventManagerMock->expects($this->once())->method('dispatch')->with($eventName, $data);
164  $this->assertEquals(
165  $newTarget,
166  $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $this->sourceMock, $target)
167  );
168  }
169 
171  {
172  $fields['code']['aspect'] = '*';
173  $this->fieldsetConfigMock
174  ->expects($this->once())
175  ->method('getFieldset')
176  ->with('fieldset', 'global')
177  ->will($this->returnValue($fields));
178 
179  $sourceMock = $this->createPartialMock(\Magento\Framework\Api\ExtensibleDataInterface::class, [
180  'getExtensionAttributes', 'getCode'
181  ]);
182  $targetMock = $this->createPartialMock(\Magento\Framework\Api\ExtensibleDataInterface::class, [
183  'getExtensionAttributes',
184  'setCode',
185  'setExtensionAttributes'
186  ]);
187 
189  ->expects($this->any())
190  ->method('getExtensionAttributes')
191  ->willReturnSelf();
193  ->expects($this->once())
194  ->method('getCode')
195  ->willReturn('code');
196 
198  ->expects($this->any())
199  ->method('getExtensionAttributes')
200  ->willReturnSelf();
202  ->expects($this->any())
203  ->method('setExtensionAttributes')
204  ->willReturnSelf();
206  ->expects($this->once())
207  ->method('setCode')
208  ->with('code');
209 
210  $this->eventManagerMock->expects($this->once())->method('dispatch');
211  $result = $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $sourceMock, $targetMock);
212  $this->assertEquals($result, $targetMock);
213  }
214 
216  {
217  $fields['code']['aspect'] = '*';
218  $source['code'] = 'code';
219  $this->fieldsetConfigMock
220  ->expects($this->once())
221  ->method('getFieldset')
222  ->with('fieldset', 'global')
223  ->will($this->returnValue($fields));
224 
225  $sourceMock = $this->createPartialMock(\Magento\Framework\Api\AbstractSimpleObject::class, [
226  '__toArray'
227  ]);
228  $targetMock = $this->createPartialMock(\Magento\Framework\Api\AbstractSimpleObject::class, [
229  'setData'
230  ]);
231 
233  ->expects($this->once())
234  ->method('__toArray')
235  ->willReturn($source);
236 
238  ->expects($this->once())
239  ->method('setData')
240  ->with('code', 'code');
241 
242  $this->eventManagerMock->expects($this->once())->method('dispatch');
243  $result = $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $sourceMock, $targetMock);
244  $this->assertEquals($result, $targetMock);
245  }
246 
248  {
249  $this->fieldsetConfigMock->expects($this->never())->method('getFieldset');
250  $this->assertNull($this->copy->getDataFromFieldset('fieldset', 'aspect', 'source'));
251  }
252 
254  {
255  $this->fieldsetConfigMock
256  ->expects($this->once())
257  ->method('getFieldset')
258  ->with('fieldset', 'global')
259  ->will($this->returnValue(null));
260  $this->sourceMock
261  ->expects($this->never())
262  ->method('getDataUsingMethod');
263  $this->assertNull($this->copy->getDataFromFieldset('fieldset', 'aspect', $this->sourceMock));
264  }
265 
267  {
268  $fields['code']['aspect'] = 'value';
269  $this->fieldsetConfigMock
270  ->expects($this->once())
271  ->method('getFieldset')
272  ->with('fieldset', 'global')
273  ->will($this->returnValue($fields));
274  $this->sourceMock
275  ->expects($this->once())
276  ->method('getDataUsingMethod')
277  ->with('code')
278  ->will($this->returnValue('value'));
279 
280  $this->assertEquals(
281  ['value' => 'value'],
282  $this->copy->getDataFromFieldset('fieldset', 'aspect', $this->sourceMock)
283  );
284  }
285 
287  {
288  $fields['code']['aspect'] = [];
289  $this->fieldsetConfigMock
290  ->expects($this->once())
291  ->method('getFieldset')
292  ->with('fieldset', 'global')
293  ->will($this->returnValue($fields));
294  $this->sourceMock
295  ->expects($this->never())
296  ->method('getDataUsingMethod');
297 
298  $this->assertEquals(
299  [],
300  $this->copy->getDataFromFieldset('fieldset', 'aspect', $this->sourceMock)
301  );
302  }
303 }
$source
Definition: source.php:23
$target
Definition: skip.phtml:8
$fields
Definition: details.phtml:14