Completed
Push — master ( eec493...b2acb8 )
by Aimeos
02:39
created

StandardTest::testAddProductOptionalParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 */
8
9
namespace Aimeos\Controller\Frontend\Basket;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
{
14
	private $object;
15
	private $context;
16
	private $testItem;
17
18
19
	protected function setUp()
20
	{
21
		\Aimeos\MShop::cache( true );
22
23
		$this->context = \TestHelperFrontend::getContext();
24
25
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
26
		$this->testItem = $manager->findItem( 'U:TESTP', ['attribute', 'media', 'price', 'product', 'text'] );
27
28
		$this->object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
29
	}
30
31
32
	protected function tearDown()
33
	{
34
		\Aimeos\MShop::cache( false );
35
36
		$this->object->clear();
37
		$this->context->getSession()->set( 'aimeos', [] );
38
39
		unset( $this->context, $this->object );
40
	}
41
42
43
	public function testAdd()
44
	{
45
		$result = $this->object->add( ['order.base.comment' => 'test'] );
46
47
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
48
		$this->assertEquals( 'test', $this->object->get()->getComment() );
49
	}
50
51
52
	public function testClear()
53
	{
54
		$this->object->addProduct( $this->testItem );
55
56
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $this->object->clear() );
57
		$this->assertEquals( 0, count( $this->object->get()->getProducts() ) );
58
	}
59
60
61
	public function testGet()
62
	{
63
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $this->object->get() );
64
	}
65
66
67
	public function testSave()
68
	{
69
		$stub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Base\Standard::class )
70
			->setConstructorArgs( [$this->context] )
71
			->setMethods( ['setSession'] )
72
			->getMock();
73
74
		\Aimeos\MShop::inject( 'order/base', $stub );
75
76
		$stub->expects( $this->exactly( 2 ) )->method( 'setSession' );
77
78
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
79
		$object->addProduct( $this->testItem );
80
81
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $object->save() );
82
	}
83
84
85
	public function testSetType()
86
	{
87
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $this->object->setType( 'test' ) );
88
	}
89
90
91
	public function testLoad()
92
	{
93
		$stub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Base\Standard::class )
94
			->setConstructorArgs( [$this->context] )
95
			->setMethods( ['load'] )
96
			->getMock();
97
98
		\Aimeos\MShop::inject( 'order/base', $stub );
99
100
		$stub->expects( $this->once() )->method( 'load' )
101
			->will( $this->returnValue( $stub->createItem() ) );
102
103
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
104
105
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $object->load( -1 ) );
106
	}
107
108
109
	public function testStore()
110
	{
111
		$stub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Base\Standard::class )
112
			->setConstructorArgs( [$this->context] )
113
			->setMethods( ['store'] )
114
			->getMock();
115
116
		\Aimeos\MShop::inject( 'order/base', $stub );
117
118
		$priceManager = \Aimeos\MShop::create( $this->context, 'price' );
119
120
		$basket = $this->getMockBuilder( \Aimeos\MShop\Order\Item\Base\Standard::class )
121
			->setConstructorArgs( [$priceManager->createItem(), $this->context->getLocale()] )
122
			->setMethods( ['check'] )
123
			->getMock();
124
125
		$object = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
126
			->setConstructorArgs( [$this->context] )
127
			->setMethods( ['get'] )
128
			->getMock();
129
130
		$object->expects( $this->once() )->method( 'get' )->will( $this->returnValue( $basket ) );
131
		$basket->expects( $this->once() )->method( 'check' )->will( $this->returnValue( $basket ) );
132
		$stub->expects( $this->once() )->method( 'store' )->will( $this->returnValue( $basket ) );
133
134
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $object->store() );
135
	}
136
137
138
	public function testStoreLimit()
139
	{
140
		$this->context->setEditor( 'core:lib/mshoplib' );
141
		$config = $this->context->getConfig();
142
		$config->set( 'controller/frontend/basket/limit-count', 0 );
143
		$config->set( 'controller/frontend/basket/limit-seconds', 86400 * 365 );
144
145
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
146
147
		$this->setExpectedException( \Aimeos\Controller\Frontend\Basket\Exception::class );
148
		$object->store();
149
	}
150
151
152
	public function testAddDeleteProduct()
153
	{
154
		$basket = $this->object->get();
155
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
156
		$item = $manager->findItem( 'CNC', ['attribute', 'media', 'price', 'product', 'text'] );
157
158
		$result1 = $this->object->addProduct( $item, 2, [], [], [], 'default', 'unitsupplier' );
159
		$item2 = $this->object->get()->getProduct( 0 );
160
		$result2 = $this->object->deleteProduct( 0 );
161
162
		$this->assertEquals( 0, count( $basket->getProducts() ) );
163
		$this->assertEquals( 'CNC', $item2->getProductCode() );
164
		$this->assertEquals( 'default', $item2->getStockType() );
165
		$this->assertEquals( 'unitsupplier', $item2->getSupplierCode() );
166
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result1 );
167
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result2 );
168
	}
169
170
171
	public function testAddProductCustomAttribute()
172
	{
173
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'custom', [], 'product', 'date' );
174
		$attrValues = [$attrItem->getId() => '2000-01-01'];
175
176
		$result = $this->object->addProduct( $this->testItem, 1, [], [], $attrValues );
177
		$basket = $this->object->get();
178
179
		$this->assertEquals( 1, count( $basket->getProducts() ) );
180
		$this->assertEquals( '2000-01-01', $basket->getProduct( 0 )->getAttribute( 'date', 'custom' ) );
181
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
182
	}
183
184
185
	public function testAddProductCustomPrice()
186
	{
187
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'custom', [], 'product', 'price' );
188
		$attrValues = [$attrItem->getId() => '0.01'];
189
190
		$result = $this->object->addProduct( $this->testItem, 1, [], [], $attrValues );
191
		$basket = $this->object->get();
192
193
		$this->assertEquals( 1, count( $basket->getProducts() ) );
194
		$this->assertEquals( '0.01', $basket->getProduct( 0 )->getPrice()->getValue() );
195
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
196
	}
197
198
199
	public function testAddProductCustomPriceException()
200
	{
201
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'custom', [], 'product', 'price' );
202
		$attrValues = [$attrItem->getId() => ','];
203
204
		$this->setExpectedException( \Aimeos\Controller\Frontend\Basket\Exception::class );
205
		$this->object->addProduct( $this->testItem, 1, [], [], $attrValues );
206
	}
207
208
209
	public function testAddProductAttributePrice()
210
	{
211
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( 'xs', [], 'product', 'size' );
212
213
		$result = $this->object->addProduct( $this->testItem, 1, [], [$attrItem->getId() => 2] );
214
215
		$this->assertEquals( '43.90', $this->object->get()->getPrice()->getValue() );
216
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
217
	}
218
219
220
	public function testAddProductAttributeNotAssigned()
221
	{
222
		$attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->findItem( '30', [], 'product', 'width' );
223
		$ids = [$attrItem->getId()];
224
225
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
226
		$this->object->addProduct( $this->testItem, 1, [], $ids, $ids );
227
	}
228
229
230
	public function testAddProductNegativeQuantityException()
231
	{
232
		$this->setExpectedException( '\\Aimeos\\MShop\\Order\\Exception' );
233
		$this->object->addProduct( $this->testItem, -1 );
234
	}
235
236
237
	public function testAddProductNoPriceException()
238
	{
239
		$item = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'MNOP' );
240
241
		$this->setExpectedException( '\\Aimeos\\MShop\\Price\\Exception' );
242
		$this->object->addProduct( $item );
243
	}
244
245
246
	public function testAddProductConfigAttributeException()
247
	{
248
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
249
		$this->object->addProduct( $this->testItem, 1, [], [-1] );
250
	}
251
252
253
	public function testAddProductLowQuantityPriceException()
254
	{
255
		$item = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'IJKL', ['attribute', 'price'] );
256
257
		$this->setExpectedException( '\\Aimeos\\MShop\\Price\\Exception' );
258
		$this->object->addProduct( $item );
259
	}
260
261
262
	public function testAddProductHigherQuantities()
263
	{
264
		$item = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'IJKL' );
265
266
		$result = $this->object->addProduct( $item, 2 );
267
268
		$this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() );
269
		$this->assertEquals( 'IJKL', $this->object->get()->getProduct( 0 )->getProductCode() );
270
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
271
	}
272
273
274
	public function testAddProductOptionalParameters()
275
	{
276
		$item = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'IJKL' );
277
278
		$product = $this->object->addProduct( $item, 2, [], [], [], 'stock', 'supplier', 123 )->get()->getProduct( 0 );
279
280
		$this->assertEquals( 'stock', $product->getStockType() );
281
		$this->assertEquals( 'supplier', $product->getSupplierCode() );
282
		$this->assertEquals( 123, $product->getSiteId() );
283
	}
284
285
286
	public function testDeleteProductFlagError()
287
	{
288
		$this->object->addProduct( $this->testItem, 2 );
289
290
		$item = $this->object->get()->getProduct( 0 );
291
		$item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
292
293
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
294
		$this->object->deleteProduct( 0 );
295
	}
296
297
298
	public function testUpdateProduct()
299
	{
300
		$this->object->addProduct( $this->testItem );
301
302
		$item = $this->object->get()->getProduct( 0 );
303
		$this->assertEquals( 1, $item->getQuantity() );
304
305
		$result = $this->object->updateProduct( 0, 4 );
306
		$item = $this->object->get()->getProduct( 0 );
307
308
		$this->assertEquals( 4, $item->getQuantity() );
309
		$this->assertEquals( 'U:TESTP', $item->getProductCode() );
310
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
311
	}
312
313
314
	public function testUpdateProductFlagError()
315
	{
316
		$this->object->addProduct( $this->testItem, 2 );
317
318
		$item = $this->object->get()->getProduct( 0 );
319
		$item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
320
321
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
322
		$this->object->updateProduct( 0, 4 );
323
	}
324
325
326
	public function testAddCoupon()
327
	{
328
		$this->object->addProduct( $this->testItem, 2 );
329
330
		$result = $this->object->addCoupon( 'GHIJ' );
331
		$basket = $this->object->get();
332
333
		$this->assertEquals( 1, count( $basket->getCoupons() ) );
334
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
335
	}
336
337
338
	public function testAddCouponExceedCount()
339
	{
340
		$this->object->addProduct( $this->testItem, 2 );
341
		$this->object->addCoupon( 'GHIJ' );
342
343
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
344
		$this->object->addCoupon( 'GHIJ' );
345
	}
346
347
348
	public function testAddCouponInvalidCode()
349
	{
350
		$this->setExpectedException( \Aimeos\MShop\Plugin\Provider\Exception::class );
351
		$this->object->addCoupon( 'invalid' );
352
	}
353
354
355
	public function testDeleteCoupon()
356
	{
357
		$this->object->addProduct( $this->testItem, 2 );
358
		$this->object->addCoupon( '90AB' );
359
360
		$result = $this->object->deleteCoupon( '90AB' );
361
		$basket = $this->object->get();
362
363
		$this->assertEquals( 0, count( $basket->getCoupons() ) );
364
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
365
	}
366
367
368
	public function testAddAddress()
369
	{
370
		$values = 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
		);
390
391
		$result = $this->object->addAddress( 'payment', $values );
392
		$address = $this->object->get()->getAddress( 'payment', 0 );
393
394
		$this->assertEquals( 'Example company', $address->getCompany() );
395
		$this->assertEquals( 'Dr.', $address->getTitle() );
396
		$this->assertEquals( 'firstunit', $address->getFirstname() );
397
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
398
	}
399
400
401
	public function testDeleteAddress()
402
	{
403
		$this->object->addAddress( 'payment', [] );
404
		$this->assertEquals( 1, count( $this->object->get()->getAddress( 'payment') ) );
405
406
		$result = $this->object->deleteAddress( 'payment' );
407
408
		$this->assertEquals( 0, count( $this->object->get()->getAddress( 'payment') ) );
409
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result );
410
	}
411
412
413
414
	public function testAddServicePayment()
415
	{
416
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
417
		$service = $manager->findItem( 'unitpaymentcode', [], 'service', 'payment' );
418
419
		$this->object->addService( $service );
420
		$item = $this->object->get()->getService( 'payment', 0 )->getCode();
421
		$this->assertEquals( 'unitpaymentcode', $item );
422
423
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
424
		$this->object->addService( $service, ['prepay' => true] );
425
	}
426
427
428
	public function testAddServiceDelivery()
429
	{
430
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
431
		$service = $manager->findItem( 'unitcode', [], 'service', 'delivery' );
432
433
		$this->object->addService( $service );
434
		$item = $this->object->get()->getService( 'delivery', 0 );
435
		$this->assertEquals( 'unitcode', $item->getCode() );
436
437
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
438
		$this->object->addService( $service, ['fast shipping' => true, 'air shipping' => false] );
439
	}
440
441
442
	public function testDeleteServices()
443
	{
444
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
445
		$service = $manager->findItem( 'unitcode', [], 'service', 'delivery' );
446
447
		$this->assertSame( $this->object, $this->object->addService( $service ) );
448
		$this->assertEquals( 'unitcode', $this->object->get()->getService( 'delivery', 0 )->getCode() );
449
450
		$this->assertSame( $this->object, $this->object->deleteService( 'delivery' ) );
451
		$this->assertEquals( 0, count( $this->object->get()->getService( 'delivery' ) ) );
452
	}
453
454
455
	public function testDeleteServicePosition()
456
	{
457
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
458
		$service = $manager->findItem( 'unitcode', [], 'service', 'delivery' );
459
460
		$this->assertSame( $this->object, $this->object->addService( $service ) );
461
		$this->assertEquals( 'unitcode', $this->object->get()->getService( 'delivery', 0 )->getCode() );
462
463
		$this->assertSame( $this->object, $this->object->deleteService( 'delivery', 0 ) );
464
		$this->assertEquals( 0, count( $this->object->get()->getService( 'delivery' ) ) );
465
	}
466
467
468
	public function testCheckLocale()
469
	{
470
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
471
		$payment = $manager->findItem( 'unitpaymentcode', [], 'service', 'payment' );
472
		$delivery = $manager->findItem( 'unitcode', [], 'service', 'delivery' );
473
474
		$this->object->addProduct( $this->testItem, 2 );
475
		$this->object->addCoupon( 'OPQR' );
476
477
		$this->object->addService( $payment );
478
		$this->object->addService( $delivery );
479
480
		$basket = $this->object->get();
481
		$price = $basket->getPrice();
482
483
		foreach( $basket->getProducts() as $product )
484
		{
485
			$this->assertEquals( 2, $product->getQuantity() );
486
			$product->getPrice()->setCurrencyId( 'CHF' );
487
		}
488
489
		$basket->getService( 'delivery', 0 )->getPrice()->setCurrencyId( 'CHF' );
490
		$basket->getService( 'payment', 0 )->getPrice()->setCurrencyId( 'CHF' );
491
		$basket->getLocale()->setCurrencyId( 'CHF' );
492
		$price->setCurrencyId( 'CHF' );
493
494
		$this->context->getLocale()->setCurrencyId( 'CHF' );
495
		$this->object->addAddress( 'payment', $this->getAddress( 'Example company' )->toArray() );
496
497
		$this->context->getSession()->set( 'aimeos/basket/currency', 'CHF' );
498
		$this->context->getLocale()->setCurrencyId( 'EUR' );
499
500
		$this->context->getSession()->set( 'aimeos/basket/content-unittest-en-EUR-', null );
501
502
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
503
		$basket = $object->get();
504
505
		foreach( $basket->getProducts() as $product )
506
		{
507
			$this->assertEquals( 'EUR', $product->getPrice()->getCurrencyId() );
508
			$this->assertEquals( 2, $product->getQuantity() );
509
		}
510
511
		$this->assertEquals( 'EUR', $basket->getService( 'payment', 0 )->getPrice()->getCurrencyId() );
512
		$this->assertEquals( 'EUR', $basket->getService( 'delivery', 0 )->getPrice()->getCurrencyId() );
513
		$this->assertEquals( 'EUR', $basket->getLocale()->getCurrencyId() );
514
		$this->assertEquals( 'EUR', $basket->getPrice()->getCurrencyId() );
515
	}
516
517
518
	/**
519
	 * @param string $company
520
	 */
521
	protected function getAddress( $company )
522
	{
523
		$customer = \Aimeos\MShop\Customer\Manager\Factory::create( \TestHelperFrontend::getContext(), 'Standard' );
524
		$addressManager = $customer->getSubManager( 'address', 'Standard' );
525
526
		$search = $addressManager->createSearch();
527
		$search->setConditions( $search->compare( '==', 'customer.address.company', $company ) );
528
		$items = $addressManager->searchItems( $search );
529
530
		if( ( $item = reset( $items ) ) === false ) {
531
			throw new \RuntimeException( sprintf( 'No address item with company "%1$s" found', $company ) );
532
		}
533
534
		return $item;
535
	}
536
}
537