StandardTest::testAddAddress()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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