Passed
Push — master ( 64e7d7...5c5569 )
by Aimeos
04:35
created

BaseTest::testArrayMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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