Completed
Push — master ( e9d499...62281e )
by Aimeos
03:07
created

StandardTest::testGetBodyOverwriteCoupon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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