1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\ImportExport\Strategy; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
9
|
|
|
|
10
|
|
|
use Oro\Bundle\AddressBundle\Entity\Country; |
11
|
|
|
use Oro\Bundle\IntegrationBundle\Entity\Channel; |
12
|
|
|
use Oro\Bundle\ImportExportBundle\Context\ContextInterface; |
13
|
|
|
|
14
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\CartAddress; |
15
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\CartItem; |
16
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Cart; |
17
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\CartStatus; |
18
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Customer; |
19
|
|
|
use OroCRM\Bundle\MagentoBundle\ImportExport\Strategy\CartStrategy; |
20
|
|
|
|
21
|
|
|
class CartStrategyTest extends AbstractStrategyTest |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
protected function getStrategy() |
27
|
|
|
{ |
28
|
|
|
$strategy = new CartStrategy( |
29
|
|
|
$this->eventDispatcher, |
30
|
|
|
$this->strategyHelper, |
31
|
|
|
$this->fieldHelper, |
32
|
|
|
$this->databaseHelper |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$strategy->setOwnerHelper($this->defaultOwnerHelper); |
36
|
|
|
$strategy->setLogger($this->logger); |
37
|
|
|
$strategy->setChannelHelper($this->channelHelper); |
38
|
|
|
$strategy->setAddressHelper($this->addressHelper); |
39
|
|
|
|
40
|
|
|
return $strategy; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param mixed $expected |
45
|
|
|
* @param mixed $entity |
46
|
|
|
* @param mixed $databaseEntity |
47
|
|
|
* |
48
|
|
|
* @dataProvider contactInfoDataProvider |
49
|
|
|
*/ |
50
|
|
|
public function testContactInfo($expected, $entity, $databaseEntity = null) |
51
|
|
|
{ |
52
|
|
|
$strategy = $this->getStrategy(); |
53
|
|
|
|
54
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ContextInterface $context */ |
55
|
|
|
$context = $this->getMock('Oro\Bundle\ImportExportBundle\Context\ContextInterface'); |
56
|
|
|
$strategy->setImportExportContext($context); |
57
|
|
|
$strategy->setEntityName('OroCRM\Bundle\MagentoBundle\Entity\Cart'); |
58
|
|
|
|
59
|
|
|
$execution = $this->getMock('Akeneo\Bundle\BatchBundle\Item\ExecutionContext'); |
60
|
|
|
$this->jobExecution->expects($this->any())->method('getExecutionContext') |
|
|
|
|
61
|
|
|
->will($this->returnValue($execution)); |
62
|
|
|
$strategy->setStepExecution($this->stepExecution); |
63
|
|
|
|
64
|
|
|
$this->databaseHelper->expects($this->once())->method('getEntityReference')->will($this->returnArgument(0)); |
|
|
|
|
65
|
|
|
$this->databaseHelper->expects($this->once())->method('findOneByIdentity')->willReturn($databaseEntity); |
66
|
|
|
|
67
|
|
|
$actualEntity = $strategy->process($entity); |
68
|
|
|
if ($actualEntity) { |
69
|
|
|
$expected->setImportedAt($actualEntity->getImportedAt()); |
70
|
|
|
$expected->setSyncedAt($actualEntity->getSyncedAt()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->assertEquals($expected, $actualEntity); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
* |
79
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
80
|
|
|
*/ |
81
|
|
|
public function contactInfoDataProvider() |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
|
|
'items count' => [null, $this->getEntity()], |
85
|
|
|
'without contact info' => [null, $this->getEntity(['itemsCount' => 1])], |
86
|
|
|
'email' => [ |
87
|
|
|
$this->getEntity(['itemsCount' => 1, 'email' => '[email protected]']), |
88
|
|
|
$this->getEntity(['itemsCount' => 1, 'email' => '[email protected]']), |
89
|
|
|
|
90
|
|
|
], |
91
|
|
|
'dont change status' => [ |
92
|
|
|
$this->getEntity( |
93
|
|
|
['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('custom')] |
94
|
|
|
), |
95
|
|
|
$this->getEntity( |
96
|
|
|
['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('custom')] |
97
|
|
|
), |
98
|
|
|
$this->getEntity( |
99
|
|
|
['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('custom')] |
100
|
|
|
) |
101
|
|
|
], |
102
|
|
|
'change status' => [ |
103
|
|
|
'expected' => $this->getEntity( |
104
|
|
|
['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('expired')] |
105
|
|
|
), |
106
|
|
|
'entity' => $this->getEntity( |
107
|
|
|
['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('expired')] |
108
|
|
|
), |
109
|
|
|
'databaseEntity' => $this->getEntity( |
110
|
|
|
['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('open')] |
111
|
|
|
) |
112
|
|
|
], |
113
|
|
|
'update customer email' => [ |
114
|
|
|
'expected' => $this->getEntity( |
115
|
|
|
[ |
116
|
|
|
'itemsCount' => 1, |
117
|
|
|
'email' => '[email protected]', |
118
|
|
|
'customer' => $this->getCustomer('[email protected]') |
119
|
|
|
] |
120
|
|
|
), |
121
|
|
|
'entity' => $this->getEntity( |
122
|
|
|
[ |
123
|
|
|
'itemsCount' => 1, |
124
|
|
|
'email' => '[email protected]', |
125
|
|
|
'customer' => $this->getCustomer() |
126
|
|
|
] |
127
|
|
|
), |
128
|
|
|
'databaseEntity' => $this->getEntity( |
129
|
|
|
[ |
130
|
|
|
'itemsCount' => 1, |
131
|
|
|
'email' => '[email protected]', |
132
|
|
|
'customer' => $this->getCustomer('[email protected]') |
133
|
|
|
] |
134
|
|
|
) |
135
|
|
|
], |
136
|
|
|
'add new cart item and remove old' => [ |
137
|
|
|
'expected' => $this->getEntity( |
138
|
|
|
[ |
139
|
|
|
'itemsCount' => 1, |
140
|
|
|
'email' => '[email protected]', |
141
|
|
|
'cartItems' => new ArrayCollection([$this->getCartItem(1)]), |
142
|
|
|
'itemsQty' => 1, |
143
|
|
|
] |
144
|
|
|
), |
145
|
|
|
'entity' => $this->getEntity( |
146
|
|
|
[ |
147
|
|
|
'itemsCount' => 1, |
148
|
|
|
'email' => '[email protected]', |
149
|
|
|
'cartItems' => new ArrayCollection([$this->getCartItem(1)]), |
150
|
|
|
'itemsQty' => 1, |
151
|
|
|
] |
152
|
|
|
), |
153
|
|
|
'databaseEntity' => $this->getEntity( |
154
|
|
|
[ |
155
|
|
|
'itemsCount' => 1, |
156
|
|
|
'email' => '[email protected]', |
157
|
|
|
'cartItems' => new ArrayCollection([$this->getCartItem(2)]), |
158
|
|
|
'itemsQty' => 1, |
159
|
|
|
] |
160
|
|
|
) |
161
|
|
|
], |
162
|
|
|
'add new cart item and keep old one' => [ |
163
|
|
|
'expected' => $this->getEntity( |
164
|
|
|
[ |
165
|
|
|
'itemsCount' => 1, |
166
|
|
|
'email' => '[email protected]', |
167
|
|
|
'cartItems' => new ArrayCollection([$this->getCartItem(1), $this->getCartItem(2)]), |
168
|
|
|
'itemsQty' => 2, |
169
|
|
|
] |
170
|
|
|
), |
171
|
|
|
'entity' => $this->getEntity( |
172
|
|
|
[ |
173
|
|
|
'itemsCount' => 1, |
174
|
|
|
'email' => '[email protected]', |
175
|
|
|
'cartItems' => new ArrayCollection([$this->getCartItem(1), $this->getCartItem(2)]), |
176
|
|
|
'itemsQty' => 2, |
177
|
|
|
] |
178
|
|
|
), |
179
|
|
|
'databaseEntity' => $this->getEntity( |
180
|
|
|
[ |
181
|
|
|
'itemsCount' => 1, |
182
|
|
|
'email' => '[email protected]', |
183
|
|
|
'cartItems' => new ArrayCollection([$this->getCartItem(2)]), |
184
|
|
|
'itemsQty' => 1, |
185
|
|
|
] |
186
|
|
|
) |
187
|
|
|
], |
188
|
|
|
'update existing address' => [ |
189
|
|
|
'expected' => $this->getEntity( |
190
|
|
|
[ |
191
|
|
|
'itemsCount' => 1, |
192
|
|
|
'email' => '[email protected]', |
193
|
|
|
'shippingAddress' => $this->getAddress('US') |
194
|
|
|
] |
195
|
|
|
), |
196
|
|
|
'entity' => $this->getEntity( |
197
|
|
|
[ |
198
|
|
|
'itemsCount' => 1, |
199
|
|
|
'email' => '[email protected]', |
200
|
|
|
'shippingAddress' => $this->getAddress('US') |
201
|
|
|
] |
202
|
|
|
), |
203
|
|
|
'databaseEntity' => $this->getEntity( |
204
|
|
|
[ |
205
|
|
|
'itemsCount' => 1, |
206
|
|
|
'email' => '[email protected]', |
207
|
|
|
'shippingAddress' => $this->getAddress('US') |
208
|
|
|
] |
209
|
|
|
) |
210
|
|
|
], |
211
|
|
|
'drop without country' => [ |
212
|
|
|
'expected' => $this->getEntity( |
213
|
|
|
[ |
214
|
|
|
'itemsCount' => 1, |
215
|
|
|
'email' => '[email protected]', |
216
|
|
|
'billingAddress' => null |
217
|
|
|
] |
218
|
|
|
), |
219
|
|
|
'entity' => $this->getEntity( |
220
|
|
|
[ |
221
|
|
|
'itemsCount' => 1, |
222
|
|
|
'email' => '[email protected]', |
223
|
|
|
'billingAddress' => $this->getAddress() |
224
|
|
|
] |
225
|
|
|
) |
226
|
|
|
] |
227
|
|
|
]; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Test setting removed field on removed cart items |
232
|
|
|
*/ |
233
|
|
|
public function testUpdateRemovedCartItems() |
234
|
|
|
{ |
235
|
|
|
$channel = new Channel(); |
236
|
|
|
|
237
|
|
|
$cartItem1 = new CartItem(); |
238
|
|
|
$cartItem1->setName('Cart Item 1'); |
239
|
|
|
|
240
|
|
|
$cartItem2 = new CartItem(); |
241
|
|
|
$cartItem2->setName('Cart Item 2'); |
242
|
|
|
|
243
|
|
|
$cartItem3 = new CartItem(); |
244
|
|
|
$cartItem3->setName('Cart Item 3'); |
245
|
|
|
|
246
|
|
|
$existingCartItems = new ArrayCollection(); |
247
|
|
|
$existingCartItems->add($cartItem1); |
248
|
|
|
$existingCartItems->add($cartItem2); |
249
|
|
|
|
250
|
|
|
$existingCart = new Cart(); |
251
|
|
|
$existingCart->setCartItems($existingCartItems); |
252
|
|
|
$existingCart->setChannel($channel); |
253
|
|
|
$existingCart->setItemsQty(2); |
254
|
|
|
|
255
|
|
|
$newCartItems = new ArrayCollection(); |
256
|
|
|
$newCartItems->add($cartItem2); |
257
|
|
|
$newCartItems->add($cartItem3); |
258
|
|
|
|
259
|
|
|
$newCart = new Cart(); |
260
|
|
|
$newCart->setCartItems($newCartItems); |
261
|
|
|
$newCart->setChannel($channel); |
262
|
|
|
$newCart->setItemsQty(2); |
263
|
|
|
|
264
|
|
|
$this->databaseHelper->expects($this->once()) |
|
|
|
|
265
|
|
|
->method('findOneByIdentity') |
266
|
|
|
->will($this->returnValue($existingCart)); |
267
|
|
|
|
268
|
|
|
$this->databaseHelper->expects($this->once()) |
269
|
|
|
->method('getEntityReference') |
270
|
|
|
->will($this->returnValue($channel)); |
271
|
|
|
|
272
|
|
|
$this->databaseHelper->expects($this->once()) |
273
|
|
|
->method('getIdentifier') |
274
|
|
|
->will($this->returnValue('identifier')); |
275
|
|
|
|
276
|
|
|
$this->databaseHelper->expects($this->once()) |
277
|
|
|
->method('find') |
278
|
|
|
->with('OroCRM\Bundle\MagentoBundle\Entity\Cart', 'identifier') |
279
|
|
|
->will($this->returnValue($newCart)); |
280
|
|
|
|
281
|
|
|
$strategy = $this->getStrategy(); |
282
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ContextInterface $context */ |
283
|
|
|
$context = $this->getMock('Oro\Bundle\ImportExportBundle\Context\ContextInterface'); |
284
|
|
|
$strategy->setImportExportContext($context); |
285
|
|
|
$strategy->setEntityName('OroCRM\Bundle\MagentoBundle\Entity\Cart'); |
286
|
|
|
$execution = $this->getMock('Akeneo\Bundle\BatchBundle\Item\ExecutionContext'); |
287
|
|
|
$this->jobExecution->expects($this->any())->method('getExecutionContext') |
|
|
|
|
288
|
|
|
->will($this->returnValue($execution)); |
289
|
|
|
$strategy->setStepExecution($this->stepExecution); |
290
|
|
|
|
291
|
|
|
$strategy->process($existingCart); |
292
|
|
|
|
293
|
|
|
$this->assertTrue($cartItem1->isRemoved()); |
294
|
|
|
$this->assertFalse($cartItem2->isRemoved()); |
295
|
|
|
$this->assertFalse($cartItem3->isRemoved()); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param int $originId |
300
|
|
|
* |
301
|
|
|
* @return CartItem |
302
|
|
|
*/ |
303
|
|
|
protected function getCartItem($originId) |
304
|
|
|
{ |
305
|
|
|
$cartItem = new CartItem(); |
306
|
|
|
$cartItem->setOriginId($originId); |
307
|
|
|
|
308
|
|
|
return $cartItem; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param string $countryCode |
313
|
|
|
* @return CartAddress |
314
|
|
|
*/ |
315
|
|
|
protected function getAddress($countryCode = null) |
316
|
|
|
{ |
317
|
|
|
$address = new CartAddress(); |
318
|
|
|
|
319
|
|
|
if ($countryCode) { |
|
|
|
|
320
|
|
|
$address->setCountry(new Country($countryCode)); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return $address; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param string $email |
328
|
|
|
* @return Customer |
329
|
|
|
*/ |
330
|
|
|
protected function getCustomer($email = null) |
331
|
|
|
{ |
332
|
|
|
$customer = new Customer(); |
333
|
|
|
if ($email) { |
|
|
|
|
334
|
|
|
$customer->setEmail($email); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $customer; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param array $properties |
342
|
|
|
* |
343
|
|
|
* @return Cart |
344
|
|
|
*/ |
345
|
|
|
protected function getEntity(array $properties = []) |
346
|
|
|
{ |
347
|
|
|
$cart = new Cart(); |
348
|
|
|
|
349
|
|
|
$channel = new Channel(); |
350
|
|
|
$cart->setChannel($channel); |
351
|
|
|
|
352
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
353
|
|
|
|
354
|
|
|
foreach ($properties as $property => $value) { |
355
|
|
|
if ($value instanceof ArrayCollection) { |
356
|
|
|
foreach ($value as $item) { |
357
|
|
|
try { |
358
|
|
|
$propertyAccessor->setValue($item, 'cart', $cart); |
359
|
|
|
} catch (NoSuchPropertyException $e) { |
|
|
|
|
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
} elseif (is_object($value)) { |
363
|
|
|
try { |
364
|
|
|
$propertyAccessor->setValue($value, 'cart', $cart); |
365
|
|
|
} catch (NoSuchPropertyException $e) { |
|
|
|
|
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
$propertyAccessor->setValue($cart, $property, $value); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
return $cart; |
|
|
|
|
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: