Completed
Branch master (4ffab1)
by Aimeos
02:31
created

StandardTest::testSetAddressDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Aimeos\Controller\Frontend\Basket;
4
5
6
/**
7
 * @copyright Metaways Infosystems GmbH, 2012
8
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
9
 * @copyright Aimeos (aimeos.org), 2015
10
 */
11
class StandardTest extends \PHPUnit_Framework_TestCase
12
{
13
	private $object;
14
	private $context;
15
	private $testItem;
16
17
18
	protected function setUp()
19
	{
20
		$this->context = \TestHelperFrontend::getContext();
21
		$this->object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
22
23
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
24
25
		$search = $productManager->createSearch();
26
		$search->setConditions( $search->compare( '==', 'product.code', 'U:TESTP' ) );
27
28
		$items = $productManager->searchItems( $search, array( 'text' ) );
29
30
		if( ( $this->testItem = reset( $items ) ) === false ) {
31
			throw new \Exception( 'Product not found' );
32
		}
33
	}
34
35
36
	protected function tearDown()
37
	{
38
		$this->object->clear();
39
		$this->context->getSession()->set( 'aimeos', array() );
40
41
		unset( $this->object, $this->testItem );
42
	}
43
44
45
	public function testAddDeleteProduct()
46
	{
47
		$basket = $this->object->get();
48
49
50
		$this->object->addProduct( $this->testItem->getId(), 2 );
51
52
		$this->assertEquals( 1, count( $basket->getProducts() ) );
53
		$this->assertEquals( 2, $basket->getProduct( 0 )->getQuantity() );
54
		$this->assertEquals( 'U:TESTPSUB01', $basket->getProduct( 0 )->getProductCode() );
55
56
57
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
58
59
		$search = $productManager->createSearch();
60
		$search->setConditions( $search->compare( '==', 'product.code', 'CNC' ) );
61
62
		$items = $productManager->searchItems( $search, array( 'text' ) );
63
64
		if( ( $item = reset( $items ) ) === false ) {
65
			throw new \Exception( 'Product not found' );
66
		}
67
68
		$this->object->addProduct( $item->getId(), 2, array(), array(), array(), array(), array(), 'default' );
69
		$item2 = $this->object->get()->getProduct( 1 );
70
		$this->object->deleteProduct( 0 );
71
72
		$this->assertEquals( 1, count( $basket->getProducts() ) );
73
		$this->assertEquals( $item2, $basket->getProduct( 1 ) );
74
		$this->assertEquals( 'CNC', $basket->getProduct( 1 )->getProductCode() );
75
	}
76
77
78
	public function testAddProductBundle()
79
	{
80
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
81
82
		$search = $productManager->createSearch();
83
		$search->setConditions( $search->compare( '==', 'product.code', 'U:BUNDLE' ) );
84
85
		$items = $productManager->searchItems( $search );
86
87
		if( ( $item = reset( $items ) ) === false ) {
88
			throw new \Exception( 'Product not found' );
89
		}
90
91
92
		$this->object->addProduct( $item->getId(), 1 );
93
94
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
95
		$this->assertEquals( 'U:BUNDLE', $this->object->get()->getProduct( 0 )->getProductCode() );
96
		$this->assertEquals( 2, count( $this->object->get()->getProduct( 0 )->getProducts() ) );
97
	}
98
99
100
	public function testAddProductVariant()
101
	{
102
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
103
104
		$search = $productManager->createSearch();
105
		$search->setConditions( $search->compare( '==', 'product.code', 'CNC' ) );
106
107
		$items = $productManager->searchItems( $search, array( 'text' ) );
108
109
		if( ( $item = reset( $items ) ) === false ) {
110
			throw new \Exception( 'Product not found' );
111
		}
112
113
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
114
115
		$search = $attributeManager->createSearch();
116
		$search->setConditions( $search->compare( '==', 'attribute.code', array( 'xs', 'white' ) ) );
117
118
		$attributes = $attributeManager->searchItems( $search );
119
120
		if( count( $attributes ) === 0 ) {
121
			throw new \Exception( 'Attributes not found' );
122
		}
123
124
125
		$this->object->addProduct( $item->getId(), 1, array(), array_keys( $attributes ), array(), array(), array(), 'default' );
126
127
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
128
		$this->assertEquals( 'CNC', $this->object->get()->getProduct( 0 )->getProductCode() );
129
	}
130
131
132
	public function testAddProductVariantIncomplete()
133
	{
134
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
135
136
		$search = $productManager->createSearch();
137
		$search->setConditions( $search->compare( '==', 'product.code', 'U:TEST' ) );
138
139
		$items = $productManager->searchItems( $search, array() );
140
141
		if( ( $item = reset( $items ) ) === false ) {
142
			throw new \Exception( 'Product not found' );
143
		}
144
145
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
146
147
		$search = $attributeManager->createSearch();
148
		$expr = array(
149
			$search->compare( '==', 'attribute.domain', 'product' ),
150
			$search->compare( '==', 'attribute.code', '30' ),
151
			$search->compare( '==', 'attribute.type.code', 'length' ),
152
		);
153
		$search->setConditions( $search->combine( '&&', $expr ) );
154
155
		$attributes = $attributeManager->searchItems( $search );
156
157
		if( count( $attributes ) === 0 ) {
158
			throw new \Exception( 'Attributes not found' );
159
		}
160
161
162
		$this->object->addProduct( $item->getId(), 1, array(), array_keys( $attributes ) );
163
164
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
165
		$this->assertEquals( 'U:TESTSUB02', $this->object->get()->getProduct( 0 )->getProductCode() );
166
		$this->assertEquals( 2, count( $this->object->get()->getProduct( 0 )->getAttributes() ) );
167
	}
168
169
170
	public function testAddProductVariantNonUnique()
171
	{
172
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
173
174
		$search = $productManager->createSearch();
175
		$search->setConditions( $search->compare( '==', 'product.code', 'U:TEST' ) );
176
177
		$items = $productManager->searchItems( $search, array() );
178
179
		if( ( $item = reset( $items ) ) === false ) {
180
			throw new \Exception( 'Product not found' );
181
		}
182
183
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
184
185
		$search = $attributeManager->createSearch();
186
		$expr = array(
187
			$search->compare( '==', 'attribute.domain', 'product' ),
188
			$search->compare( '==', 'attribute.code', '30' ),
189
			$search->compare( '==', 'attribute.type.code', 'width' ),
190
		);
191
		$search->setConditions( $search->combine( '&&', $expr ) );
192
193
		$attributes = $attributeManager->searchItems( $search );
194
195
		if( count( $attributes ) === 0 ) {
196
			throw new \Exception( 'Attributes not found' );
197
		}
198
199
200
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
201
		$this->object->addProduct( $item->getId(), 1, array(), array_keys( $attributes ) );
202
	}
203
204
205
	public function testAddProductVariantNotRequired()
206
	{
207
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
208
209
		$search = $attributeManager->createSearch();
210
		$search->setConditions( $search->compare( '==', 'attribute.code', 'xs' ) );
211
212
		$attributes = $attributeManager->searchItems( $search );
213
214
		if( count( $attributes ) === 0 ) {
215
			throw new \Exception( 'Attribute not found' );
216
		}
217
218
		$options = array( 'variant' => false );
219
220
		$this->object->addProduct( $this->testItem->getId(), 1, $options, array_keys( $attributes ) );
221
222
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
223
		$this->assertEquals( 'U:TESTP', $this->object->get()->getProduct( 0 )->getProductCode() );
224
	}
225
226
227
	public function testAddProductConfigAttribute()
228
	{
229
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
230
231
		$search = $attributeManager->createSearch();
232
		$search->setConditions( $search->compare( '==', 'attribute.code', 'xs' ) );
233
234
		$attributes = $attributeManager->searchItems( $search );
235
236
		if( empty( $attributes ) ) {
237
			throw new \Exception( 'Attribute not found' );
238
		}
239
240
		$this->object->addProduct( $this->testItem->getId(), 1, array(), array(), array_keys( $attributes ) );
241
		$basket = $this->object->get();
242
243
		$this->assertEquals( 1, count( $basket->getProducts() ) );
244
		$this->assertEquals( 'U:TESTPSUB01', $basket->getProduct( 0 )->getProductCode() );
245
		$this->assertEquals( 'xs', $basket->getProduct( 0 )->getAttribute( 'size', 'config' ) );
246
	}
247
248
249
	public function testAddProductHiddenAttribute()
250
	{
251
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
252
253
		$search = $attributeManager->createSearch();
254
		$expr = array(
255
			$search->compare( '==', 'attribute.code', '29' ),
256
			$search->compare( '==', 'attribute.type.code', 'width' ),
257
		);
258
		$search->setConditions( $search->combine( '&&', $expr ) );
259
260
		$attributes = $attributeManager->searchItems( $search );
261
262
		if( empty( $attributes ) ) {
263
			throw new \Exception( 'Attribute not found' );
264
		}
265
266
		$this->object->addProduct( $this->testItem->getId(), 1, array(), array(), array(), array_keys( $attributes ) );
267
268
		$basket = $this->object->get();
269
		$this->assertEquals( 1, count( $basket->getProducts() ) );
270
271
		$product = $basket->getProduct( 0 );
272
		$this->assertEquals( 'U:TESTPSUB01', $product->getProductCode() );
273
274
		$attributes = $product->getAttributes();
275
		$this->assertEquals( 1, count( $attributes ) );
276
277
		if( ( $attribute = reset( $attributes ) ) === false ) {
278
			throw new \Exception( 'No attribute' );
279
		}
280
281
		$this->assertEquals( 'hidden', $attribute->getType() );
282
		$this->assertEquals( '29', $product->getAttribute( 'width', 'hidden' ) );
283
	}
284
285
286
	public function testAddProductCustomAttribute()
287
	{
288
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
289
290
		$search = $attributeManager->createSearch();
291
		$expr = array(
292
			$search->compare( '==', 'attribute.code', 'custom' ),
293
			$search->compare( '==', 'attribute.type.code', 'date' ),
294
		);
295
		$search->setConditions( $search->combine( '&&', $expr ) );
296
297
		$attributes = $attributeManager->searchItems( $search );
298
299
		if( ( $attrItem = reset( $attributes ) ) === false ) {
300
			throw new \Exception( 'Attribute not found' );
301
		}
302
303
		$attrValues = array( $attrItem->getId() => '2000-01-01' );
304
305
		$this->object->addProduct( $this->testItem->getId(), 1, array(), array(), array(), array(), $attrValues );
306
		$basket = $this->object->get();
307
308
		$this->assertEquals( 1, count( $basket->getProducts() ) );
309
		$this->assertEquals( '2000-01-01', $basket->getProduct( 0 )->getAttribute( 'date', 'custom' ) );
310
	}
311
312
313
	public function testAddProductAttributeNotAssigned()
314
	{
315
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
316
317
		$search = $attributeManager->createSearch();
318
		$expr = array(
319
			$search->compare( '==', 'attribute.code', '30' ),
320
			$search->compare( '==', 'attribute.type.code', 'width' ),
321
		);
322
		$search->setConditions( $search->combine( '&&', $expr ) );
323
324
		$attribute = $attributeManager->searchItems( $search );
325
326
		if( empty( $attribute ) ) {
327
			throw new \Exception( 'Attribute not found' );
328
		}
329
330
		$hiddenAttrIds = array_keys( $attribute );
331
		$configAttrIds = array_keys( $attribute );
332
333
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
334
		$this->object->addProduct( $this->testItem->getId(), 1, array(), array(), $configAttrIds, $hiddenAttrIds );
335
	}
336
337
338
	public function testAddProductNegativeQuantityException()
339
	{
340
		$this->setExpectedException( '\\Aimeos\\MShop\\Order\\Exception' );
341
		$this->object->addProduct( $this->testItem->getId(), -1 );
342
	}
343
344
345
	public function testAddProductNotEnoughStockException()
346
	{
347
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
348
349
		$search = $productManager->createSearch();
350
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
351
352
		$items = $productManager->searchItems( $search );
353
354
		if( ( $item = reset( $items ) ) === false ) {
355
			throw new \Exception( 'Product not found' );
356
		}
357
358
		try
359
		{
360
			$this->object->addProduct( $item->getId(), 5, array(), array(), array(), array(), array(), 'unit_warehouse3' );
361
			throw new \Exception( 'Expected exception not thrown' );
362
		}
363
		catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
364
		{
365
			$item = $this->object->get()->getProduct( 0 );
366
			$this->assertEquals( 3, $item->getQuantity() );
367
		}
368
	}
369
370
371
	public function testAddProductNoStockException()
372
	{
373
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
374
375
		$search = $productManager->createSearch();
376
		$search->setConditions( $search->compare( '==', 'product.code', 'EFGH' ) );
377
378
		$items = $productManager->searchItems( $search );
379
380
		if( ( $item = reset( $items ) ) === false ) {
381
			throw new \Exception( 'Product not found' );
382
		}
383
384
		try
385
		{
386
			$this->object->addProduct( $item->getId(), 5, array(), array(), array(), array(), array(), 'unit_warehouse2' );
387
			throw new \Exception( 'Expected exception not thrown' );
388
		}
389
		catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
390
		{
391
			$this->assertEquals( array(), $this->object->get()->getProducts() );
392
		}
393
	}
394
395
396
	public function testAddProductNoStockRequired()
397
	{
398
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
399
400
		$search = $productManager->createSearch();
401
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
402
403
		$items = $productManager->searchItems( $search );
404
405
		if( ( $item = reset( $items ) ) === false ) {
406
			throw new \Exception( 'Product not found' );
407
		}
408
409
		$this->object->addProduct( $item->getId(), 5, array( 'stock' => false ) );
410
	}
411
412
413
	public function testAddProductNoStockItem()
414
	{
415
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
416
417
		$search = $productManager->createSearch();
418
		$search->setConditions( $search->compare( '==', 'product.code', 'QRST' ) );
419
420
		$items = $productManager->searchItems( $search );
421
422
		if( ( $item = reset( $items ) ) === false ) {
423
			throw new \Exception( 'Product not found' );
424
		}
425
426
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
427
		$this->object->addProduct( $item->getId(), 1 );
428
	}
429
430
431
	public function testAddProductNoPriceException()
432
	{
433
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
434
435
		$search = $productManager->createSearch();
436
		$search->setConditions( $search->compare( '==', 'product.code', 'MNOP' ) );
437
438
		$items = $productManager->searchItems( $search );
439
440
		if( ( $item = reset( $items ) ) === false ) {
441
			throw new \Exception( 'Product not found' );
442
		}
443
444
		$this->setExpectedException( '\\Aimeos\\MShop\\Price\\Exception' );
445
		$this->object->addProduct( $item->getId(), 1 );
446
	}
447
448
449
	public function testAddProductConfigAttributeException()
450
	{
451
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
452
		$this->object->addProduct( $this->testItem->getId(), 1, array(), array(), array( -1 ) );
453
	}
454
455
456
	public function testAddProductEmptySelectionException()
457
	{
458
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
459
		$search = $productManager->createSearch();
460
		$search->setConditions( $search->compare( '==', 'product.code', 'U:noSel' ) );
461
		$items = $productManager->searchItems( $search );
462
463
		if( ( $item = reset( $items ) ) === false ) {
464
			throw new \Exception( 'Product not found' );
465
		}
466
467
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
468
		$this->object->addProduct( $item->getId(), 1 );
469
	}
470
471
472
	public function testAddProductSelectionWithPricelessItem()
473
	{
474
		$this->object->addProduct( $this->testItem->getId(), 1 );
475
476
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
477
		$search = $productManager->createSearch();
478
479
		$search->setConditions( $search->compare( '==', 'product.code', 'U:TESTPSUB01' ) );
480
		$items = $productManager->searchItems( $search );
481
482
		if( ( $item = reset( $items ) ) === false ) {
483
			throw new \Exception( 'Product not found' );
484
		}
485
486
		$this->assertEquals( 'U:TESTPSUB01', $this->object->get()->getProduct( 0 )->getProductCode() );
487
	}
488
489
490
	public function testAddProductLowQuantityPriceException()
491
	{
492
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
493
		$search = $productManager->createSearch();
494
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
495
		$items = $productManager->searchItems( $search );
496
497
		if( ( $item = reset( $items ) ) === false ) {
498
			throw new \Exception( 'Product not found' );
499
		}
500
501
		$this->setExpectedException( '\\Aimeos\\MShop\\Price\\Exception' );
502
		$this->object->addProduct( $item->getId(), 1 );
503
	}
504
505
506
	public function testAddProductHigherQuantities()
507
	{
508
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
509
		$search = $productManager->createSearch();
510
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
511
		$items = $productManager->searchItems( $search );
512
513
		if( ( $item = reset( $items ) ) === false ) {
514
			throw new \Exception( 'Product not found' );
515
		}
516
517
		$this->object->addProduct( $item->getId(), 2, array(), array(), array(), array(), array(), 'unit_warehouse3' );
518
519
		$this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() );
520
		$this->assertEquals( 'IJKL', $this->object->get()->getProduct( 0 )->getProductCode() );
521
	}
522
523
524
	public function testDeleteProductFlagError()
525
	{
526
		$this->object->addProduct( $this->testItem->getId(), 2 );
527
528
		$item = $this->object->get()->getProduct( 0 );
529
		$item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
530
531
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
532
		$this->object->deleteProduct( 0 );
533
	}
534
535
536
	public function testEditProduct()
537
	{
538
		$this->object->addProduct( $this->testItem->getId(), 1 );
539
540
		$item = $this->object->get()->getProduct( 0 );
541
		$this->assertEquals( 1, $item->getQuantity() );
542
543
		$this->object->editProduct( 0, 4 );
544
545
		$item = $this->object->get()->getProduct( 0 );
546
		$this->assertEquals( 4, $item->getQuantity() );
547
		$this->assertEquals( 'U:TESTPSUB01', $item->getProductCode() );
548
	}
549
550
551
	public function testEditProductAttributes()
552
	{
553
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
554
		$search = $productManager->createSearch();
555
		$search->setConditions( $search->compare( '==', 'product.code', 'U:TESTP' ) );
556
		$items = $productManager->searchItems( $search );
557
558
		if( ( $item = reset( $items ) ) === false ) {
559
			throw new \Exception( 'Product not found' );
560
		}
561
562
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
563
		$search = $attributeManager->createSearch();
564
		$conditions = array(
565
			$search->compare( '==', 'attribute.domain', 'product' ),
566
			$search->combine( '||', array(
567
				$search->combine( '&&', array(
568
					$search->compare( '==', 'attribute.code', 'xs' ),
569
					$search->compare( '==', 'attribute.type.code', 'size' ),
570
				) ),
571
				$search->combine( '&&', array(
572
					$search->compare( '==', 'attribute.code', 'white' ),
573
					$search->compare( '==', 'attribute.type.code', 'color' ),
574
				) ),
575
			) )
576
		);
577
		$search->setConditions( $search->combine( '&&', $conditions ) );
578
		$attributes = $attributeManager->searchItems( $search );
579
580
		if( ( $attribute = reset( $attributes ) ) === false ) {
581
			throw new \Exception( 'No attributes available' );
582
		}
583
584
585
		$this->object->addProduct( $item->getId(), 1, array(), array(), array_keys( $attributes ) );
586
		$this->object->editProduct( 0, 4 );
587
588
		$item = $this->object->get()->getProduct( 0 );
589
		$this->assertEquals( 2, count( $item->getAttributes() ) );
590
		$this->assertEquals( 4, $item->getQuantity() );
591
592
593
		$this->object->editProduct( 0, 3, array(), array( $attribute->getType() ) );
594
595
		$item = $this->object->get()->getProduct( 0 );
596
		$this->assertEquals( 3, $item->getQuantity() );
597
		$this->assertEquals( 1, count( $item->getAttributes() ) );
598
		$this->assertEquals( 'U:TESTPSUB01', $item->getProductCode() );
599
	}
600
601
602
	public function testEditProductNotEnoughStock()
603
	{
604
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
605
		$search = $productManager->createSearch();
606
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
607
		$items = $productManager->searchItems( $search );
608
609
		if( ( $item = reset( $items ) ) === false ) {
610
			throw new \Exception( 'Product not found' );
611
		}
612
613
		$this->object->addProduct( $item->getId(), 2, array(), array(), array(), array(), array(), 'unit_warehouse3' );
614
615
		$item = $this->object->get()->getProduct( 0 );
616
		$this->assertEquals( 2, $item->getQuantity() );
617
618
		try
619
		{
620
			$this->object->editProduct( 0, 5 );
621
			throw new \Exception( 'Expected exception not thrown' );
622
		}
623
		catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
624
		{
625
			$item = $this->object->get()->getProduct( 0 );
626
			$this->assertEquals( 3, $item->getQuantity() );
627
			$this->assertEquals( 'IJKL', $item->getProductCode() );
628
		}
629
	}
630
631
632
	public function testEditProductNoStock()
633
	{
634
		$context = \TestHelperFrontend::getContext();
635
636
		$productManager = \Aimeos\MShop\Factory::createManager( $context, 'product' );
637
		$search = $productManager->createSearch();
638
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
639
		$items = $productManager->searchItems( $search );
640
641
		if( ( $item = reset( $items ) ) === false ) {
642
			throw new \Exception( 'Product not found' );
643
		}
644
645
		$orderProductManager = \Aimeos\MShop\Factory::createManager( $context, 'order/base/product' );
646
		$orderProductItem = $orderProductManager->createItem();
647
		$orderProductItem->copyFrom( $item );
648
		$orderProductItem->setQuantity( 2 );
649
		$orderProductItem->setWarehouseCode( 'unit_warehouse3' );
650
651
		$pos = $this->object->get()->addProduct( $orderProductItem, 1 );
652
653
		$item = $this->object->get()->getProduct( $pos );
654
		$this->assertEquals( 2, $item->getQuantity() );
655
656
		try
657
		{
658
			$this->object->editProduct( $pos, 5 );
659
			throw new \Exception( 'Expected exception not thrown' );
660
		}
661
		catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
662
		{
663
			$this->assertEquals( 3, $this->object->get()->getProduct( $pos )->getQuantity() );
664
		}
665
	}
666
667
668
	public function testEditProductStockNotChecked()
669
	{
670
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
671
		$search = $productManager->createSearch();
672
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
673
		$items = $productManager->searchItems( $search );
674
675
		if( ( $item = reset( $items ) ) === false ) {
676
			throw new \Exception( 'Product not found' );
677
		}
678
679
		$this->object->addProduct( $item->getId(), 2, array(), array(), array(), array(), array(), 'unit_warehouse3' );
680
681
		$item = $this->object->get()->getProduct( 0 );
682
		$this->assertEquals( 2, $item->getQuantity() );
683
684
		$this->object->editProduct( 0, 5, array( 'stock' => false ) );
685
686
		$item = $this->object->get()->getProduct( 0 );
687
		$this->assertEquals( 5, $item->getQuantity() );
688
		$this->assertEquals( 'IJKL', $item->getProductCode() );
689
	}
690
691
692
	public function testEditProductFlagError()
693
	{
694
		$this->object->addProduct( $this->testItem->getId(), 2 );
695
696
		$item = $this->object->get()->getProduct( 0 );
697
		$item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
698
699
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
700
		$this->object->editProduct( 0, 4 );
701
	}
702
703
704
	public function testAddCoupon()
705
	{
706
		$this->object->addProduct( $this->testItem->getId(), 2 );
707
		$this->object->addCoupon( 'GHIJ' );
708
709
		$basket = $this->object->get();
710
711
		$this->assertEquals( 1, count( $basket->getCoupons() ) );
712
	}
713
714
715
	public function testAddCouponInvalidCode()
716
	{
717
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
718
		$this->object->addCoupon( 'invalid' );
719
	}
720
721
722
	public function testAddCouponMissingRequirements()
723
	{
724
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
725
		$this->object->addCoupon( 'OPQR' );
726
	}
727
728
729
	public function testDeleteCoupon()
730
	{
731
		$this->object->addProduct( $this->testItem->getId(), 2 );
732
		$this->object->addCoupon( '90AB' );
733
		$this->object->deleteCoupon( '90AB' );
734
735
		$basket = $this->object->get();
736
737
		$this->assertEquals( 0, count( $basket->getCoupons() ) );
738
	}
739
740
741
	public function testClear()
742
	{
743
		$this->object->addProduct( $this->testItem->getId(), 2 );
744
		$this->object->clear();
745
746
		$this->assertEquals( 0, count( $this->object->get()->getProducts() ) );
747
	}
748
749
750
	public function testSetAddressDelete()
751
	{
752
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, null );
753
754
		$this->setExpectedException( '\Aimeos\MShop\Order\Exception' );
755
		$this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT );
756
	}
757
758
759
	public function testSetBillingAddressByItem()
760
	{
761
		$item = $this->getAddress( 'Example company' );
762
763
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, $item );
764
765
		$address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT );
766
		$this->assertEquals( 'Example company', $address->getCompany() );
767
	}
768
769
770
	public function testSetBillingAddressByArray()
771
	{
772
		$fixture = array(
773
			'order.base.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>',
774
			'order.base.address.vatid' => 'DE999999999',
775
			'order.base.address.title' => '<br/>Dr.',
776
			'order.base.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR,
777
			'order.base.address.firstname' => 'firstunit',
778
			'order.base.address.lastname' => 'lastunit',
779
			'order.base.address.address1' => 'unit str.',
780
			'order.base.address.address2' => ' 166',
781
			'order.base.address.address3' => '4.OG',
782
			'order.base.address.postal' => '22769',
783
			'order.base.address.city' => 'Hamburg',
784
			'order.base.address.state' => 'Hamburg',
785
			'order.base.address.countryid' => 'de',
786
			'order.base.address.languageid' => 'de',
787
			'order.base.address.telephone' => '05554433221',
788
			'order.base.address.email' => '[email protected]',
789
			'order.base.address.telefax' => '05554433222',
790
			'order.base.address.website' => 'www.example.com',
791
			'order.base.address.flag' => 0,
792
		);
793
794
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, $fixture );
795
796
		$address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT );
797
		$this->assertEquals( 'Example company', $address->getCompany() );
798
		$this->assertEquals( 'Dr.', $address->getTitle() );
799
		$this->assertEquals( 'firstunit', $address->getFirstname() );
800
	}
801
802
803
	public function testSetBillingAddressByArrayError()
804
	{
805
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
806
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, array( 'error' => false ) );
807
	}
808
809
810
	public function testSetBillingAddressParameterError()
811
	{
812
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
813
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, 'error' );
814
	}
815
816
817
	public function testSetDeliveryAddressByItem()
818
	{
819
		$item = $this->getAddress( 'Example company' );
820
821
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY, $item );
822
823
		$address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY );
824
		$this->assertEquals( 'Example company', $address->getCompany() );
825
	}
826
827
828
	public function testSetDeliveryAddressByArray()
829
	{
830
		$fixture = array(
831
			'order.base.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>',
832
			'order.base.address.vatid' => 'DE999999999',
833
			'order.base.address.title' => '<br/>Dr.',
834
			'order.base.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR,
835
			'order.base.address.firstname' => 'firstunit',
836
			'order.base.address.lastname' => 'lastunit',
837
			'order.base.address.address1' => 'unit str.',
838
			'order.base.address.address2' => ' 166',
839
			'order.base.address.address3' => '4.OG',
840
			'order.base.address.postal' => '22769',
841
			'order.base.address.city' => 'Hamburg',
842
			'order.base.address.state' => 'Hamburg',
843
			'order.base.address.countryid' => 'de',
844
			'order.base.address.languageid' => 'de',
845
			'order.base.address.telephone' => '05554433221',
846
			'order.base.address.email' => '[email protected]',
847
			'order.base.address.telefax' => '05554433222',
848
			'order.base.address.website' => 'www.example.com',
849
			'order.base.address.flag' => 0,
850
		);
851
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY, $fixture );
852
853
		$address = $this->object->get()->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY );
854
		$this->assertEquals( 'Example company', $address->getCompany() );
855
		$this->assertEquals( 'Dr.', $address->getTitle() );
856
		$this->assertEquals( 'firstunit', $address->getFirstname() );
857
	}
858
859
860
	public function testSetDeliveryAddressByArrayError()
861
	{
862
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
863
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY, array( 'error' => false ) );
864
	}
865
866
867
	public function testSetDeliveryAddressTypeError()
868
	{
869
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
870
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY, 'error' );
871
	}
872
873
874
	public function testSetServicePayment()
875
	{
876
		$service = $this->getService( 'unitpaymentcode' );
877
878
		$this->object->setService( 'payment', $service->getId(), array() );
879
		$this->assertEquals( 'unitpaymentcode', $this->object->get()->getService( 'payment' )->getCode() );
880
881
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
882
		$this->object->setService( 'payment', $service->getId(), array( 'prepay' => true ) );
883
	}
884
885
886
	public function testSetDeliveryOption()
887
	{
888
		$service = $this->getService( 'unitcode' );
889
890
		$this->object->setService( 'delivery', $service->getId(), array() );
891
		$this->assertEquals( 'unitcode', $this->object->get()->getService( 'delivery' )->getCode() );
892
893
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
894
		$this->object->setService( 'delivery', $service->getId(), array( 'fast shipping' => true, 'air shipping' => false ) );
895
	}
896
897
898
	public function testCheckLocale()
899
	{
900
		$this->object->addProduct( $this->testItem->getId(), 2 );
901
		$this->object->addCoupon( 'OPQR' );
902
903
		$this->object->setService( 'payment', $this->getService( 'unitpaymentcode' )->getId() );
904
		$this->object->setService( 'delivery', $this->getService( 'unitcode' )->getId() );
905
906
		$basket = $this->object->get();
907
		$price = $basket->getPrice();
908
909
		foreach( $basket->getProducts() as $product )
910
		{
911
			$this->assertEquals( 2, $product->getQuantity() );
912
			$product->getPrice()->setCurrencyId( 'CHF' );
913
		}
914
915
		$basket->getService( 'delivery' )->getPrice()->setCurrencyId( 'CHF' );
916
		$basket->getService( 'payment' )->getPrice()->setCurrencyId( 'CHF' );
917
		$basket->getLocale()->setCurrencyId( 'CHF' );
918
		$price->setCurrencyId( 'CHF' );
919
920
		$this->context->getLocale()->setCurrencyId( 'CHF' );
921
		$this->object->setAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, $this->getAddress( 'Example company' ) );
922
923
		$this->context->getSession()->set( 'aimeos/basket/currency', 'CHF' );
924
		$this->context->getLocale()->setCurrencyId( 'EUR' );
925
926
		$this->context->getSession()->set( 'aimeos/basket/content-unittest-en-EUR-', null );
927
928
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
929
		$basket = $object->get();
930
931
		foreach( $basket->getProducts() as $product )
932
		{
933
			$this->assertEquals( 'EUR', $product->getPrice()->getCurrencyId() );
934
			$this->assertEquals( 2, $product->getQuantity() );
935
		}
936
937
		$this->assertEquals( 'EUR', $basket->getService( 'payment' )->getPrice()->getCurrencyId() );
938
		$this->assertEquals( 'EUR', $basket->getService( 'delivery' )->getPrice()->getCurrencyId() );
939
		$this->assertEquals( 'EUR', $basket->getLocale()->getCurrencyId() );
940
		$this->assertEquals( 'EUR', $basket->getPrice()->getCurrencyId() );
941
	}
942
943
944
	/**
945
	 * @param string $company
946
	 */
947
	protected function getAddress( $company )
948
	{
949
		$customer = \Aimeos\MShop\Customer\Manager\Factory::createManager( \TestHelperFrontend::getContext(), 'Standard' );
950
		$addressManager = $customer->getSubManager( 'address', 'Standard' );
951
952
		$search = $addressManager->createSearch();
953
		$search->setConditions( $search->compare( '==', 'customer.address.company', $company ) );
954
		$items = $addressManager->searchItems( $search );
955
956
		if( ( $item = reset( $items ) ) === false ) {
957
			throw new \Exception( sprintf( 'No address item with company "%1$s" found', $company ) );
958
		}
959
960
		return $item;
961
	}
962
963
964
	/**
965
	 * @param string $code
966
	 */
967
	protected function getService( $code )
968
	{
969
		$serviceManager = \Aimeos\MShop\Service\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
970
971
		$search = $serviceManager->createSearch();
972
		$search->setConditions( $search->compare( '==', 'service.code', $code ) );
973
974
		$result = $serviceManager->searchItems( $search, array( 'text' ) );
975
976
		if( ( $item = reset( $result ) ) === false ) {
977
			throw new \Exception( sprintf( 'No service item with code "%1$s" found', $code ) );
978
		}
979
980
		return $item;
981
	}
982
}
983