Completed
Push — master ( 45b214...6fb08c )
by Aimeos
03:09
created

StandardTest::testSetDeliveryAddressByArray()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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