Passed
Push — master ( ed6c8e...268125 )
by Aimeos
03:53
created

StandardTest::testGetBodyAddVariantAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 36
rs 9.536
c 0
b 0
f 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-2021
7
 */
8
9
10
namespace Aimeos\Client\Html\Basket\Standard;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $context;
17
18
19
	protected function setUp() : void
20
	{
21
		$this->context = \TestHelperHtml::getContext();
22
23
		$view = \TestHelperHtml::getView();
24
		$view->standardBasket = \Aimeos\MShop::create( $this->context, 'order/base' )->create();
25
26
		$this->object = new \Aimeos\Client\Html\Basket\Standard\Standard( $this->context );
27
		$this->object->setView( $view );
28
	}
29
30
31
	protected function tearDown() : void
32
	{
33
		\Aimeos\Controller\Frontend\Basket\Factory::create( $this->context )->clear();
34
		unset( $this->object );
35
	}
36
37
38
	public function testHeader()
39
	{
40
		$tags = [];
41
		$expire = null;
42
43
		$this->object->setView( $this->object->data( $this->object->getView(), $tags, $expire ) );
44
		$output = $this->object->header();
45
46
		$this->assertStringContainsString( '<title>Basket | Aimeos</title>', $output );
47
	}
48
49
50
	public function testHeaderException()
51
	{
52
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Basket\Standard\Standard::class )
53
			->setConstructorArgs( [$this->context] )
54
			->setMethods( array( 'data' ) )
55
			->getMock();
56
57
		$mock->setView( \TestHelperHtml::getView() );
58
59
		$mock->expects( $this->once() )->method( 'data' )
60
			->will( $this->throwException( new \RuntimeException() ) );
61
62
		$mock->header();
63
	}
64
65
66
	public function testBody()
67
	{
68
		$output = $this->object->body();
69
70
		$this->assertStringStartsWith( '<section class="aimeos basket-standard"', $output );
71
		$this->assertStringContainsString( '<div class="common-summary-detail', $output );
72
		$this->assertStringContainsString( '<div class="basket-standard-coupon', $output );
73
	}
74
75
76
	public function testBodyClientHtmlException()
77
	{
78
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Basket\Standard\Standard::class )
79
			->setConstructorArgs( [$this->context] )
80
			->setMethods( array( 'data' ) )
81
			->getMock();
82
83
		$mock->setView( \TestHelperHtml::getView() );
84
85
		$mock->expects( $this->once() )->method( 'data' )
86
			->will( $this->throwException( new \Aimeos\Client\Html\Exception() ) );
87
88
		$mock->body();
89
	}
90
91
92
	public function testBodyControllerFrontendException()
93
	{
94
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Basket\Standard\Standard::class )
95
			->setConstructorArgs( [$this->context] )
96
			->setMethods( array( 'data' ) )
97
			->getMock();
98
99
		$mock->setView( \TestHelperHtml::getView() );
100
101
		$mock->expects( $this->once() )->method( 'data' )
102
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
103
104
		$mock->body();
105
	}
106
107
108
	public function testBodyMShopException()
109
	{
110
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Basket\Standard\Standard::class )
111
			->setConstructorArgs( [$this->context] )
112
			->setMethods( array( 'data' ) )
113
			->getMock();
114
115
		$mock->setView( \TestHelperHtml::getView() );
116
117
		$mock->expects( $this->once() )->method( 'data' )
118
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
119
120
		$mock->body();
121
	}
122
123
124
	public function testBodyException()
125
	{
126
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Basket\Standard\Standard::class )
127
			->setConstructorArgs( [$this->context] )
128
			->setMethods( array( 'data' ) )
129
			->getMock();
130
131
		$mock->setView( \TestHelperHtml::getView() );
132
133
		$mock->expects( $this->once() )->method( 'data' )
134
			->will( $this->throwException( new \RuntimeException() ) );
135
136
		$mock->body();
137
	}
138
139
140
	public function testBodyAddSingle()
141
	{
142
		$view = $this->object->getView();
143
		$param = array(
144
			'b_action' => 'add',
145
			'b_prodid' => $this->getProductItem( 'CNE' )->getId(),
146
			'b_quantity' => 1,
147
			'b_stocktype' => 'default',
148
		);
149
150
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
151
		$view->addHelper( 'param', $helper );
152
153
		$this->object->init();
154
		$output = $this->object->body();
155
156
		$this->assertRegExp( '#<tbody>.*<td class="price">18.00 .+</td>.*</tbody>#smU', $output );
157
		$this->assertRegExp( '#<tfoot>.*<tr class="subtotal">.*<td class="price">18.00 .+</td>.*</tfoot>#smU', $output );
158
		$this->assertRegExp( '#<tfoot>.*<tr class="delivery">.*<td class="price">1.00 .+</td>.*</tfoot>#smU', $output );
159
		$this->assertRegExp( '#<tfoot>.*<tr class="total">.*<td class="price">19.00 .+</td>.*</tfoot>#smU', $output );
160
		$this->assertRegExp( '#<tfoot>.*<tr class="tax">.*<td class="price">3.03 .+</td>.*.*</tfoot>#smU', $output );
161
	}
162
163
164
	public function testBodyAddMulti()
165
	{
166
		$view = $this->object->getView();
167
		$param = array(
168
			'b_action' => 'add',
169
			'b_prod' => array(
170
				array(
171
					'prodid' => $this->getProductItem( 'CNC' )->getId(),
172
					'quantity' => 1,
173
					'stocktype' => 'default',
174
				),
175
				array(
176
					'prodid' => $this->getProductItem( 'CNE' )->getId(),
177
					'quantity' => 1,
178
					'stocktype' => 'default',
179
				),
180
			),
181
		);
182
183
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
184
		$view->addHelper( 'param', $helper );
185
186
		$this->object->init();
187
		$output = $this->object->body();
188
189
		$this->assertRegExp( '#<tbody>.*<td class="price">18.00 .+</td>.*</tbody>#smU', $output );
190
		$this->assertRegExp( '#<tbody>.*<td class="price">600.00 .+</td>.*</tbody>#smU', $output );
191
		$this->assertRegExp( '#<tfoot>.*<tr class="subtotal">.*<td class="price">618.00 .+</td>.*</tfoot>#smU', $output );
192
		$this->assertRegExp( '#<tfoot>.*<tr class="delivery">.*<td class="price">31.00 .+</td>.*</tfoot>#smU', $output );
193
		$this->assertRegExp( '#<tfoot>.*<tr class="total">.*<td class="price">649.00 .+</td>.*</tfoot>#smU', $output );
194
	}
195
196
197
	public function testBodyAddVariantAttribute()
198
	{
199
		$attrManager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context );
200
201
		$search = $attrManager->filter();
202
		$expr = array(
203
			$search->compare( '==', 'attribute.domain', 'product' ),
204
			$search->or( array(
205
				$search->and( array(
206
					$search->compare( '==', 'attribute.code', '30' ),
207
					$search->compare( '==', 'attribute.type', 'length' ),
208
				) ),
209
				$search->and( array(
210
					$search->compare( '==', 'attribute.code', '30' ),
211
					$search->compare( '==', 'attribute.type', 'width' ),
212
				) ),
213
			) ),
214
		);
215
		$search->setConditions( $search->and( $expr ) );
216
		$attributes = $attrManager->search( $search );
217
218
		$view = $this->object->getView();
219
		$param = array(
220
			'b_action' => 'add',
221
			'b_prodid' => $this->getProductItem( 'U:TEST' )->getId(),
222
			'b_quantity' => 2,
223
			'b_attrvarid' => $attributes->keys()->toArray(),
224
		);
225
226
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
227
		$view->addHelper( 'param', $helper );
228
229
		$this->object->init();
230
		$output = $this->object->body();
231
232
		$this->assertRegExp( '#<li class="attr-item.*<span class="value">.*30.*</span>.*</li>.*<li class="attr-item.*<span class="value">.*30.*</span>.*</li>#smU', $output );
233
	}
234
235
236
	public function testBodyAddConfigAttribute()
237
	{
238
		$manager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context );
239
240
		$search = $manager->filter();
241
		$expr = array(
242
			$search->compare( '==', 'attribute.code', 'white' ),
243
			$search->compare( '==', 'attribute.domain', 'product' ),
244
			$search->compare( '==', 'attribute.type', 'color' ),
245
		);
246
		$search->setConditions( $search->and( $expr ) );
247
248
		if( ( $attribute = $manager->search( $search )->first() ) === null ) {
249
			throw new \RuntimeException( 'No attribute' );
250
		}
251
252
		$view = $this->object->getView();
253
		$param = array(
254
			'b_action' => 'add',
255
			'b_prodid' => $this->getProductItem( 'CNE' )->getId(),
256
			'b_quantity' => 2,
257
			'b_attrconfid' => ['id' => [0 => $attribute->getId()], 'qty' => [0 =>1]],
258
			'b_stocktype' => 'default',
259
		);
260
261
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
262
		$view->addHelper( 'param', $helper );
263
264
		$this->object->init();
265
		$output = $this->object->body();
266
267
		$this->assertRegExp( '#<li class="attr-item.*<span class="value">.*weiß.*</span>.*</li>#smU', $output );
268
	}
269
270
271
	public function testBodyAddCustomAttribute()
272
	{
273
		$manager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context );
274
275
		$search = $manager->filter();
276
		$expr = array(
277
			$search->compare( '==', 'attribute.code', 'custom' ),
278
			$search->compare( '==', 'attribute.domain', 'product' ),
279
			$search->compare( '==', 'attribute.type', 'date' ),
280
		);
281
		$search->setConditions( $search->and( $expr ) );
282
283
		if( ( $attribute = $manager->search( $search )->first() ) === null ) {
284
			throw new \RuntimeException( 'No attribute' );
285
		}
286
287
		$view = $this->object->getView();
288
		$param = array(
289
			'b_action' => 'add',
290
			'b_prodid' => $this->getProductItem( 'U:TESTP' )->getId(),
291
			'b_quantity' => 2,
292
			'b_attrcustid' => array( $attribute->getId() => '2000-01-01' ),
293
			'b_stocktype' => 'default',
294
		);
295
296
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
297
		$view->addHelper( 'param', $helper );
298
299
		$this->object->init();
300
		$output = $this->object->body();
301
302
		$this->assertRegExp( '#<li class="attr-item.*<span class="value">.*2000-01-01.*</span>.*</li>#smU', $output );
303
	}
304
305
306
	public function testBodyEditSingle()
307
	{
308
		$this->addProduct( 'CNE', 2, 'default' );
309
310
		$view = $this->object->getView();
311
		$param = array(
312
			'b_action' => 'edit',
313
			'b_position' => 0,
314
			'b_quantity' => 1,
315
		);
316
317
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
318
		$view->addHelper( 'param', $helper );
319
320
		$this->object->init();
321
		$output = $this->object->body();
322
323
		$this->assertRegExp( '#<tbody>.*<td class="price">18.00 .+</td>.*</tbody>#smU', $output );
324
		$this->assertRegExp( '#<tfoot>.*<tr class="subtotal">.*<td class="price">18.00 .+</td>.*</tfoot>#smU', $output );
325
		$this->assertRegExp( '#<tfoot>.*<tr class="delivery">.*<td class="price">1.00 .+</td>.*</tfoot>#smU', $output );
326
		$this->assertRegExp( '#<tfoot>.*<tr class="total">.*<td class="price">19.00 .+</td>.*</tfoot>#smU', $output );
327
	}
328
329
330
	public function testBodyEditMulti()
331
	{
332
		$this->addProduct( 'CNE', 1, 'default' );
333
		$this->addProduct( 'CNC', 2, 'default' );
334
335
		$view = $this->object->getView();
336
		$param = array(
337
			'b_action' => 'edit',
338
			'b_prod' => array(
339
				array(
340
					'position' => 0,
341
					'quantity' => 2,
342
				),
343
				array(
344
					'position' => 1,
345
					'quantity' => 1,
346
				),
347
			),
348
		);
349
350
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
351
		$view->addHelper( 'param', $helper );
352
353
		$this->object->init();
354
		$output = $this->object->body();
355
356
		$this->assertRegExp( '#<tbody>.*<td class="price">36.00 .+</td>.*</tbody>#smU', $output );
357
		$this->assertRegExp( '#<tbody>.*<td class="price">600.00 .+</td>.*</tbody>#smU', $output );
358
		$this->assertRegExp( '#<tfoot>.*<tr class="subtotal">.*<td class="price">636.00 .+</td>.*</tfoot>#smU', $output );
359
		$this->assertRegExp( '#<tfoot>.*<tr class="delivery">.*<td class="price">32.00 .+</td>.*</tfoot>#smU', $output );
360
		$this->assertRegExp( '#<tfoot>.*<tr class="total">.*<td class="price">668.00 .+</td>.*</tfoot>#smU', $output );
361
	}
362
363
364
	public function testBodyDeleteSingle()
365
	{
366
		$this->addProduct( 'CNE', 2, 'default' );
367
		$this->addProduct( 'CNC', 1, 'default' );
368
369
		$view = $this->object->getView();
370
		$param = array(
371
			'b_action' => 'delete',
372
			'b_position' => 1,
373
		);
374
375
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
376
		$view->addHelper( 'param', $helper );
377
378
		$this->object->init();
379
		$output = $this->object->body();
380
381
		$this->assertRegExp( '#<tbody>.*<td class="price">36.00 .+</td>.*</tbody>#smU', $output );
382
		$this->assertRegExp( '#<tfoot>.*<tr class="subtotal">.*<td class="price">36.00 .+</td>.*</tfoot>#smU', $output );
383
		$this->assertRegExp( '#<tfoot>.*<tr class="delivery">.*<td class="price">2.00 .+</td>.*</tfoot>#smU', $output );
384
		$this->assertRegExp( '#<tfoot>.*<tr class="total">.*<td class="price">38.00 .+</td>.*</tfoot>#smU', $output );
385
	}
386
387
388
	public function testBodyDeleteMulti()
389
	{
390
		$this->addProduct( 'CNE', 1, 'default' );
391
		$this->addProduct( 'CNC', 1, 'default' );
392
393
		$view = $this->object->getView();
394
		$param = array(
395
			'b_action' => 'delete',
396
			'b_position' => array( 0, 1 ),
397
		);
398
399
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
400
		$view->addHelper( 'param', $helper );
401
402
		$this->object->init();
403
		$output = $this->object->body();
404
405
		$this->assertRegExp( '#<tfoot>.*<tr class="total">.*<td class="price">0.00 .+</td>.*</tfoot>#smU', $output );
406
	}
407
408
409
	public function testBodyDeleteInvalid()
410
	{
411
		$view = $this->object->getView();
412
		$param = array(
413
			'b_action' => 'delete',
414
			'b_position' => -1,
415
		);
416
417
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
418
		$view->addHelper( 'param', $helper );
419
420
		$this->object->init();
421
422
		$this->assertEquals( 1, count( $view->get( 'standardErrorList', [] ) ) );
423
	}
424
425
426
	public function testBodyAddCoupon()
427
	{
428
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
429
		$controller->addProduct( $this->getProductItem( 'CNC' ), 1 );
430
431
		$view = $this->object->getView();
432
433
		$param = array( 'b_coupon' => '90AB' );
434
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
435
		$view->addHelper( 'param', $helper );
436
437
		$this->object->init();
438
439
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
440
		$view->standardBasket = $controller->get();
441
		$output = $this->object->body();
442
443
		$this->assertRegExp( '#<li class="attr-item">.*90AB.*</li>#smU', $output );
444
	}
445
446
447
	public function testBodyDeleteCoupon()
448
	{
449
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
450
		$controller->addProduct( $this->getProductItem( 'CNC' ), 1 );
451
452
		$view = $this->object->getView();
453
454
		$param = array( 'b_coupon' => '90AB' );
455
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
456
		$view->addHelper( 'param', $helper );
457
458
		$this->object->init();
459
460
461
		$param = array( 'b_action' => 'coupon-delete', 'b_coupon' => '90AB' );
462
463
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
464
		$view->addHelper( 'param', $helper );
465
466
		$this->object->init();
467
468
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
469
		$view->standardBasket = $controller->get();
470
		$output = $this->object->body();
471
472
		$this->assertNotRegExp( '#<ul class="attr-list">#smU', $output );
473
	}
474
475
476
	public function testBodyOverwriteCoupon()
477
	{
478
		$this->context->getConfig()->set( 'client/html/basket/standard/coupon/overwrite', true );
479
480
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
481
		$controller->addProduct( $this->getProductItem( 'CNC' ), 1 );
482
483
		$view = $this->object->getView();
484
485
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, ['b_coupon' => '90AB'] );
486
		$view->addHelper( 'param', $helper );
487
488
		$this->object->init();
489
490
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, ['b_coupon' => 'OPQR'] );
491
		$view->addHelper( 'param', $helper );
492
493
		$this->object->init();
494
495
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
496
		$view->standardBasket = $controller->get();
497
		$output = $this->object->body();
498
499
		$this->assertStringContainsString( 'OPQR', $output );
500
		$this->assertStringNotContainsString( '90AB', $output );
501
	}
502
503
504
	public function testGetSubClientInvalid()
505
	{
506
		$this->expectException( '\\Aimeos\\Client\\Html\\Exception' );
507
		$this->object->getSubClient( 'invalid', 'invalid' );
508
	}
509
510
511
	public function testGetSubClientInvalidName()
512
	{
513
		$this->expectException( '\\Aimeos\\Client\\Html\\Exception' );
514
		$this->object->getSubClient( '$$$', '$$$' );
515
	}
516
517
518
	/**
519
	 * @param string $code
520
	 * @param integer $quantity
521
	 * @param string $stockType
522
	 */
523
	protected function addProduct( $code, $quantity, $stockType )
524
	{
525
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
526
		$search = $manager->filter();
527
		$search->setConditions( $search->compare( '==', 'product.code', $code ) );
528
529
		if( ( $item = $manager->search( $search )->first() ) === null ) {
530
			throw new \RuntimeException( sprintf( 'No product item with code "%1$s" found', $code ) );
531
		}
532
533
		$view = $this->object->getView();
534
		$param = array(
535
			'b_action' => 'add',
536
			'b_prodid' => $item->getId(),
537
			'b_quantity' => $quantity,
538
			'b_stocktype' => $stockType,
539
		);
540
541
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
542
		$view->addHelper( 'param', $helper );
543
544
		$this->object->init();
545
	}
546
547
548
	/**
549
	 * @param string $code
550
	 */
551
	protected function getProductItem( $code )
552
	{
553
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
554
		$search = $manager->filter();
555
		$search->setConditions( $search->compare( '==', 'product.code', $code ) );
556
557
		if( ( $item = $manager->search( $search, ['price'] )->first() ) === null ) {
558
			throw new \RuntimeException( sprintf( 'No product item with code "%1$s" found', $code ) );
559
		}
560
561
		return $item;
562
	}
563
}
564