StandardTest   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 517
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 39
eloc 249
c 3
b 0
f 0
dl 0
loc 517
rs 9.28

37 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 8 1
A testAddProductConfigAttributeException() 0 4 1
A testAddProductNegativeQuantityException() 0 4 1
A testAddProductAttributeNotAssigned() 0 7 1
A testAddProductHigherQuantities() 0 9 1
A testAdd() 0 6 1
A testLoad() 0 15 1
A testAddProductLowQuantityPriceException() 0 6 1
A testClear() 0 6 1
A testGet() 0 3 1
A setUp() 0 10 1
A testAddProductNoPriceException() 0 6 1
A testSave() 0 15 1
A testStoreLimit() 0 11 1
A testSetType() 0 3 1
A testAddProductCustomPrice() 0 11 1
A testAddProductFractionalQuantity() 0 8 1
A testAddDeleteProduct() 0 16 1
A testAddProductCustomAttribute() 0 11 1
A testAddProductCustomPriceException() 0 7 1
A testDeleteAddress() 0 9 1
A testDeleteServices() 0 10 1
A testAddAddress() 0 31 1
A testCheckLocale() 0 47 3
A testAddCouponInvalidCode() 0 4 1
A testUpdateProductFlagError() 0 9 1
A testAddCoupon() 0 9 1
A testAddServiceDelivery() 0 11 1
A testAddServicePayment() 0 11 1
A testAddProductAttributePrice() 0 9 1
A testDeleteServicePosition() 0 10 1
A testUpdateProduct() 0 13 1
A testAddCouponExceedCount() 0 7 1
A testDeleteCoupon() 0 10 1
A testDeleteProductFlagError() 0 9 1
A getAddress() 0 7 1
A testStore() 0 26 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2024
7
 */
8
9
namespace Aimeos\Controller\Frontend\Basket;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $context;
16
	private $testItem;
17
18
19
	protected function setUp() : void
20
	{
21
		\Aimeos\MShop::cache( true );
22
23
		$this->context = \TestHelper::context();
24
25
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
26
		$this->testItem = $manager->find( 'U:TESTP', ['attribute', 'media', 'price', 'product', 'text'] );
27
28
		$this->object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
29
	}
30
31
32
	protected function tearDown() : void
33
	{
34
		\Aimeos\MShop::cache( false );
35
36
		$this->object->clear();
37
		$this->context->session()->set( 'aimeos', [] );
38
39
		unset( $this->context, $this->object );
40
	}
41
42
43
	public function testAdd()
44
	{
45
		$result = $this->object->add( ['order.comment' => 'test'] );
46
47
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
48
		$this->assertEquals( 'test', $this->object->get()->getComment() );
49
	}
50
51
52
	public function testClear()
53
	{
54
		$this->object->addProduct( $this->testItem );
55
56
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $this->object->clear() );
57
		$this->assertEquals( 0, count( $this->object->get()->getProducts() ) );
58
	}
59
60
61
	public function testGet()
62
	{
63
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Iface::class, $this->object->get() );
64
	}
65
66
67
	public function testSave()
68
	{
69
		$stub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Standard::class )
70
			->setConstructorArgs( [$this->context] )
71
			->onlyMethods( ['setSession'] )
72
			->getMock();
73
74
		\Aimeos\MShop::inject( \Aimeos\MShop\Order\Manager\Standard::class, $stub );
75
76
		$stub->expects( $this->exactly( 2 ) )->method( 'setSession' );
77
78
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
79
		$object->addProduct( $this->testItem );
80
81
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $object->save() );
82
	}
83
84
85
	public function testSetType()
86
	{
87
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $this->object->setType( 'test' ) );
88
	}
89
90
91
	public function testLoad()
92
	{
93
		$stub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Standard::class )
94
			->setConstructorArgs( [$this->context] )
95
			->onlyMethods( ['get'] )
96
			->getMock();
97
98
		\Aimeos\MShop::inject( \Aimeos\MShop\Order\Manager\Standard::class, $stub );
99
100
		$stub->expects( $this->once() )->method( 'get' )
101
			->willReturn( $stub->create() );
102
103
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
104
105
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Iface::class, $object->load( -1 ) );
106
	}
107
108
109
	public function testStore()
110
	{
111
		$stub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Standard::class )
112
			->setConstructorArgs( [$this->context] )
113
			->onlyMethods( ['save'] )
114
			->getMock();
115
116
		\Aimeos\MShop::inject( \Aimeos\MShop\Order\Manager\Standard::class, $stub );
117
118
		$priceManager = \Aimeos\MShop::create( $this->context, 'price' );
119
120
		$basket = $this->getMockBuilder( \Aimeos\MShop\Order\Item\Standard::class )
121
			->setConstructorArgs( [$priceManager->create(), $this->context->locale()] )
122
			->onlyMethods( ['check'] )
123
			->getMock();
124
125
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
126
			->setConstructorArgs( [$this->context] )
127
			->onlyMethods( ['get'] )
128
			->getMock();
129
130
		$object->expects( $this->once() )->method( 'get' )->willReturn( $basket );
131
		$basket->expects( $this->once() )->method( 'check' )->willReturn( $basket );
132
		$stub->expects( $this->once() )->method( 'save' )->willReturn( $basket );
133
134
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Iface::class, $object->store() );
135
	}
136
137
138
	public function testStoreLimit()
139
	{
140
		$this->context->setEditor( 'core' );
141
		$config = $this->context->config();
142
		$config->set( 'controller/frontend/basket/limit-count', 0 );
143
		$config->set( 'controller/frontend/basket/limit-seconds', 86400 * 365 );
144
145
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
146
147
		$this->expectException( \Aimeos\Controller\Frontend\Basket\Exception::class );
148
		$object->store();
149
	}
150
151
152
	public function testAddDeleteProduct()
153
	{
154
		$basket = $this->object->get();
155
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
156
		$item = $manager->find( 'CNC', ['attribute', 'media', 'price', 'product', 'text', 'locale/site'] );
157
158
		$result1 = $this->object->addProduct( $item, 2 );
159
		$item2 = $this->object->get()->getProduct( 0 );
160
		$result2 = $this->object->deleteProduct( 0 );
161
162
		$this->assertEquals( 0, count( $basket->getProducts() ) );
163
		$this->assertEquals( 'CNC', $item2->getProductCode() );
164
		$this->assertEquals( 'default', $item2->getStockType() );
165
		$this->assertEquals( 'Unit test site', $item2->getVendor() );
166
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result1 );
167
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result2 );
168
	}
169
170
171
	public function testAddProductFractionalQuantity()
172
	{
173
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
174
		$item = $manager->find( 'CNC', ['attribute', 'media', 'price', 'product', 'text'] );
175
		$item->setConfig( ['quantity-step' => '0.1'] );
176
177
		$this->object->addProduct( $item, 2.31 );
178
		$this->assertEquals( 2.4, $this->object->get()->getProduct( 0 )->getQuantity() );
179
	}
180
181
182
	public function testAddProductCustomAttribute()
183
	{
184
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'custom', [], 'product', 'date' );
185
		$attrValues = [$attrItem->getId() => '2000-01-01'];
186
187
		$result = $this->object->addProduct( $this->testItem, 1, [], [], $attrValues );
188
		$basket = $this->object->get();
189
190
		$this->assertEquals( 1, count( $basket->getProducts() ) );
191
		$this->assertEquals( '2000-01-01', $basket->getProduct( 0 )->getAttribute( 'date', 'custom' ) );
192
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
193
	}
194
195
196
	public function testAddProductCustomPrice()
197
	{
198
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'custom', [], 'product', 'price' );
199
		$attrValues = [$attrItem->getId() => '0.01'];
200
201
		$result = $this->object->addProduct( $this->testItem, 1, [], [], $attrValues );
202
		$basket = $this->object->get();
203
204
		$this->assertEquals( 1, count( $basket->getProducts() ) );
205
		$this->assertEquals( '0.01', $basket->getProduct( 0 )->getPrice()->getValue() );
206
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
207
	}
208
209
210
	public function testAddProductCustomPriceException()
211
	{
212
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'custom', [], 'product', 'price' );
213
		$attrValues = [$attrItem->getId() => ','];
214
215
		$this->expectException( \Aimeos\Controller\Frontend\Basket\Exception::class );
216
		$this->object->addProduct( $this->testItem, 1, [], [], $attrValues );
217
	}
218
219
220
	public function testAddProductAttributePrice()
221
	{
222
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'xs', [], 'product', 'size' );
223
224
		$result = $this->object->addProduct( $this->testItem, 1, [], [$attrItem->getId() => 2] );
225
226
		$this->assertEquals( '43.90', $this->object->get()->getPrice()->getValue() );
227
		$this->assertEquals( '25.90', $this->object->get()->getProducts()->first()->getAttributeItems()->getPrice()->first() );
228
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
229
	}
230
231
232
	public function testAddProductAttributeNotAssigned()
233
	{
234
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( '30', [], 'product', 'width' );
235
		$ids = [$attrItem->getId()];
236
237
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
238
		$this->object->addProduct( $this->testItem, 1, [], $ids, $ids );
239
	}
240
241
242
	public function testAddProductNegativeQuantityException()
243
	{
244
		$this->expectException( '\\Aimeos\\MShop\\Order\\Exception' );
245
		$this->object->addProduct( $this->testItem, -1 );
246
	}
247
248
249
	public function testAddProductNoPriceException()
250
	{
251
		$item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'MNOP' );
252
253
		$this->expectException( '\\Aimeos\\MShop\\Price\\Exception' );
254
		$this->object->addProduct( $item );
255
	}
256
257
258
	public function testAddProductConfigAttributeException()
259
	{
260
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
261
		$this->object->addProduct( $this->testItem, 1, [], [-1] );
262
	}
263
264
265
	public function testAddProductLowQuantityPriceException()
266
	{
267
		$item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'IJKL', ['attribute', 'price'] );
268
269
		$this->expectException( '\\Aimeos\\MShop\\Price\\Exception' );
270
		$this->object->addProduct( $item );
271
	}
272
273
274
	public function testAddProductHigherQuantities()
275
	{
276
		$item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'IJKL', ['price'] );
277
278
		$result = $this->object->addProduct( $item, 2, [], [], [], 'unitstock' );
279
280
		$this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() );
281
		$this->assertEquals( 'IJKL', $this->object->get()->getProduct( 0 )->getProductCode() );
282
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
283
	}
284
285
286
	public function testDeleteProductFlagError()
287
	{
288
		$this->object->addProduct( $this->testItem, 2 );
289
290
		$item = $this->object->get()->getProduct( 0 );
291
		$item->setFlags( \Aimeos\MShop\Order\Item\Product\Base::FLAG_IMMUTABLE );
292
293
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
294
		$this->object->deleteProduct( 0 );
295
	}
296
297
298
	public function testUpdateProduct()
299
	{
300
		$this->object->addProduct( $this->testItem );
301
302
		$item = $this->object->get()->getProduct( 0 );
303
		$this->assertEquals( 1, $item->getQuantity() );
304
305
		$result = $this->object->updateProduct( 0, 4 );
306
		$item = $this->object->get()->getProduct( 0 );
307
308
		$this->assertEquals( 4, $item->getQuantity() );
309
		$this->assertEquals( 'U:TESTP', $item->getProductCode() );
310
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
311
	}
312
313
314
	public function testUpdateProductFlagError()
315
	{
316
		$this->object->addProduct( $this->testItem, 2 );
317
318
		$item = $this->object->get()->getProduct( 0 );
319
		$item->setFlags( \Aimeos\MShop\Order\Item\Product\Base::FLAG_IMMUTABLE );
320
321
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
322
		$this->object->updateProduct( 0, 4 );
323
	}
324
325
326
	public function testAddCoupon()
327
	{
328
		$this->object->addProduct( $this->testItem, 2 );
329
330
		$result = $this->object->addCoupon( 'GHIJ' );
331
		$basket = $this->object->get();
332
333
		$this->assertEquals( 1, count( $basket->getCoupons() ) );
334
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
335
	}
336
337
338
	public function testAddCouponExceedCount()
339
	{
340
		$this->object->addProduct( $this->testItem, 2 );
341
		$this->object->addCoupon( 'GHIJ' );
342
343
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
344
		$this->object->addCoupon( 'GHIJ' );
345
	}
346
347
348
	public function testAddCouponInvalidCode()
349
	{
350
		$this->expectException( \Aimeos\MShop\Plugin\Provider\Exception::class );
351
		$this->object->addCoupon( 'invalid' );
352
	}
353
354
355
	public function testDeleteCoupon()
356
	{
357
		$this->object->addProduct( $this->testItem, 2 );
358
		$this->object->addCoupon( '90AB' );
359
360
		$result = $this->object->deleteCoupon( '90AB' );
361
		$basket = $this->object->get();
362
363
		$this->assertEquals( 0, count( $basket->getCoupons() ) );
364
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
365
	}
366
367
368
	public function testAddAddress()
369
	{
370
		$values = array(
371
			'order.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>',
372
			'order.address.vatid' => 'DE999999999',
373
			'order.address.title' => '<br/>Dr.',
374
			'order.address.salutation' => 'mr',
375
			'order.address.firstname' => 'firstunit',
376
			'order.address.lastname' => 'lastunit',
377
			'order.address.address1' => 'unit str.',
378
			'order.address.address2' => ' 166',
379
			'order.address.address3' => '4.OG',
380
			'order.address.postal' => '22769',
381
			'order.address.city' => 'Hamburg',
382
			'order.address.state' => 'Hamburg',
383
			'order.address.countryid' => 'de',
384
			'order.address.languageid' => 'de',
385
			'order.address.telephone' => '05554433221',
386
			'order.address.email' => '[email protected]',
387
			'order.address.telefax' => '05554433222',
388
			'order.address.website' => 'www.example.com',
389
			'random key from a random manager' => [], // ups, not a string
390
		);
391
392
		$result = $this->object->addAddress( 'payment', $values );
393
		$address = $this->object->get()->getAddress( 'payment', 0 );
394
395
		$this->assertEquals( 'Example company', $address->getCompany() );
396
		$this->assertEquals( 'Dr.', $address->getTitle() );
397
		$this->assertEquals( 'firstunit', $address->getFirstname() );
398
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
399
	}
400
401
402
	public function testDeleteAddress()
403
	{
404
		$this->object->addAddress( 'payment', [] );
405
		$this->assertEquals( 1, count( $this->object->get()->getAddress( 'payment' ) ) );
406
407
		$result = $this->object->deleteAddress( 'payment' );
408
409
		$this->assertEquals( 0, count( $this->object->get()->getAddress( 'payment' ) ) );
410
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
411
	}
412
413
414
415
	public function testAddServicePayment()
416
	{
417
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
418
		$service = $manager->find( 'unitpaymentcode', [], 'service', 'payment' );
419
420
		$this->object->addService( $service );
421
		$item = $this->object->get()->getService( 'payment', 0 )->getCode();
422
		$this->assertEquals( 'unitpaymentcode', $item );
423
424
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
425
		$this->object->addService( $service, ['prepay' => true] );
426
	}
427
428
429
	public function testAddServiceDelivery()
430
	{
431
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
432
		$service = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' );
433
434
		$this->object->addService( $service );
435
		$item = $this->object->get()->getService( 'delivery', 0 );
436
		$this->assertEquals( 'unitdeliverycode', $item->getCode() );
437
438
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
439
		$this->object->addService( $service, ['fast shipping' => true, 'air shipping' => false] );
440
	}
441
442
443
	public function testDeleteServices()
444
	{
445
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
446
		$service = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' );
447
448
		$this->assertSame( $this->object, $this->object->addService( $service ) );
449
		$this->assertEquals( 'unitdeliverycode', $this->object->get()->getService( 'delivery', 0 )->getCode() );
450
451
		$this->assertSame( $this->object, $this->object->deleteService( 'delivery' ) );
452
		$this->assertEquals( 0, count( $this->object->get()->getService( 'delivery' ) ) );
453
	}
454
455
456
	public function testDeleteServicePosition()
457
	{
458
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
459
		$service = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' );
460
461
		$this->assertSame( $this->object, $this->object->addService( $service ) );
462
		$this->assertEquals( 'unitdeliverycode', $this->object->get()->getService( 'delivery', 0 )->getCode() );
463
464
		$this->assertSame( $this->object, $this->object->deleteService( 'delivery', 0 ) );
465
		$this->assertEquals( 0, count( $this->object->get()->getService( 'delivery' ) ) );
466
	}
467
468
469
	public function testCheckLocale()
470
	{
471
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
472
		$payment = $manager->find( 'unitpaymentcode', [], 'service', 'payment' );
473
		$delivery = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' );
474
475
		$this->object->addProduct( $this->testItem, 2 );
476
		$this->object->addCoupon( 'OPQR' );
477
478
		$this->object->addService( $payment );
479
		$this->object->addService( $delivery );
480
481
		$basket = $this->object->get();
482
		$price = $basket->getPrice();
483
484
		foreach( $basket->getProducts() as $product )
485
		{
486
			$this->assertEquals( 2, $product->getQuantity() );
487
			$product->getPrice()->setCurrencyId( 'CHF' );
488
		}
489
490
		$basket->getService( 'delivery', 0 )->getPrice()->setCurrencyId( 'CHF' );
491
		$basket->getService( 'payment', 0 )->getPrice()->setCurrencyId( 'CHF' );
492
		$basket->locale()->setCurrencyId( 'CHF' );
493
		$price->setCurrencyId( 'CHF' );
494
495
		$this->context->locale()->setCurrencyId( 'CHF' );
496
		$this->object->addAddress( 'payment', $this->getAddress( 'Example company' )->toArray() );
497
498
		$this->context->session()->set( 'aimeos/basket/currency', 'CHF' );
499
		$this->context->locale()->setCurrencyId( 'EUR' );
500
501
		$this->context->session()->set( 'aimeos/basket/content-unittest-en-EUR-', null );
502
503
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
504
		$basket = $object->get();
505
506
		foreach( $basket->getProducts() as $product )
507
		{
508
			$this->assertEquals( 'EUR', $product->getPrice()->getCurrencyId() );
509
			$this->assertEquals( 2, $product->getQuantity() );
510
		}
511
512
		$this->assertEquals( 'EUR', $basket->getService( 'payment', 0 )->getPrice()->getCurrencyId() );
513
		$this->assertEquals( 'EUR', $basket->getService( 'delivery', 0 )->getPrice()->getCurrencyId() );
514
		$this->assertEquals( 'EUR', $basket->locale()->getCurrencyId() );
515
		$this->assertEquals( 'EUR', $basket->getPrice()->getCurrencyId() );
516
	}
517
518
519
	/**
520
	 * @param string $company
521
	 */
522
	protected function getAddress( $company )
523
	{
524
		$addressManager = \Aimeos\MShop::create( \TestHelper::context(), 'customer/address' );
525
		$search = $addressManager->filter()->add( ['customer.address.company' => $company] );
526
527
		return $addressManager->search( $search )
528
			->first( new \RuntimeException( sprintf( 'No address item with company "%1$s" found', $company ) ) );
529
	}
530
}
531