Passed
Push — master ( 23b209...e4b48e )
by Aimeos
17:50 queued 08:55
created

StandardTest::testAddProductOptionalParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
rs 10
c 0
b 0
f 0
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-2022
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.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( ['get'] )
96
			->getMock();
97
98
		\Aimeos\MShop::inject( 'order/base', $stub );
99
100
		$stub->expects( $this->once() )->method( 'get' )
101
			->will( $this->returnValue( $stub->create() ) );
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->create(), $this->context->locale()] )
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->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->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
228
	}
229
230
231
	public function testAddProductAttributeNotAssigned()
232
	{
233
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( '30', [], 'product', 'width' );
234
		$ids = [$attrItem->getId()];
235
236
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
237
		$this->object->addProduct( $this->testItem, 1, [], $ids, $ids );
238
	}
239
240
241
	public function testAddProductNegativeQuantityException()
242
	{
243
		$this->expectException( '\\Aimeos\\MShop\\Order\\Exception' );
244
		$this->object->addProduct( $this->testItem, -1 );
245
	}
246
247
248
	public function testAddProductNoPriceException()
249
	{
250
		$item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'MNOP' );
251
252
		$this->expectException( '\\Aimeos\\MShop\\Price\\Exception' );
253
		$this->object->addProduct( $item );
254
	}
255
256
257
	public function testAddProductConfigAttributeException()
258
	{
259
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
260
		$this->object->addProduct( $this->testItem, 1, [], [-1] );
261
	}
262
263
264
	public function testAddProductLowQuantityPriceException()
265
	{
266
		$item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'IJKL', ['attribute', 'price'] );
267
268
		$this->expectException( '\\Aimeos\\MShop\\Price\\Exception' );
269
		$this->object->addProduct( $item );
270
	}
271
272
273
	public function testAddProductHigherQuantities()
274
	{
275
		$item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'IJKL', ['price'] );
276
277
		$result = $this->object->addProduct( $item, 2, [], [], [], 'unitstock' );
278
279
		$this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() );
280
		$this->assertEquals( 'IJKL', $this->object->get()->getProduct( 0 )->getProductCode() );
281
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
282
	}
283
284
285
	public function testDeleteProductFlagError()
286
	{
287
		$this->object->addProduct( $this->testItem, 2 );
288
289
		$item = $this->object->get()->getProduct( 0 );
290
		$item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
291
292
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
293
		$this->object->deleteProduct( 0 );
294
	}
295
296
297
	public function testUpdateProduct()
298
	{
299
		$this->object->addProduct( $this->testItem );
300
301
		$item = $this->object->get()->getProduct( 0 );
302
		$this->assertEquals( 1, $item->getQuantity() );
303
304
		$result = $this->object->updateProduct( 0, 4 );
305
		$item = $this->object->get()->getProduct( 0 );
306
307
		$this->assertEquals( 4, $item->getQuantity() );
308
		$this->assertEquals( 'U:TESTP', $item->getProductCode() );
309
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
310
	}
311
312
313
	public function testUpdateProductFlagError()
314
	{
315
		$this->object->addProduct( $this->testItem, 2 );
316
317
		$item = $this->object->get()->getProduct( 0 );
318
		$item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
319
320
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
321
		$this->object->updateProduct( 0, 4 );
322
	}
323
324
325
	public function testAddCoupon()
326
	{
327
		$this->object->addProduct( $this->testItem, 2 );
328
329
		$result = $this->object->addCoupon( 'GHIJ' );
330
		$basket = $this->object->get();
331
332
		$this->assertEquals( 1, count( $basket->getCoupons() ) );
333
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
334
	}
335
336
337
	public function testAddCouponExceedCount()
338
	{
339
		$this->object->addProduct( $this->testItem, 2 );
340
		$this->object->addCoupon( 'GHIJ' );
341
342
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
343
		$this->object->addCoupon( 'GHIJ' );
344
	}
345
346
347
	public function testAddCouponInvalidCode()
348
	{
349
		$this->expectException( \Aimeos\MShop\Plugin\Provider\Exception::class );
350
		$this->object->addCoupon( 'invalid' );
351
	}
352
353
354
	public function testDeleteCoupon()
355
	{
356
		$this->object->addProduct( $this->testItem, 2 );
357
		$this->object->addCoupon( '90AB' );
358
359
		$result = $this->object->deleteCoupon( '90AB' );
360
		$basket = $this->object->get();
361
362
		$this->assertEquals( 0, count( $basket->getCoupons() ) );
363
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
364
	}
365
366
367
	public function testAddAddress()
368
	{
369
		$values = array(
370
			'order.base.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>',
371
			'order.base.address.vatid' => 'DE999999999',
372
			'order.base.address.title' => '<br/>Dr.',
373
			'order.base.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR,
374
			'order.base.address.firstname' => 'firstunit',
375
			'order.base.address.lastname' => 'lastunit',
376
			'order.base.address.address1' => 'unit str.',
377
			'order.base.address.address2' => ' 166',
378
			'order.base.address.address3' => '4.OG',
379
			'order.base.address.postal' => '22769',
380
			'order.base.address.city' => 'Hamburg',
381
			'order.base.address.state' => 'Hamburg',
382
			'order.base.address.countryid' => 'de',
383
			'order.base.address.languageid' => 'de',
384
			'order.base.address.telephone' => '05554433221',
385
			'order.base.address.email' => '[email protected]',
386
			'order.base.address.telefax' => '05554433222',
387
			'order.base.address.website' => 'www.example.com',
388
			'random key from a random manager' => [], // ups, not a string
389
		);
390
391
		$result = $this->object->addAddress( 'payment', $values );
392
		$address = $this->object->get()->getAddress( 'payment', 0 );
393
394
		$this->assertEquals( 'Example company', $address->getCompany() );
395
		$this->assertEquals( 'Dr.', $address->getTitle() );
396
		$this->assertEquals( 'firstunit', $address->getFirstname() );
397
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
398
	}
399
400
401
	public function testDeleteAddress()
402
	{
403
		$this->object->addAddress( 'payment', [] );
404
		$this->assertEquals( 1, count( $this->object->get()->getAddress( 'payment' ) ) );
405
406
		$result = $this->object->deleteAddress( 'payment' );
407
408
		$this->assertEquals( 0, count( $this->object->get()->getAddress( 'payment' ) ) );
409
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
410
	}
411
412
413
414
	public function testAddServicePayment()
415
	{
416
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
417
		$service = $manager->find( 'unitpaymentcode', [], 'service', 'payment' );
418
419
		$this->object->addService( $service );
420
		$item = $this->object->get()->getService( 'payment', 0 )->getCode();
421
		$this->assertEquals( 'unitpaymentcode', $item );
422
423
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
424
		$this->object->addService( $service, ['prepay' => true] );
425
	}
426
427
428
	public function testAddServiceDelivery()
429
	{
430
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
431
		$service = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' );
432
433
		$this->object->addService( $service );
434
		$item = $this->object->get()->getService( 'delivery', 0 );
435
		$this->assertEquals( 'unitdeliverycode', $item->getCode() );
436
437
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
438
		$this->object->addService( $service, ['fast shipping' => true, 'air shipping' => false] );
439
	}
440
441
442
	public function testDeleteServices()
443
	{
444
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
445
		$service = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' );
446
447
		$this->assertSame( $this->object, $this->object->addService( $service ) );
448
		$this->assertEquals( 'unitdeliverycode', $this->object->get()->getService( 'delivery', 0 )->getCode() );
449
450
		$this->assertSame( $this->object, $this->object->deleteService( 'delivery' ) );
451
		$this->assertEquals( 0, count( $this->object->get()->getService( 'delivery' ) ) );
452
	}
453
454
455
	public function testDeleteServicePosition()
456
	{
457
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
458
		$service = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' );
459
460
		$this->assertSame( $this->object, $this->object->addService( $service ) );
461
		$this->assertEquals( 'unitdeliverycode', $this->object->get()->getService( 'delivery', 0 )->getCode() );
462
463
		$this->assertSame( $this->object, $this->object->deleteService( 'delivery', 0 ) );
464
		$this->assertEquals( 0, count( $this->object->get()->getService( 'delivery' ) ) );
465
	}
466
467
468
	public function testCheckLocale()
469
	{
470
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
471
		$payment = $manager->find( 'unitpaymentcode', [], 'service', 'payment' );
472
		$delivery = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' );
473
474
		$this->object->addProduct( $this->testItem, 2 );
475
		$this->object->addCoupon( 'OPQR' );
476
477
		$this->object->addService( $payment );
478
		$this->object->addService( $delivery );
479
480
		$basket = $this->object->get();
481
		$price = $basket->getPrice();
482
483
		foreach( $basket->getProducts() as $product )
484
		{
485
			$this->assertEquals( 2, $product->getQuantity() );
486
			$product->getPrice()->setCurrencyId( 'CHF' );
487
		}
488
489
		$basket->getService( 'delivery', 0 )->getPrice()->setCurrencyId( 'CHF' );
490
		$basket->getService( 'payment', 0 )->getPrice()->setCurrencyId( 'CHF' );
491
		$basket->locale()->setCurrencyId( 'CHF' );
492
		$price->setCurrencyId( 'CHF' );
493
494
		$this->context->locale()->setCurrencyId( 'CHF' );
495
		$this->object->addAddress( 'payment', $this->getAddress( 'Example company' )->toArray() );
496
497
		$this->context->session()->set( 'aimeos/basket/currency', 'CHF' );
498
		$this->context->locale()->setCurrencyId( 'EUR' );
499
500
		$this->context->session()->set( 'aimeos/basket/content-unittest-en-EUR-', null );
501
502
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
503
		$basket = $object->get();
504
505
		foreach( $basket->getProducts() as $product )
506
		{
507
			$this->assertEquals( 'EUR', $product->getPrice()->getCurrencyId() );
508
			$this->assertEquals( 2, $product->getQuantity() );
509
		}
510
511
		$this->assertEquals( 'EUR', $basket->getService( 'payment', 0 )->getPrice()->getCurrencyId() );
512
		$this->assertEquals( 'EUR', $basket->getService( 'delivery', 0 )->getPrice()->getCurrencyId() );
513
		$this->assertEquals( 'EUR', $basket->locale()->getCurrencyId() );
514
		$this->assertEquals( 'EUR', $basket->getPrice()->getCurrencyId() );
515
	}
516
517
518
	/**
519
	 * @param string $company
520
	 */
521
	protected function getAddress( $company )
522
	{
523
		$customer = \Aimeos\MShop\Customer\Manager\Factory::create( \TestHelper::context(), 'Standard' );
524
		$addressManager = $customer->getSubManager( 'address', 'Standard' );
525
526
		$search = $addressManager->filter();
527
		$search->setConditions( $search->compare( '==', 'customer.address.company', $company ) );
528
529
		if( ( $item = $addressManager->search( $search )->first() ) === null ) {
530
			throw new \RuntimeException( sprintf( 'No address item with company "%1$s" found', $company ) );
531
		}
532
533
		return $item;
534
	}
535
}
536