Passed
Push — master ( d10a1a...f8298b )
by Aimeos
07:58
created

StandardTest::testAddProductFractionalQuantity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
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-2020
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 = \TestHelperFrontend::getContext();
24
25
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
26
		$this->testItem = $manager->findItem( '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->getSession()->set( 'aimeos', [] );
38
39
		unset( $this->context, $this->object );
40
	}
41
42
43
	public function testAdd()
44
	{
45
		$result = $this->object->add( ['order.base.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\Base\Iface::class, $this->object->get() );
64
	}
65
66
67
	public function testSave()
68
	{
69
		$stub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Base\Standard::class )
70
			->setConstructorArgs( [$this->context] )
71
			->setMethods( ['setSession'] )
72
			->getMock();
73
74
		\Aimeos\MShop::inject( 'order/base', $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\Base\Standard::class )
94
			->setConstructorArgs( [$this->context] )
95
			->setMethods( ['load'] )
96
			->getMock();
97
98
		\Aimeos\MShop::inject( 'order/base', $stub );
99
100
		$stub->expects( $this->once() )->method( 'load' )
101
			->will( $this->returnValue( $stub->createItem() ) );
102
103
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
104
105
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $object->load( -1 ) );
106
	}
107
108
109
	public function testStore()
110
	{
111
		$stub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Base\Standard::class )
112
			->setConstructorArgs( [$this->context] )
113
			->setMethods( ['store'] )
114
			->getMock();
115
116
		\Aimeos\MShop::inject( 'order/base', $stub );
117
118
		$priceManager = \Aimeos\MShop::create( $this->context, 'price' );
119
120
		$basket = $this->getMockBuilder( \Aimeos\MShop\Order\Item\Base\Standard::class )
121
			->setConstructorArgs( [$priceManager->createItem(), $this->context->getLocale()] )
122
			->setMethods( ['check'] )
123
			->getMock();
124
125
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
126
			->setConstructorArgs( [$this->context] )
127
			->setMethods( ['get'] )
128
			->getMock();
129
130
		$object->expects( $this->once() )->method( 'get' )->will( $this->returnValue( $basket ) );
131
		$basket->expects( $this->once() )->method( 'check' )->will( $this->returnValue( $basket ) );
132
		$stub->expects( $this->once() )->method( 'store' )->will( $this->returnValue( $basket ) );
133
134
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $object->store() );
135
	}
136
137
138
	public function testStoreLimit()
139
	{
140
		$this->context->setEditor( 'core:lib/mshoplib' );
141
		$config = $this->context->getConfig();
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->findItem( 'CNC', ['attribute', 'media', 'price', 'product', 'text'] );
157
158
		$result1 = $this->object->addProduct( $item, 2, [], [], [], 'default', 'unitsupplier' );
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( 'unitsupplier', $item2->getSupplierCode() );
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
		$basket = $this->object->get();
0 ignored issues
show
Unused Code introduced by
The assignment to $basket is dead and can be removed.
Loading history...
174
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
175
		$item = $manager->findItem( 'CNC', ['attribute', 'media', 'price', 'product', 'text'] );
176
		$item->setConfig( ['quantity-step' => '0.1'] );
177
178
		$result1 = $this->object->addProduct( $item, 2.31 );
0 ignored issues
show
Unused Code introduced by
The assignment to $result1 is dead and can be removed.
Loading history...
179
		$this->assertEquals( 2.4, $this->object->get()->getProduct( 0 )->getQuantity() );
180
	}
181
182
183
	public function testAddProductCustomAttribute()
184
	{
185
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'custom', [], 'product', 'date' );
186
		$attrValues = [$attrItem->getId() => '2000-01-01'];
187
188
		$result = $this->object->addProduct( $this->testItem, 1, [], [], $attrValues );
189
		$basket = $this->object->get();
190
191
		$this->assertEquals( 1, count( $basket->getProducts() ) );
192
		$this->assertEquals( '2000-01-01', $basket->getProduct( 0 )->getAttribute( 'date', 'custom' ) );
193
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
194
	}
195
196
197
	public function testAddProductCustomPrice()
198
	{
199
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'custom', [], 'product', 'price' );
200
		$attrValues = [$attrItem->getId() => '0.01'];
201
202
		$result = $this->object->addProduct( $this->testItem, 1, [], [], $attrValues );
203
		$basket = $this->object->get();
204
205
		$this->assertEquals( 1, count( $basket->getProducts() ) );
206
		$this->assertEquals( '0.01', $basket->getProduct( 0 )->getPrice()->getValue() );
207
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
208
	}
209
210
211
	public function testAddProductCustomPriceException()
212
	{
213
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'custom', [], 'product', 'price' );
214
		$attrValues = [$attrItem->getId() => ','];
215
216
		$this->expectException( \Aimeos\Controller\Frontend\Basket\Exception::class );
217
		$this->object->addProduct( $this->testItem, 1, [], [], $attrValues );
218
	}
219
220
221
	public function testAddProductAttributePrice()
222
	{
223
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'xs', [], 'product', 'size' );
224
225
		$result = $this->object->addProduct( $this->testItem, 1, [], [$attrItem->getId() => 2] );
226
227
		$this->assertEquals( '43.90', $this->object->get()->getPrice()->getValue() );
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' )->findItem( '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' )->findItem( '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' )->findItem( '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' )->findItem( 'IJKL', ['price'] );
277
278
		$result = $this->object->addProduct( $item, 2 );
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 testAddProductOptionalParameters()
287
	{
288
		$item = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'IJKL', ['price'] );
289
290
		$product = $this->object->addProduct( $item, 2, [], [], [], 'stock', 'supplier', 123 )->get()->getProduct( 0 );
291
292
		$this->assertEquals( 'stock', $product->getStockType() );
293
		$this->assertEquals( 'supplier', $product->getSupplierCode() );
294
		$this->assertEquals( 123, $product->getSiteId() );
295
	}
296
297
298
	public function testDeleteProductFlagError()
299
	{
300
		$this->object->addProduct( $this->testItem, 2 );
301
302
		$item = $this->object->get()->getProduct( 0 );
303
		$item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
304
305
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
306
		$this->object->deleteProduct( 0 );
307
	}
308
309
310
	public function testUpdateProduct()
311
	{
312
		$this->object->addProduct( $this->testItem );
313
314
		$item = $this->object->get()->getProduct( 0 );
315
		$this->assertEquals( 1, $item->getQuantity() );
316
317
		$result = $this->object->updateProduct( 0, 4 );
318
		$item = $this->object->get()->getProduct( 0 );
319
320
		$this->assertEquals( 4, $item->getQuantity() );
321
		$this->assertEquals( 'U:TESTP', $item->getProductCode() );
322
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
323
	}
324
325
326
	public function testUpdateProductFlagError()
327
	{
328
		$this->object->addProduct( $this->testItem, 2 );
329
330
		$item = $this->object->get()->getProduct( 0 );
331
		$item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
332
333
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
334
		$this->object->updateProduct( 0, 4 );
335
	}
336
337
338
	public function testAddCoupon()
339
	{
340
		$this->object->addProduct( $this->testItem, 2 );
341
342
		$result = $this->object->addCoupon( 'GHIJ' );
343
		$basket = $this->object->get();
344
345
		$this->assertEquals( 1, count( $basket->getCoupons() ) );
346
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
347
	}
348
349
350
	public function testAddCouponExceedCount()
351
	{
352
		$this->object->addProduct( $this->testItem, 2 );
353
		$this->object->addCoupon( 'GHIJ' );
354
355
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
356
		$this->object->addCoupon( 'GHIJ' );
357
	}
358
359
360
	public function testAddCouponInvalidCode()
361
	{
362
		$this->expectException( \Aimeos\MShop\Plugin\Provider\Exception::class );
363
		$this->object->addCoupon( 'invalid' );
364
	}
365
366
367
	public function testDeleteCoupon()
368
	{
369
		$this->object->addProduct( $this->testItem, 2 );
370
		$this->object->addCoupon( '90AB' );
371
372
		$result = $this->object->deleteCoupon( '90AB' );
373
		$basket = $this->object->get();
374
375
		$this->assertEquals( 0, count( $basket->getCoupons() ) );
376
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
377
	}
378
379
380
	public function testAddAddress()
381
	{
382
		$values = array(
383
			'order.base.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>',
384
			'order.base.address.vatid' => 'DE999999999',
385
			'order.base.address.title' => '<br/>Dr.',
386
			'order.base.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR,
387
			'order.base.address.firstname' => 'firstunit',
388
			'order.base.address.lastname' => 'lastunit',
389
			'order.base.address.address1' => 'unit str.',
390
			'order.base.address.address2' => ' 166',
391
			'order.base.address.address3' => '4.OG',
392
			'order.base.address.postal' => '22769',
393
			'order.base.address.city' => 'Hamburg',
394
			'order.base.address.state' => 'Hamburg',
395
			'order.base.address.countryid' => 'de',
396
			'order.base.address.languageid' => 'de',
397
			'order.base.address.telephone' => '05554433221',
398
			'order.base.address.email' => '[email protected]',
399
			'order.base.address.telefax' => '05554433222',
400
			'order.base.address.website' => 'www.example.com',
401
		);
402
403
		$result = $this->object->addAddress( 'payment', $values );
404
		$address = $this->object->get()->getAddress( 'payment', 0 );
405
406
		$this->assertEquals( 'Example company', $address->getCompany() );
407
		$this->assertEquals( 'Dr.', $address->getTitle() );
408
		$this->assertEquals( 'firstunit', $address->getFirstname() );
409
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
410
	}
411
412
413
	public function testDeleteAddress()
414
	{
415
		$this->object->addAddress( 'payment', [] );
416
		$this->assertEquals( 1, count( $this->object->get()->getAddress( 'payment' ) ) );
417
418
		$result = $this->object->deleteAddress( 'payment' );
419
420
		$this->assertEquals( 0, count( $this->object->get()->getAddress( 'payment' ) ) );
421
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
422
	}
423
424
425
426
	public function testAddServicePayment()
427
	{
428
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
429
		$service = $manager->findItem( 'unitpaymentcode', [], 'service', 'payment' );
430
431
		$this->object->addService( $service );
432
		$item = $this->object->get()->getService( 'payment', 0 )->getCode();
433
		$this->assertEquals( 'unitpaymentcode', $item );
434
435
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
436
		$this->object->addService( $service, ['prepay' => true] );
437
	}
438
439
440
	public function testAddServiceDelivery()
441
	{
442
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
443
		$service = $manager->findItem( 'unitcode', [], 'service', 'delivery' );
444
445
		$this->object->addService( $service );
446
		$item = $this->object->get()->getService( 'delivery', 0 );
447
		$this->assertEquals( 'unitcode', $item->getCode() );
448
449
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
450
		$this->object->addService( $service, ['fast shipping' => true, 'air shipping' => false] );
451
	}
452
453
454
	public function testDeleteServices()
455
	{
456
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
457
		$service = $manager->findItem( 'unitcode', [], 'service', 'delivery' );
458
459
		$this->assertSame( $this->object, $this->object->addService( $service ) );
460
		$this->assertEquals( 'unitcode', $this->object->get()->getService( 'delivery', 0 )->getCode() );
461
462
		$this->assertSame( $this->object, $this->object->deleteService( 'delivery' ) );
463
		$this->assertEquals( 0, count( $this->object->get()->getService( 'delivery' ) ) );
464
	}
465
466
467
	public function testDeleteServicePosition()
468
	{
469
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
470
		$service = $manager->findItem( 'unitcode', [], 'service', 'delivery' );
471
472
		$this->assertSame( $this->object, $this->object->addService( $service ) );
473
		$this->assertEquals( 'unitcode', $this->object->get()->getService( 'delivery', 0 )->getCode() );
474
475
		$this->assertSame( $this->object, $this->object->deleteService( 'delivery', 0 ) );
476
		$this->assertEquals( 0, count( $this->object->get()->getService( 'delivery' ) ) );
477
	}
478
479
480
	public function testCheckLocale()
481
	{
482
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
483
		$payment = $manager->findItem( 'unitpaymentcode', [], 'service', 'payment' );
484
		$delivery = $manager->findItem( 'unitcode', [], 'service', 'delivery' );
485
486
		$this->object->addProduct( $this->testItem, 2 );
487
		$this->object->addCoupon( 'OPQR' );
488
489
		$this->object->addService( $payment );
490
		$this->object->addService( $delivery );
491
492
		$basket = $this->object->get();
493
		$price = $basket->getPrice();
494
495
		foreach( $basket->getProducts() as $product )
496
		{
497
			$this->assertEquals( 2, $product->getQuantity() );
498
			$product->getPrice()->setCurrencyId( 'CHF' );
499
		}
500
501
		$basket->getService( 'delivery', 0 )->getPrice()->setCurrencyId( 'CHF' );
502
		$basket->getService( 'payment', 0 )->getPrice()->setCurrencyId( 'CHF' );
503
		$basket->getLocale()->setCurrencyId( 'CHF' );
504
		$price->setCurrencyId( 'CHF' );
505
506
		$this->context->getLocale()->setCurrencyId( 'CHF' );
507
		$this->object->addAddress( 'payment', $this->getAddress( 'Example company' )->toArray() );
508
509
		$this->context->getSession()->set( 'aimeos/basket/currency', 'CHF' );
510
		$this->context->getLocale()->setCurrencyId( 'EUR' );
511
512
		$this->context->getSession()->set( 'aimeos/basket/content-unittest-en-EUR-', null );
513
514
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
515
		$basket = $object->get();
516
517
		foreach( $basket->getProducts() as $product )
518
		{
519
			$this->assertEquals( 'EUR', $product->getPrice()->getCurrencyId() );
520
			$this->assertEquals( 2, $product->getQuantity() );
521
		}
522
523
		$this->assertEquals( 'EUR', $basket->getService( 'payment', 0 )->getPrice()->getCurrencyId() );
524
		$this->assertEquals( 'EUR', $basket->getService( 'delivery', 0 )->getPrice()->getCurrencyId() );
525
		$this->assertEquals( 'EUR', $basket->getLocale()->getCurrencyId() );
526
		$this->assertEquals( 'EUR', $basket->getPrice()->getCurrencyId() );
527
	}
528
529
530
	/**
531
	 * @param string $company
532
	 */
533
	protected function getAddress( $company )
534
	{
535
		$customer = \Aimeos\MShop\Customer\Manager\Factory::create( \TestHelperFrontend::getContext(), 'Standard' );
536
		$addressManager = $customer->getSubManager( 'address', 'Standard' );
537
538
		$search = $addressManager->createSearch();
539
		$search->setConditions( $search->compare( '==', 'customer.address.company', $company ) );
540
541
		if( ( $item = $addressManager->searchItems( $search )->first() ) === null ) {
542
			throw new \RuntimeException( sprintf( 'No address item with company "%1$s" found', $company ) );
543
		}
544
545
		return $item;
546
	}
547
}
548