Passed
Push — master ( a3e004...b231c1 )
by Aimeos
07:52 queued 02:33
created

BaseTest::testSetCoupons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
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() : void
23
	{
24
		$context = \TestHelperMShop::context();
25
26
		$priceManager = \Aimeos\MShop\Price\Manager\Factory::create( $context );
27
		$locale = \Aimeos\MShop\Locale\Manager\Factory::create( $context )->create();
28
29
		$this->object = new \Aimeos\MShop\Order\Item\Base\Standard( $priceManager->create(), $locale, [] );
30
31
32
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::create( $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->create();
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->create();
48
		$prod1->setProductCode( 'prod1' );
49
		$prod1->setPrice( $price );
50
51
		$price = $priceManager->create();
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->create();
59
		$prod2->setProductCode( 'prod2' );
60
		$prod2->setPrice( $price );
61
62
63
		$this->products = [$prod1, $prod2];
64
		$this->coupons = map( ['OPQR' => [$prod1]] );
65
66
		$this->addresses = array(
67
			'payment' => [0 => $orderAddressManager->create()->setType( 'payment' )->setId( null )],
68
			'delivery' => [0 => $orderAddressManager->create()->setType( 'delivery' )->setId( null )],
69
		);
70
71
		$this->services = array(
72
			'payment' => [0 => $orderServiceManager->create()->setType( 'payment' )->setCode( 'testpay' )->setId( null )],
73
			'delivery' => [1 => $orderServiceManager->create()->setType( 'delivery' )->setCode( 'testship' )->setId( null )],
74
		);
75
	}
76
77
78
	protected function tearDown() : void
79
	{
80
		unset( $this->object, $this->products, $this->addresses, $this->services, $this->coupons );
81
	}
82
83
84
	public function testArrayMethods()
85
	{
86
		$this->assertFalse( isset( $this->object['test'] ) );
87
		$this->assertEquals( null, $this->object['test'] );
88
89
		$this->object['test'] = 'value';
90
91
		$this->assertTrue( isset( $this->object['test'] ) );
92
		$this->assertEquals( 'value', $this->object['test'] );
93
94
		$this->expectException( \LogicException::class );
95
		unset( $this->object['test'] );
96
	}
97
98
99
	public function testMagicMethods()
100
	{
101
		$this->assertFalse( isset( $this->object->test ) );
102
		$this->assertEquals( null, $this->object->test );
103
104
		$this->object->test = 'value';
105
106
		$this->assertTrue( isset( $this->object->test ) );
107
		$this->assertEquals( 'value', $this->object->test );
108
	}
109
110
111
	public function testGetSet()
112
	{
113
		$this->assertEquals( false, $this->object->get( 'test', false ) );
114
115
		$this->object->set( 'test', 'value' );
116
117
		$this->assertEquals( 'value', $this->object->get( 'test', false ) );
118
	}
119
120
121
	public function testIsModified()
122
	{
123
		$this->assertFalse( $this->object->isModified() );
124
	}
125
126
127
	public function testAddProductAppend()
128
	{
129
		$this->object->setProducts( $this->products );
130
131
		$products = $this->object->getProducts();
132
		$product = $this->createProduct( 'prodid3' );
133
		$products[] = $product;
134
135
		$result = $this->object->addProduct( $product );
136
137
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
138
		$this->assertEquals( $products, $this->object->getProducts() );
139
		$this->assertSame( $product, $this->object->getProduct( 2 ) );
140
		$this->assertTrue( $this->object->isModified() );
141
	}
142
143
144
	public function testAddProductInsert()
145
	{
146
		$this->object->setProducts( $this->products );
147
148
		$products = $this->object->getProducts();
149
		$products[1] = $this->createProduct( 'prodid3' );
150
151
		$result = $this->object->addProduct( $products[1], 1 );
152
153
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
154
		$this->assertSame( $products[1], $this->object->getProduct( 1 ) );
155
		$this->assertEquals( $products, $this->object->getProducts() );
156
		$this->assertTrue( $this->object->isModified() );
157
	}
158
159
160
	public function testAddProductInsertEnd()
161
	{
162
		$this->object->setProducts( $this->products );
163
164
		$products = $this->object->getProducts();
165
		$product = $this->createProduct( 'prodid3' );
166
		$products[] = $product;
167
168
		$result = $this->object->addProduct( $product, 2 );
169
170
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
171
		$this->assertEquals( $products, $this->object->getProducts() );
172
		$this->assertSame( $product, $this->object->getProduct( 2 ) );
173
		$this->assertTrue( $this->object->isModified() );
174
	}
175
176
177
	public function testAddProductSame()
178
	{
179
		$product = $this->createProduct( 'prodid3' )->setQuantity( 5 );
180
181
		$this->object->addProduct( $product );
182
		$this->object->addProduct( $product );
183
184
		$this->assertEquals( 10, $this->object->getProduct( 0 )->getQuantity() );
185
		$this->assertEquals( [0 => $product], $this->object->getProducts()->toArray() );
186
		$this->assertTrue( $this->object->isModified() );
187
	}
188
189
190
	public function testAddProductStablePosition()
191
	{
192
		$this->object->setProducts( $this->products );
193
194
		$product = $this->createProduct( 'prodid3' )->setQuantity( 5 );
195
		$this->object->addProduct( $product );
196
197
		$testProduct = $this->object->getProduct( 1 );
198
		$this->object->deleteProduct( 0 );
199
		$this->object->deleteProduct( 1 );
200
		$result = $this->object->addProduct( $testProduct, 1 );
201
202
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
203
		$this->assertEquals( [1 => $testProduct, 2 => $product], $this->object->getProducts()->toArray() );
204
	}
205
206
207
	public function testDeleteProduct()
208
	{
209
		$this->object->addProduct( $this->products[0] );
210
		$result = $this->object->deleteProduct( 0 );
211
212
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
213
		$this->assertSame( [], $this->object->getProducts()->toArray() );
214
		$this->assertTrue( $this->object->isModified() );
215
	}
216
217
218
	public function testGetProducts()
219
	{
220
		$this->object->setProducts( $this->products );
221
222
		$this->assertSame( $this->products, $this->object->getProducts()->toArray() );
223
		$this->assertSame( $this->products[1], $this->object->getProduct( 1 ) );
224
	}
225
226
227
	public function testSetProducts()
228
	{
229
		$result = $this->object->setProducts( $this->products );
230
231
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
232
		$this->assertSame( $this->products, $this->object->getProducts()->toArray() );
233
		$this->assertTrue( $this->object->isModified() );
234
	}
235
236
237
	public function testAddAddress()
238
	{
239
		$type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT;
240
		$result = $this->object->addAddress( $this->addresses[$type][0], $type );
241
242
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
243
		$this->assertEquals( $this->addresses[$type], $this->object->getAddress( $type ) );
244
		$this->assertTrue( $this->object->isModified() );
245
	}
246
247
248
	public function testAddAddressMultiple()
249
	{
250
		$this->object->addAddress( $this->addresses['payment'][0], 'payment' );
251
		$result = $this->object->addAddress( $this->addresses['payment'][0], 'payment' );
252
253
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
254
		$this->assertEquals( 2, count( $this->object->getAddress( 'payment' ) ) );
255
		$this->assertTrue( $this->object->isModified() );
256
	}
257
258
259
	public function testAddAddressPosition()
260
	{
261
		$type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT;
262
263
		$this->object->addAddress( $this->addresses[$type][0], $type );
264
		$result = $this->object->addAddress( $this->addresses[$type][0], $type, 0 );
265
266
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
267
		$this->assertEquals( $this->addresses[$type], $this->object->getAddress( $type ) );
268
		$this->assertTrue( $this->object->isModified() );
269
	}
270
271
272
	public function testDeleteAddress()
273
	{
274
		$type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT;
275
		$this->object->setAddresses( $this->addresses );
276
		$result = $this->object->deleteAddress( $type );
277
278
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
279
		$this->assertEquals( [], $this->object->getAddress( $type ) );
280
		$this->assertTrue( $this->object->isModified() );
281
	}
282
283
284
	public function testDeleteAddressPosition()
285
	{
286
		$type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT;
287
		$this->object->setAddresses( $this->addresses );
288
289
		$result = $this->object->deleteAddress( $type, 0 );
290
291
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
292
		$this->assertEquals( [], $this->object->getAddress( $type ) );
293
		$this->assertTrue( $this->object->isModified() );
294
	}
295
296
297
	public function testDeleteAddressPositionInvalid()
298
	{
299
		$type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT;
300
		$this->object->setAddresses( $this->addresses );
301
302
		$result = $this->object->deleteAddress( $type, 1 );
303
304
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
305
		$this->assertEquals( $this->addresses[$type], $this->object->getAddress( $type ) );
306
	}
307
308
309
	public function testGetAddress()
310
	{
311
		$this->object->setAddresses( $this->addresses );
312
		$type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT;
313
314
		$this->assertEquals( $this->addresses[$type], $this->object->getAddress( $type ) );
315
	}
316
317
318
	public function testGetAddressSingle()
319
	{
320
		$this->object->setAddresses( $this->addresses );
321
		$type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT;
322
323
		$this->assertEquals( $this->addresses[$type][0], $this->object->getAddress( $type, 0 ) );
324
	}
325
326
327
	public function testGetAddressException()
328
	{
329
		$this->expectException( \Aimeos\MShop\Order\Exception::class );
330
		$this->object->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, 0 );
331
	}
332
333
334
	public function testSetAddresses()
335
	{
336
		$result = $this->object->setAddresses( $this->addresses );
337
338
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
339
		$this->assertEquals( $this->addresses, $this->object->getAddresses()->toArray() );
340
		$this->assertTrue( $this->object->isModified() );
341
	}
342
343
344
	public function testAddService()
345
	{
346
		$type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT;
347
		$result = $this->object->addService( $this->services[$type][0], $type );
348
349
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
350
		$this->assertEquals( 1, count( $this->object->getService( $type ) ) );
351
		$this->assertTrue( $this->object->isModified() );
352
	}
353
354
355
	public function testAddServicePosition()
356
	{
357
		$type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT;
358
359
		$this->object->addService( $this->services[$type][0], $type );
360
		$result = $this->object->addService( $this->services[$type][0], $type, 0 );
361
362
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
363
		$this->assertEquals( 1, count( $this->object->getService( $type ) ) );
364
		$this->assertTrue( $this->object->isModified() );
365
	}
366
367
368
	public function testDeleteService()
369
	{
370
		$type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT;
371
		$this->object->setServices( $this->services );
372
373
		$result = $this->object->deleteService( $type );
374
375
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
376
		$this->assertEquals( [], $this->object->getService( $type ) );
377
		$this->assertTrue( $this->object->isModified() );
378
	}
379
380
381
	public function testDeleteServicePosition()
382
	{
383
		$type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT;
384
		$this->object->setServices( $this->services );
385
386
		$result = $this->object->deleteService( $type, 0 );
387
388
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
389
		$this->assertEquals( [], $this->object->getService( $type ) );
390
		$this->assertTrue( $this->object->isModified() );
391
	}
392
393
394
	public function testDeleteServicePositionInvalid()
395
	{
396
		$type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT;
397
		$this->object->setServices( $this->services );
398
399
		$result = $this->object->deleteService( $type, 1 );
400
401
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
402
		$this->assertEquals( $this->services[$type], $this->object->getService( $type ) );
403
	}
404
405
406
	public function testGetService()
407
	{
408
		$this->object->setServices( $this->services );
409
410
		$payments = $this->object->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT );
411
		$deliveries = $this->object->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY );
412
413
		$this->assertEquals( 2, count( $this->object->getServices() ) );
414
		$this->assertEquals( 1, count( $payments ) );
415
		$this->assertEquals( 1, count( $deliveries ) );
416
417
		$this->assertEquals( $this->services['payment'], $payments );
418
		$this->assertEquals( $this->services['delivery'], $deliveries );
419
	}
420
421
422
	public function testGetServiceSingle()
423
	{
424
		$this->object->setServices( $this->services );
425
426
		$service = $this->object->getService( 'payment', 0 );
427
		$this->assertEquals( 'testpay', $service->getCode() );
428
	}
429
430
431
	public function testGetServiceException()
432
	{
433
		$this->expectException( \Aimeos\MShop\Order\Exception::class );
434
		$this->object->getService( 'payment', 100 );
435
	}
436
437
438
	public function testSetServices()
439
	{
440
		$result = $this->object->setServices( $this->services );
441
442
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
443
		$this->assertEquals( $this->services, $this->object->getServices()->toArray() );
444
		$this->assertTrue( $this->object->isModified() );
445
	}
446
447
448
	public function testAddCoupon()
449
	{
450
		$result = $this->object->addCoupon( 'OPQR' );
451
452
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
453
		$this->assertEquals( ['OPQR' => []], $this->object->getCoupons()->toArray() );
454
		$this->assertTrue( $this->object->isModified() );
455
	}
456
457
458
	public function testDeleteCoupon()
459
	{
460
		$this->object->setCoupons( $this->coupons );
461
		$result = $this->object->deleteCoupon( 'OPQR' );
462
463
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
464
		$this->assertEquals( [], $this->object->getCoupons()->toArray() );
465
		$this->assertTrue( $this->object->isModified() );
466
	}
467
468
469
	public function testGetCoupons()
470
	{
471
		$this->object->setCoupons( $this->coupons );
472
		$this->assertEquals( $this->coupons, $this->object->getCoupons() );
473
	}
474
475
476
	public function testSetCoupon()
477
	{
478
		$result = $this->object->setCoupon( 'OPQR', $this->coupons['OPQR'] );
479
480
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
481
		$this->assertEquals( map( ['OPQR' => $this->coupons['OPQR']] ), $this->object->getCoupons() );
482
		$this->assertTrue( $this->object->isModified() );
483
	}
484
485
486
	public function testSetCoupons()
487
	{
488
		$result = $this->object->setCoupons( $this->coupons );
489
490
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
491
		$this->assertEquals( $this->coupons, $this->object->getCoupons() );
492
		$this->assertTrue( $this->object->isModified() );
493
	}
494
495
496
	public function testGetTaxes()
497
	{
498
		$this->object->setProducts( $this->products );
499
		$this->object->setServices( $this->services );
500
501
		$result = $this->object->getTaxes();
502
503
		$this->assertArrayHasKey( 'tax', $result );
504
		$this->assertArrayHasKey( '0.00', $result['tax'] );
505
		$this->assertArrayHasKey( '0.50', $result['tax'] );
506
		$this->assertEquals( '0.0000', $result['tax']['0.00']->getTaxValue() );
507
		$this->assertEquals( '0.1095', $result['tax']['0.50']->getTaxValue() );
508
	}
509
510
511
	public function testGetCosts()
512
	{
513
		$this->object->setProducts( $this->products );
514
		$this->object->setServices( $this->services );
515
516
		$this->assertEquals( '3.11', round( $this->object->getCosts(), 2 ) );
517
		$this->assertEquals( '0.00', $this->object->getCosts( 'payment' ) );
518
	}
519
520
521
	public function testCheck()
522
	{
523
		foreach( $this->products as $product ) {
524
			$this->object->addProduct( $product );
525
		}
526
527
		foreach( $this->addresses as $type => $addresses )
528
		{
529
			foreach( $addresses as $address ) {
530
				$this->object->addAddress( $address, $type );
531
			}
532
		}
533
534
		foreach( $this->services as $type => $services )
535
		{
536
			foreach( $services as $service ) {
537
				$this->object->addService( $service, $type );
538
			}
539
		}
540
541
		$result = $this->object->check( ['order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service'] );
542
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result );
543
	}
544
545
546
	public function testCheckAllFailure()
547
	{
548
		$this->expectException( \Aimeos\MShop\Order\Exception::class );
549
		$this->object->check( ['order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service'] );
550
	}
551
552
553
	public function testCheckProductsFailure()
554
	{
555
		$this->expectException( \Aimeos\MShop\Order\Exception::class );
556
		$this->object->check( ['order/base/product'] );
557
	}
558
559
560
	/**
561
	 * @param string $code
562
	 */
563
	protected function createProduct( $code )
564
	{
565
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::create( \TestHelperMShop::context() );
566
		$orderProductManager = $orderManager->getSubManager( 'base' )->getSubManager( 'product' );
567
		$product = $orderProductManager->create();
568
569
		$price = \Aimeos\MShop\Price\Manager\Factory::create( \TestHelperMShop::context() )->create();
570
		$price->setValue( '2.99' );
571
572
		$product->setPrice( $price );
573
		$product->setProductCode( $code );
574
575
		return $product;
576
	}
577
}
578