Passed
Push — master ( b35ffc...19a63c )
by Aimeos
04:50
created

BaseTest::testMagicMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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