1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2011 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2016 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace Aimeos\MShop\Order\Item\Base; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class BaseTest extends \PHPUnit\Framework\TestCase |
14
|
|
|
{ |
15
|
|
|
private $object; |
16
|
|
|
private $products; |
17
|
|
|
private $addresses; |
18
|
|
|
private $services; |
19
|
|
|
private $coupons; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
protected function setUp() |
23
|
|
|
{ |
24
|
|
|
$context = \TestHelperMShop::getContext(); |
25
|
|
|
|
26
|
|
|
$priceManager = \Aimeos\MShop\Price\Manager\Factory::createManager( $context ); |
27
|
|
|
$locale = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context )->createItem(); |
28
|
|
|
|
29
|
|
|
$this->object = new \Aimeos\MShop\Order\Item\Base\Standard( $priceManager->createItem(), $locale, [] ); |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( $context ); |
33
|
|
|
|
34
|
|
|
$orderBaseManager = $orderManager->getSubManager( 'base' ); |
35
|
|
|
$orderAddressManager = $orderBaseManager->getSubManager( 'address' ); |
36
|
|
|
$orderProductManager = $orderBaseManager->getSubManager( 'product' ); |
37
|
|
|
$orderServiceManager = $orderBaseManager->getSubManager( 'service' ); |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
$price = $priceManager->createItem(); |
41
|
|
|
$price->setRebate( '3.01' ); |
42
|
|
|
$price->setValue( '43.12' ); |
43
|
|
|
$price->setCosts( '1.11' ); |
44
|
|
|
$price->setTaxRate( '0.00' ); |
45
|
|
|
$price->setCurrencyId( 'EUR' ); |
46
|
|
|
|
47
|
|
|
$prod1 = $orderProductManager->createItem(); |
48
|
|
|
$prod1->setProductCode( 'prod1' ); |
49
|
|
|
$prod1->setPrice( $price ); |
50
|
|
|
|
51
|
|
|
$price = $priceManager->createItem(); |
52
|
|
|
$price->setRebate( '4.00' ); |
53
|
|
|
$price->setValue( '20.00' ); |
54
|
|
|
$price->setCosts( '2.00' ); |
55
|
|
|
$price->setTaxRate( '0.50' ); |
56
|
|
|
$price->setCurrencyId( 'EUR' ); |
57
|
|
|
|
58
|
|
|
$prod2 = $orderProductManager->createItem(); |
59
|
|
|
$prod2->setProductCode( 'prod2' ); |
60
|
|
|
$prod2->setPrice( $price ); |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
$this->products = array( $prod1, $prod2 ); |
64
|
|
|
$this->coupons = array( 'OPQR' => array( $prod1 ) ); |
65
|
|
|
|
66
|
|
|
$this->addresses = array( |
67
|
|
|
\Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT => $orderAddressManager->createItem(), |
68
|
|
|
\Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY => $orderAddressManager->createItem(), |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$this->services = array( |
72
|
|
|
'payment' => $orderServiceManager->createItem(), |
73
|
|
|
'delivery' => $orderServiceManager->createItem(), |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
protected function tearDown() |
79
|
|
|
{ |
80
|
|
|
unset( $this->object, $this->products, $this->addresses, $this->services, $this->coupons ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
public function testIsModified() |
85
|
|
|
{ |
86
|
|
|
$this->assertFalse( $this->object->isModified() ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
public function testGetProducts() |
91
|
|
|
{ |
92
|
|
|
foreach( $this->products as $product ) { |
93
|
|
|
$this->object->addProduct( $product ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->assertSame( $this->products, $this->object->getProducts() ); |
97
|
|
|
$this->assertSame( $this->products[1], $this->object->getProduct( 1 ) ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
public function testAddProductAppend() |
102
|
|
|
{ |
103
|
|
|
foreach( $this->products as $product ) { |
104
|
|
|
$this->object->addProduct( $product ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$products = $this->object->getProducts(); |
108
|
|
|
$product = $this->createProduct( 'prodid3' ); |
109
|
|
|
$products[] = $product; |
110
|
|
|
|
111
|
|
|
$this->object->addProduct( $product ); |
112
|
|
|
|
113
|
|
|
$this->assertSame( $products, $this->object->getProducts() ); |
114
|
|
|
$this->assertSame( $product, $this->object->getProduct( 2 ) ); |
115
|
|
|
$this->assertTrue( $this->object->isModified() ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
public function testAddProductInsert() |
120
|
|
|
{ |
121
|
|
|
foreach( $this->products as $product ) { |
122
|
|
|
$this->object->addProduct( $product ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$products = $this->object->getProducts(); |
126
|
|
|
$product = $this->createProduct( 'prodid3' ); |
127
|
|
|
array_splice( $products, 1, 0, array( $product ) ); |
128
|
|
|
|
129
|
|
|
$this->object->addProduct( $product, 1 ); |
130
|
|
|
|
131
|
|
|
$this->assertEquals( $products, $this->object->getProducts() ); |
132
|
|
|
$this->assertSame( $product, $this->object->getProduct( 1 ) ); |
133
|
|
|
$this->assertTrue( $this->object->isModified() ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
public function testAddProductInsertEnd() |
138
|
|
|
{ |
139
|
|
|
foreach( $this->products as $product ) { |
140
|
|
|
$this->object->addProduct( $product ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$products = $this->object->getProducts(); |
144
|
|
|
$product = $this->createProduct( 'prodid3' ); |
145
|
|
|
array_splice( $products, 2, 0, array( $product ) ); |
146
|
|
|
|
147
|
|
|
$this->object->addProduct( $product, 2 ); |
148
|
|
|
|
149
|
|
|
$this->assertEquals( $products, $this->object->getProducts() ); |
150
|
|
|
$this->assertSame( $product, $this->object->getProduct( 2 ) ); |
151
|
|
|
$this->assertTrue( $this->object->isModified() ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
public function testAddProductSame() |
156
|
|
|
{ |
157
|
|
|
foreach( $this->products as $product ) { |
158
|
|
|
$this->object->addProduct( $product ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$products = $this->object->getProducts(); |
162
|
|
|
$product = $this->createProduct( 'prodid3' ); |
163
|
|
|
$product->setQuantity( 5 ); |
164
|
|
|
$products[] = $product; |
165
|
|
|
|
166
|
|
|
$this->object->addProduct( $product ); |
167
|
|
|
$this->object->addProduct( $product ); |
168
|
|
|
|
169
|
|
|
$this->assertEquals( $products, $this->object->getProducts() ); |
170
|
|
|
$this->assertEquals( 10, $this->object->getProduct( 2 )->getQuantity() ); |
171
|
|
|
$this->assertTrue( $this->object->isModified() ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
public function testAddProductStablePosition() |
176
|
|
|
{ |
177
|
|
|
foreach( $this->products as $product ) { |
178
|
|
|
$this->object->addProduct( $product ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$product = $this->createProduct( 'prodid3' ); |
182
|
|
|
$product->setQuantity( 5 ); |
183
|
|
|
$this->object->addProduct( $product ); |
184
|
|
|
|
185
|
|
|
$this->object->deleteProduct( 0 ); |
186
|
|
|
$testProduct = $this->object->getProduct( 1 ); |
187
|
|
|
|
188
|
|
|
$this->object->deleteProduct( 1 ); |
189
|
|
|
$this->object->addProduct( $testProduct, 1 ); |
190
|
|
|
|
191
|
|
|
$expected = array( 1 => $testProduct, 2 => $product ); |
192
|
|
|
$this->assertEquals( $expected, $this->object->getProducts() ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
public function testEditProduct() |
197
|
|
|
{ |
198
|
|
|
$product = $this->createProduct( 'prodid3' ); |
199
|
|
|
$product->setQuantity( 5 ); |
200
|
|
|
$products[] = $product; |
|
|
|
|
201
|
|
|
|
202
|
|
|
$this->object->addProduct( $product ); |
203
|
|
|
$product->setQuantity( 10 ); |
204
|
|
|
$this->object->editProduct( $product, 0 ); |
205
|
|
|
|
206
|
|
|
$this->assertEquals( $products, $this->object->getProducts() ); |
207
|
|
|
$this->assertEquals( 10, $this->object->getProduct( 0 )->getQuantity() ); |
208
|
|
|
$this->assertTrue( $this->object->isModified() ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
public function testEditProductSame() |
213
|
|
|
{ |
214
|
|
|
foreach( $this->products as $product ) { |
215
|
|
|
$this->object->addProduct( $product ); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$product = clone $this->object->getProduct( 0 ); |
219
|
|
|
$product->setQuantity( 10 ); |
220
|
|
|
$this->object->editProduct( $product, 1 ); |
221
|
|
|
|
222
|
|
|
$this->assertEquals( 2, count( $this->object->getProducts() ) ); |
223
|
|
|
$this->assertEquals( 10, $this->object->getProduct( 0 )->getQuantity() ); |
224
|
|
|
$this->assertEquals( 1, $this->object->getProduct( 1 )->getQuantity() ); |
225
|
|
|
$this->assertTrue( $this->object->isModified() ); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
public function testDeleteProduct() |
230
|
|
|
{ |
231
|
|
|
foreach( $this->products as $product ) { |
232
|
|
|
$this->object->addProduct( $product ); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
unset( $this->products[1] ); |
236
|
|
|
$this->object->deleteProduct( 1 ); |
237
|
|
|
$this->assertSame( $this->products, $this->object->getProducts() ); |
238
|
|
|
$this->assertTrue( $this->object->isModified() ); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
public function testGetAddress() |
243
|
|
|
{ |
244
|
|
|
foreach( $this->addresses as $type => $address ) |
245
|
|
|
{ |
246
|
|
|
$address->setId( null ); |
247
|
|
|
$address->setType( $type ); |
248
|
|
|
$this->object->setAddress( $address, $type ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$this->assertEquals( $this->addresses, $this->object->getAddresses() ); |
252
|
|
|
|
253
|
|
|
$address = $this->object->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
254
|
|
|
$this->assertEquals( $this->addresses[\Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT], $address ); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
public function testSetAddress() |
259
|
|
|
{ |
260
|
|
|
foreach( $this->addresses as $type => $address ) { |
261
|
|
|
$this->object->setAddress( $address, $type ); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( \TestHelperMShop::getContext() ); |
265
|
|
|
$orderAddressManager = $orderManager->getSubManager( 'base' )->getSubManager( 'address' ); |
266
|
|
|
$address = $orderAddressManager->createItem(); |
267
|
|
|
|
268
|
|
|
$this->object->setAddress( $address, \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
269
|
|
|
$item = $this->object->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
270
|
|
|
|
271
|
|
|
$this->assertEquals( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, $item->getType() ); |
272
|
|
|
$this->assertTrue( $item->isModified() ); |
273
|
|
|
$this->assertNull( $item->getId() ); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
|
277
|
|
|
public function testDeleteAddress() |
278
|
|
|
{ |
279
|
|
|
foreach( $this->addresses as $type => $address ) { |
280
|
|
|
$this->object->setAddress( $address, $type ); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$this->object->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
284
|
|
|
$this->object->deleteAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
285
|
|
|
$this->assertTrue( $this->object->isModified() ); |
286
|
|
|
|
287
|
|
|
$this->setExpectedException( '\\Aimeos\\MShop\\Order\\Exception' ); |
288
|
|
|
$this->object->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
|
292
|
|
|
public function testGetService() |
293
|
|
|
{ |
294
|
|
|
foreach( $this->services as $type => $service ) |
295
|
|
|
{ |
296
|
|
|
$service->setId( null ); |
297
|
|
|
$service->setType( $type ); |
298
|
|
|
$this->object->setService( $service, $type ); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
$this->assertEquals( $this->services, $this->object->getServices() ); |
302
|
|
|
|
303
|
|
|
$type = 'payment'; |
304
|
|
|
$this->assertEquals( $this->services[$type], $this->object->getService( $type ) ); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
|
308
|
|
|
public function testSetService() |
309
|
|
|
{ |
310
|
|
|
foreach( $this->services as $type => $service ) { |
311
|
|
|
$this->object->setService( $service, $type ); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
$type = 'delivery'; |
315
|
|
|
$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( \TestHelperMShop::getContext() ); |
316
|
|
|
$orderServiceManager = $orderManager->getSubManager( 'base' )->getSubManager( 'service' ); |
317
|
|
|
$service = $orderServiceManager->createItem(); |
318
|
|
|
|
319
|
|
|
$this->object->setService( $service, $type ); |
320
|
|
|
$item = $this->object->getService( $type ); |
321
|
|
|
|
322
|
|
|
$this->assertEquals( $type, $item->getType() ); |
323
|
|
|
$this->assertTrue( $item->isModified() ); |
324
|
|
|
$this->assertNull( $item->getId() ); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
|
328
|
|
|
public function testDeleteService() |
329
|
|
|
{ |
330
|
|
|
foreach( $this->services as $type => $service ) { |
331
|
|
|
$this->object->setService( $service, $type ); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
$this->object->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT ); |
335
|
|
|
$this->object->deleteService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT ); |
336
|
|
|
$this->assertTrue( $this->object->isModified() ); |
337
|
|
|
|
338
|
|
|
$this->setExpectedException( '\\Aimeos\\MShop\\Order\\Exception' ); |
339
|
|
|
$this->object->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT ); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
|
343
|
|
|
public function testCoupons() |
344
|
|
|
{ |
345
|
|
|
foreach( $this->coupons as $code => $products ) { |
346
|
|
|
$this->object->addCoupon( $code, $products ); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
foreach( $this->object->getCoupons() as $coupon => $products ) { |
350
|
|
|
$this->assertEquals( $this->coupons[$coupon], $products ); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
|
355
|
|
|
public function testDeleteCoupon() |
356
|
|
|
{ |
357
|
|
|
foreach( $this->coupons as $code => $products ) { |
358
|
|
|
$this->object->addCoupon( $code, $products ); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
$this->object->deleteCoupon( 'OPQR' ); |
362
|
|
|
|
363
|
|
|
foreach( $this->object->getCoupons() as $coupon => $products ) { |
364
|
|
|
$this->assertEquals( [], $products ); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
$this->object->deleteCoupon( 'OPQR', true ); |
368
|
|
|
$this->assertEquals( [], $this->object->getCoupons() ); |
369
|
|
|
|
370
|
|
|
$this->assertTrue( $this->object->isModified() ); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
|
374
|
|
|
public function testCheck() |
375
|
|
|
{ |
376
|
|
|
foreach( $this->products as $product ) { |
377
|
|
|
$this->object->addProduct( $product ); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
foreach( $this->addresses as $type => $address ) { |
381
|
|
|
$this->object->setAddress( $address, $type ); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
foreach( $this->services as $type => $service ) { |
385
|
|
|
$this->object->setService( $service, $type ); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
$this->object->check( \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL ); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
|
392
|
|
|
public function testCheckInvalid() |
393
|
|
|
{ |
394
|
|
|
$this->setExpectedException( '\\Aimeos\\MShop\\Order\\Exception' ); |
395
|
|
|
$this->object->check( -1 ); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
|
399
|
|
|
public function testCheckAllFailure() |
400
|
|
|
{ |
401
|
|
|
$this->setExpectedException( '\\Aimeos\\MShop\\Order\\Exception' ); |
402
|
|
|
$this->object->check( \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL ); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
|
406
|
|
|
public function testCheckProductsFailure() |
407
|
|
|
{ |
408
|
|
|
$this->setExpectedException( '\\Aimeos\\MShop\\Order\\Exception' ); |
409
|
|
|
$this->object->check( \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT ); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* @param string $code |
415
|
|
|
*/ |
416
|
|
|
protected function createProduct( $code ) |
417
|
|
|
{ |
418
|
|
|
$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( \TestHelperMShop::getContext() ); |
419
|
|
|
$orderProductManager = $orderManager->getSubManager( 'base' )->getSubManager( 'product' ); |
420
|
|
|
$product = $orderProductManager->createItem(); |
421
|
|
|
|
422
|
|
|
$price = \Aimeos\MShop\Price\Manager\Factory::createManager( \TestHelperMShop::getContext() )->createItem(); |
423
|
|
|
$price->setValue( '2.99' ); |
424
|
|
|
|
425
|
|
|
$product->setPrice( $price ); |
426
|
|
|
$product->setProductCode( $code ); |
427
|
|
|
|
428
|
|
|
return $product; |
429
|
|
|
} |
430
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.