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