Completed
Push — master ( b46c0d...fe698d )
by Aimeos
06:27
created

StandardTest::testAddCouponInvalidCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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