Completed
Push — master ( f23e00...1d5010 )
by Aimeos
02:31
created

StandardTest::testStoreLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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-2017
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 static $testItem;
17
18
19
	public static function setUpBeforeClass()
20
	{
21
		$context = \TestHelperFrontend::getContext();
22
		self::$testItem = \Aimeos\MShop\Factory::createManager( $context, 'product' )->findItem( 'U:TESTP' );
23
	}
24
25
26
	protected function setUp()
27
	{
28
		\Aimeos\MShop\Factory::setCache( true );
29
30
		$this->context = \TestHelperFrontend::getContext();
31
		$this->object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
32
	}
33
34
35
	protected function tearDown()
36
	{
37
		\Aimeos\MShop\Factory::setCache( false );
38
		\Aimeos\MShop\Factory::clear();
39
40
		$this->object->clear();
41
		$this->context->getSession()->set( 'aimeos', [] );
42
43
		unset( $this->context, $this->object );
44
	}
45
46
47
	public function testClear()
48
	{
49
		$this->object->addProduct( self::$testItem->getId(), 2 );
50
		$this->object->clear();
51
52
		$this->assertEquals( 0, count( $this->object->get()->getProducts() ) );
53
	}
54
55
56
	public function testGet()
57
	{
58
		$this->assertInstanceOf( '\Aimeos\MShop\Order\Item\Base\Iface', $this->object->get() );
59
	}
60
61
62
	public function testSave()
63
	{
64
		$stub = $this->getMockBuilder( '\Aimeos\MShop\Order\Manager\Base\Standard' )
65
			->setConstructorArgs( [$this->context] )
66
			->setMethods( ['setSession'] )
67
			->getMock();
68
69
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order/base', $stub );
70
71
		$stub->expects( $this->exactly( 2 ) )->method( 'setSession' );
72
73
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
74
		$object->addProduct( self::$testItem->getId(), 2 );
75
		$object->save();
76
	}
77
78
79
	public function testSetType()
80
	{
81
		$this->assertInstanceOf( '\Aimeos\Controller\Frontend\Basket\Iface', $this->object->setType( 'test' ) );
82
	}
83
84
85
	public function testStore()
86
	{
87
		$stub = $this->getMockBuilder( '\Aimeos\MShop\Order\Manager\Base\Standard' )
88
			->setConstructorArgs( [$this->context] )
89
			->setMethods( ['store'] )
90
			->getMock();
91
92
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order/base', $stub );
93
94
		$stub->expects( $this->once() )->method( 'store' );
95
96
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
97
		$object->store();
98
	}
99
100
101
	public function testStoreLimit()
102
	{
103
		$this->context->setEditor( 'core:unittest' );
104
		$config = $this->context->getConfig();
105
		$config->set( 'controller/frontend/basket/limit-count', 0 );
106
		$config->set( 'controller/frontend/basket/limit-seconds', 86400 * 365 );
107
108
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
109
110
		$this->setExpectedException( '\Aimeos\Controller\Frontend\Basket\Exception' );
111
		$object->store();
112
	}
113
114
115
	public function testLoad()
116
	{
117
		$stub = $this->getMockBuilder( '\Aimeos\MShop\Order\Manager\Base\Standard' )
118
			->setConstructorArgs( [$this->context] )
119
			->setMethods( ['load'] )
120
			->getMock();
121
122
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order/base', $stub );
123
124
		$stub->expects( $this->once() )->method( 'load' )
125
			->will( $this->returnValue( $stub->createItem() ) );
126
127
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
128
		$object->load( -1 );
129
	}
130
131
132
	public function testAddDeleteProduct()
133
	{
134
		$basket = $this->object->get();
135
		$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' );
136
137
		$this->object->addProduct( $item->getId(), 2, [], [], [], [], [], 'default' );
138
		$item2 = $this->object->get()->getProduct( 0 );
139
		$this->object->deleteProduct( 0 );
140
141
		$this->assertEquals( 0, count( $basket->getProducts() ) );
142
		$this->assertEquals( 'CNC', $item2->getProductCode() );
143
	}
144
145
146
	public function testAddProductCustomAttribute()
147
	{
148
		$attributeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'attribute' );
149
150
		$search = $attributeManager->createSearch();
151
		$expr = array(
152
			$search->compare( '==', 'attribute.code', 'custom' ),
153
			$search->compare( '==', 'attribute.type.code', 'date' ),
154
		);
155
		$search->setConditions( $search->combine( '&&', $expr ) );
156
157
		$attributes = $attributeManager->searchItems( $search );
158
159
		if( ( $attrItem = reset( $attributes ) ) === false ) {
160
			throw new \RuntimeException( 'Attribute not found' );
161
		}
162
163
		$attrValues = array( $attrItem->getId() => '2000-01-01' );
164
165
		$this->object->addProduct( self::$testItem->getId(), 1, [], [], [], [], $attrValues );
166
		$basket = $this->object->get();
167
168
		$this->assertEquals( 1, count( $basket->getProducts() ) );
169
		$this->assertEquals( '2000-01-01', $basket->getProduct( 0 )->getAttribute( 'date', 'custom' ) );
170
	}
171
172
173
	public function testAddProductAttributeNotAssigned()
174
	{
175
		$attributeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'attribute' );
176
177
		$search = $attributeManager->createSearch();
178
		$expr = array(
179
			$search->compare( '==', 'attribute.code', '30' ),
180
			$search->compare( '==', 'attribute.type.code', 'width' ),
181
		);
182
		$search->setConditions( $search->combine( '&&', $expr ) );
183
184
		$attribute = $attributeManager->searchItems( $search );
185
186
		if( empty( $attribute ) ) {
187
			throw new \RuntimeException( 'Attribute not found' );
188
		}
189
190
		$hiddenAttrIds = array_keys( $attribute );
191
		$configAttrIds = array_keys( $attribute );
192
193
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
194
		$this->object->addProduct( self::$testItem->getId(), 1, [], [], $configAttrIds, $hiddenAttrIds );
195
	}
196
197
198
	public function testAddProductNegativeQuantityException()
199
	{
200
		$this->setExpectedException( '\\Aimeos\\MShop\\Order\\Exception' );
201
		$this->object->addProduct( self::$testItem->getId(), -1 );
202
	}
203
204
205
	public function testAddProductNoPriceException()
206
	{
207
		$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'MNOP' );
208
209
		$this->setExpectedException( '\\Aimeos\\MShop\\Price\\Exception' );
210
		$this->object->addProduct( $item->getId(), 1 );
211
	}
212
213
214
	public function testAddProductConfigAttributeException()
215
	{
216
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
217
		$this->object->addProduct( self::$testItem->getId(), 1, [], [], array( -1 ) );
218
	}
219
220
221
	public function testAddProductLowQuantityPriceException()
222
	{
223
		$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'IJKL' );
224
225
		$this->setExpectedException( '\\Aimeos\\MShop\\Price\\Exception' );
226
		$this->object->addProduct( $item->getId(), 1 );
227
	}
228
229
230
	public function testAddProductHigherQuantities()
231
	{
232
		$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'IJKL' );
233
234
		$this->object->addProduct( $item->getId(), 2, [], [], [], [], [], 'unit_type3' );
235
236
		$this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() );
237
		$this->assertEquals( 'IJKL', $this->object->get()->getProduct( 0 )->getProductCode() );
238
	}
239
240
241
	public function testDeleteProductFlagError()
242
	{
243
		$this->object->addProduct( self::$testItem->getId(), 2 );
244
245
		$item = $this->object->get()->getProduct( 0 );
246
		$item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
247
248
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
249
		$this->object->deleteProduct( 0 );
250
	}
251
252
253
	public function testEditProduct()
254
	{
255
		$this->object->addProduct( self::$testItem->getId(), 1 );
256
257
		$item = $this->object->get()->getProduct( 0 );
258
		$this->assertEquals( 1, $item->getQuantity() );
259
260
		$this->object->editProduct( 0, 4 );
261
262
		$item = $this->object->get()->getProduct( 0 );
263
		$this->assertEquals( 4, $item->getQuantity() );
264
		$this->assertEquals( 'U:TESTP', $item->getProductCode() );
265
	}
266
267
268
	public function testEditProductAttributes()
269
	{
270
		$attributeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'attribute' );
271
272
		$search = $attributeManager->createSearch();
273
		$conditions = array(
274
			$search->compare( '==', 'attribute.domain', 'product' ),
275
			$search->combine( '||', array(
276
				$search->combine( '&&', array(
277
					$search->compare( '==', 'attribute.code', 'xs' ),
278
					$search->compare( '==', 'attribute.type.code', 'size' ),
279
				) ),
280
				$search->combine( '&&', array(
281
					$search->compare( '==', 'attribute.code', 'white' ),
282
					$search->compare( '==', 'attribute.type.code', 'color' ),
283
				) ),
284
			) )
285
		);
286
		$search->setConditions( $search->combine( '&&', $conditions ) );
287
		$attributes = $attributeManager->searchItems( $search );
288
289
		if( ( $attribute = reset( $attributes ) ) === false ) {
290
			throw new \RuntimeException( 'No attributes available' );
291
		}
292
293
294
		$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'U:TESTP' );
295
296
		$this->object->addProduct( $item->getId(), 1, [], [], array_keys( $attributes ) );
297
		$this->object->editProduct( 0, 4 );
298
299
		$item = $this->object->get()->getProduct( 0 );
300
		$this->assertEquals( 2, count( $item->getAttributes() ) );
301
		$this->assertEquals( 4, $item->getQuantity() );
302
303
304
		$this->object->editProduct( 0, 3, [], array( $attribute->getType() ) );
305
306
		$item = $this->object->get()->getProduct( 0 );
307
		$this->assertEquals( 3, $item->getQuantity() );
308
		$this->assertEquals( 1, count( $item->getAttributes() ) );
309
		$this->assertEquals( 'U:TESTP', $item->getProductCode() );
310
	}
311
312
313
	public function testEditProductFlagError()
314
	{
315
		$this->object->addProduct( self::$testItem->getId(), 2 );
316
317
		$item = $this->object->get()->getProduct( 0 );
318
		$item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
319
320
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
321
		$this->object->editProduct( 0, 4 );
322
	}
323
324
325
	public function testAddCoupon()
326
	{
327
		$this->object->addProduct( self::$testItem->getId(), 2 );
328
		$this->object->addCoupon( 'GHIJ' );
329
330
		$basket = $this->object->get();
331
332
		$this->assertEquals( 1, count( $basket->getCoupons() ) );
333
	}
334
335
336
	public function testAddCouponInvalidCode()
337
	{
338
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
339
		$this->object->addCoupon( 'invalid' );
340
	}
341
342
343
	public function testAddCouponMissingRequirements()
344
	{
345
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
346
		$this->object->addCoupon( 'OPQR' );
347
	}
348
349
350
	public function testDeleteCoupon()
351
	{
352
		$this->object->addProduct( self::$testItem->getId(), 2 );
353
		$this->object->addCoupon( '90AB' );
354
		$this->object->deleteCoupon( '90AB' );
355
356
		$basket = $this->object->get();
357
358
		$this->assertEquals( 0, count( $basket->getCoupons() ) );
359
	}
360
361
362
	public function testSetAddressDelete()
363
	{
364
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, null );
365
366
		$this->setExpectedException( '\Aimeos\MShop\Order\Exception' );
367
		$this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT );
368
	}
369
370
371
	public function testSetBillingAddressByItem()
372
	{
373
		$item = $this->getAddress( 'Example company' );
374
375
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, $item );
376
377
		$address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT );
378
		$this->assertEquals( 'Example company', $address->getCompany() );
379
	}
380
381
382
	public function testSetBillingAddressByArray()
383
	{
384
		$fixture = array(
385
			'order.base.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>',
386
			'order.base.address.vatid' => 'DE999999999',
387
			'order.base.address.title' => '<br/>Dr.',
388
			'order.base.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR,
389
			'order.base.address.firstname' => 'firstunit',
390
			'order.base.address.lastname' => 'lastunit',
391
			'order.base.address.address1' => 'unit str.',
392
			'order.base.address.address2' => ' 166',
393
			'order.base.address.address3' => '4.OG',
394
			'order.base.address.postal' => '22769',
395
			'order.base.address.city' => 'Hamburg',
396
			'order.base.address.state' => 'Hamburg',
397
			'order.base.address.countryid' => 'de',
398
			'order.base.address.languageid' => 'de',
399
			'order.base.address.telephone' => '05554433221',
400
			'order.base.address.email' => '[email protected]',
401
			'order.base.address.telefax' => '05554433222',
402
			'order.base.address.website' => 'www.example.com',
403
			'order.base.address.flag' => 0,
404
		);
405
406
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, $fixture );
407
408
		$address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT );
409
		$this->assertEquals( 'Example company', $address->getCompany() );
410
		$this->assertEquals( 'Dr.', $address->getTitle() );
411
		$this->assertEquals( 'firstunit', $address->getFirstname() );
412
	}
413
414
415
	public function testSetBillingAddressByArrayError()
416
	{
417
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
418
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, array( 'error' => false ) );
419
	}
420
421
422
	public function testSetBillingAddressParameterError()
423
	{
424
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
425
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, 'error' );
426
	}
427
428
429
	public function testSetDeliveryAddressByItem()
430
	{
431
		$item = $this->getAddress( 'Example company' );
432
433
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY, $item );
434
435
		$address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY );
436
		$this->assertEquals( 'Example company', $address->getCompany() );
437
	}
438
439
440
	public function testSetDeliveryAddressByArray()
441
	{
442
		$fixture = array(
443
			'order.base.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>',
444
			'order.base.address.vatid' => 'DE999999999',
445
			'order.base.address.title' => '<br/>Dr.',
446
			'order.base.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR,
447
			'order.base.address.firstname' => 'firstunit',
448
			'order.base.address.lastname' => 'lastunit',
449
			'order.base.address.address1' => 'unit str.',
450
			'order.base.address.address2' => ' 166',
451
			'order.base.address.address3' => '4.OG',
452
			'order.base.address.postal' => '22769',
453
			'order.base.address.city' => 'Hamburg',
454
			'order.base.address.state' => 'Hamburg',
455
			'order.base.address.countryid' => 'de',
456
			'order.base.address.languageid' => 'de',
457
			'order.base.address.telephone' => '05554433221',
458
			'order.base.address.email' => '[email protected]',
459
			'order.base.address.telefax' => '05554433222',
460
			'order.base.address.website' => 'www.example.com',
461
			'order.base.address.flag' => 0,
462
		);
463
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY, $fixture );
464
465
		$address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY );
466
		$this->assertEquals( 'Example company', $address->getCompany() );
467
		$this->assertEquals( 'Dr.', $address->getTitle() );
468
		$this->assertEquals( 'firstunit', $address->getFirstname() );
469
	}
470
471
472
	public function testSetDeliveryAddressByArrayError()
473
	{
474
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
475
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY, array( 'error' => false ) );
476
	}
477
478
479
	public function testSetDeliveryAddressTypeError()
480
	{
481
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
482
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY, 'error' );
483
	}
484
485
486
	public function testSetServicePayment()
487
	{
488
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'service' );
489
		$service = $manager->findItem( 'unitpaymentcode', [], 'service', 'payment' );
490
491
		$this->object->setService( 'payment', $service->getId(), [] );
492
		$this->assertEquals( 'unitpaymentcode', $this->object->get()->getService( 'payment' )->getCode() );
493
494
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
495
		$this->object->setService( 'payment', $service->getId(), array( 'prepay' => true ) );
496
	}
497
498
499
	public function testSetDeliveryOption()
500
	{
501
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'service' );
502
		$service = $manager->findItem( 'unitcode', [], 'service', 'delivery' );
503
504
		$this->object->setService( 'delivery', $service->getId(), [] );
505
		$this->assertEquals( 'unitcode', $this->object->get()->getService( 'delivery' )->getCode() );
506
507
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
508
		$this->object->setService( 'delivery', $service->getId(), array( 'fast shipping' => true, 'air shipping' => false ) );
509
	}
510
511
512
	public function testCheckLocale()
513
	{
514
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'service' );
515
		$payment = $manager->findItem( 'unitpaymentcode', [], 'service', 'payment' );
516
		$delivery = $manager->findItem( 'unitcode', [], 'service', 'delivery' );
517
518
		$this->object->addProduct( self::$testItem->getId(), 2 );
519
		$this->object->addCoupon( 'OPQR' );
520
521
		$this->object->setService( 'payment', $payment->getId() );
522
		$this->object->setService( 'delivery', $delivery->getId() );
523
524
		$basket = $this->object->get();
525
		$price = $basket->getPrice();
526
527
		foreach( $basket->getProducts() as $product )
528
		{
529
			$this->assertEquals( 2, $product->getQuantity() );
530
			$product->getPrice()->setCurrencyId( 'CHF' );
531
		}
532
533
		$basket->getService( 'delivery' )->getPrice()->setCurrencyId( 'CHF' );
534
		$basket->getService( 'payment' )->getPrice()->setCurrencyId( 'CHF' );
535
		$basket->getLocale()->setCurrencyId( 'CHF' );
536
		$price->setCurrencyId( 'CHF' );
537
538
		$this->context->getLocale()->setCurrencyId( 'CHF' );
539
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, $this->getAddress( 'Example company' ) );
540
541
		$this->context->getSession()->set( 'aimeos/basket/currency', 'CHF' );
542
		$this->context->getLocale()->setCurrencyId( 'EUR' );
543
544
		$this->context->getSession()->set( 'aimeos/basket/content-unittest-en-EUR-', null );
545
546
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
547
		$basket = $object->get();
548
549
		foreach( $basket->getProducts() as $product )
550
		{
551
			$this->assertEquals( 'EUR', $product->getPrice()->getCurrencyId() );
552
			$this->assertEquals( 2, $product->getQuantity() );
553
		}
554
555
		$this->assertEquals( 'EUR', $basket->getService( 'payment' )->getPrice()->getCurrencyId() );
556
		$this->assertEquals( 'EUR', $basket->getService( 'delivery' )->getPrice()->getCurrencyId() );
557
		$this->assertEquals( 'EUR', $basket->getLocale()->getCurrencyId() );
558
		$this->assertEquals( 'EUR', $basket->getPrice()->getCurrencyId() );
559
	}
560
561
562
	/**
563
	 * @param string $company
564
	 */
565
	protected function getAddress( $company )
566
	{
567
		$customer = \Aimeos\MShop\Customer\Manager\Factory::createManager( \TestHelperFrontend::getContext(), 'Standard' );
568
		$addressManager = $customer->getSubManager( 'address', 'Standard' );
569
570
		$search = $addressManager->createSearch();
571
		$search->setConditions( $search->compare( '==', 'customer.address.company', $company ) );
572
		$items = $addressManager->searchItems( $search );
573
574
		if( ( $item = reset( $items ) ) === false ) {
575
			throw new \RuntimeException( sprintf( 'No address item with company "%1$s" found', $company ) );
576
		}
577
578
		return $item;
579
	}
580
}
581