Passed
Push — master ( cec6d2...ab1421 )
by Aimeos
02:44
created

StandardTest::testGetBodyDefaultCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.9
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\Catalog\Detail;
11
12
13
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...
14
{
15
	private $object;
16
	private $context;
17
18
19
	protected function setUp()
20
	{
21
		$this->context = \TestHelperHtml::getContext();
22
23
		$this->object = new \Aimeos\Client\Html\Catalog\Detail\Standard( $this->context );
24
		$this->object->setView( \TestHelperHtml::getView() );
25
	}
26
27
28
	protected function tearDown()
29
	{
30
		unset( $this->object );
31
	}
32
33
34
	public function testGetHeader()
35
	{
36
		$view = $this->object->getView();
37
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'd_prodid' => $this->getProductItem()->getId() ) );
38
		$view->addHelper( 'param', $helper );
39
40
		$tags = [];
41
		$expire = null;
42
43
		$this->object->setView( $this->object->addData( $this->object->getView(), $tags, $expire ) );
44
		$output = $this->object->getHeader();
45
46
		$this->assertContains( '<title>Cafe Noire Expresso</title>', $output );
47
		$this->assertContains( '<script type="text/javascript" defer="defer" src="http://baseurl/catalog/stock/?s_prodcode', $output );
48
		$this->assertEquals( '2098-01-01 00:00:00', $expire );
49
		$this->assertEquals( 6, count( $tags ) );
50
	}
51
52
53
	public function testGetHeaderException()
54
	{
55
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Detail\Standard::class )
56
			->setConstructorArgs( array( $this->context, \TestHelperHtml::getHtmlTemplatePaths() ) )
57
			->setMethods( array( 'addData' ) )
58
			->getMock();
59
60
		$mock->setView( \TestHelperHtml::getView() );
61
62
		$mock->expects( $this->once() )->method( 'addData' )
63
			->will( $this->throwException( new \RuntimeException() ) );
64
65
		$mock->getHeader();
66
	}
67
68
69
	public function testGetBody()
70
	{
71
		$view = $this->object->getView();
72
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'd_prodid' => $this->getProductItem()->getId() ) );
73
		$view->addHelper( 'param', $helper );
74
75
		$tags = [];
76
		$expire = null;
77
78
		$this->object->setView( $this->object->addData( $view, $tags, $expire ) );
79
		$output = $this->object->getBody();
80
81
		$this->assertStringStartsWith( '<section class="aimeos catalog-detail"', $output );
82
		$this->assertContains( '<div class="catalog-detail-basic">', $output );
83
		$this->assertContains( '<div class="catalog-detail-image', $output );
84
85
		$this->assertContains( '<div class="catalog-social">', $output );
86
		$this->assertRegExp( '/.*facebook.*/', $output );
87
88
		$this->assertContains( '<div class="catalog-actions">', $output );
89
		$this->assertContains( 'actions-button-pin', $output );
90
		$this->assertContains( 'actions-button-watch', $output );
91
		$this->assertContains( 'actions-button-favorite', $output );
92
93
		$this->assertContains( '<div class="catalog-detail-additional">', $output );
94
		$this->assertContains( '<h2 class="header description">', $output );
95
96
		$this->assertContains( '<h2 class="header attributes">', $output );
97
		$this->assertContains( '<td class="name">size</td>', $output );
98
		$this->assertContains( '<span class="attr-name">XS</span>', $output );
99
100
		$this->assertContains( '<h2 class="header properties">', $output );
101
		$this->assertContains( '<td class="name">package-height</td>', $output );
102
		$this->assertContains( '<td class="value">10.0</td>', $output );
103
104
		$this->assertContains( '<h2 class="header downloads">', $output );
105
		$this->assertContains( '<span class="media-name">path/to/folder/example5.jpg</span>', $output );
106
107
		$this->assertContains( '<section class="catalog-detail-suggest">', $output );
108
		$this->assertRegExp( '/.*Cappuccino.*/', $output );
109
110
		$this->assertContains( '<section class="catalog-detail-bought">', $output );
111
		$this->assertRegExp( '/.*Cappuccino.*/', $output );
112
113
		$this->assertContains( '<div class="catalog-detail-service', $output );
114
115
		$this->assertEquals( '2098-01-01 00:00:00', $expire );
116
		$this->assertEquals( 6, count( $tags ) );
117
	}
118
119
120
	public function testGetBodyByName()
121
	{
122
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
123
		$this->object = new \Aimeos\Client\Html\Catalog\Detail\Standard( $this->context, $paths );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\Html\Catal...Standard::__construct() has too many arguments starting with $paths. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
		$this->object = /** @scrutinizer ignore-call */ new \Aimeos\Client\Html\Catalog\Detail\Standard( $this->context, $paths );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
124
		$this->object->setView( \TestHelperHtml::getView() );
125
126
		$view = $this->object->getView();
127
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'd_name' => 'Cafe_Noire_Expresso' ) );
128
		$view->addHelper( 'param', $helper );
129
130
		$this->object->setView( $this->object->addData( $view ) );
131
		$output = $this->object->getBody();
132
133
		$this->assertContains( '<span class="value" itemprop="sku">CNE</span>', $output );
134
	}
135
136
137
	public function testGetBodyDefaultId()
138
	{
139
		$context = clone $this->context;
140
		$context->getConfig()->set( 'client/html/catalog/detail/prodid-default', $this->getProductItem()->getId() );
141
142
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
143
		$this->object = new \Aimeos\Client\Html\Catalog\Detail\Standard( $context, $paths );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\Html\Catal...Standard::__construct() has too many arguments starting with $paths. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
		$this->object = /** @scrutinizer ignore-call */ new \Aimeos\Client\Html\Catalog\Detail\Standard( $context, $paths );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
144
		$this->object->setView( \TestHelperHtml::getView() );
145
146
		$view = $this->object->getView();
147
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, [] );
148
		$view->addHelper( 'param', $helper );
149
150
		$this->object->setView( $this->object->addData( $view ) );
151
		$output = $this->object->getBody();
152
153
		$this->assertContains( '<span class="value" itemprop="sku">CNE</span>', $output );
154
	}
155
156
157
	public function testGetBodyDefaultCode()
158
	{
159
		$context = clone $this->context;
160
		$context->getConfig()->set( 'client/html/catalog/detail/prodcode-default', 'CNE' );
161
162
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
163
		$this->object = new \Aimeos\Client\Html\Catalog\Detail\Standard( $context, $paths );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\Html\Catal...Standard::__construct() has too many arguments starting with $paths. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
		$this->object = /** @scrutinizer ignore-call */ new \Aimeos\Client\Html\Catalog\Detail\Standard( $context, $paths );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
164
		$this->object->setView( \TestHelperHtml::getView() );
165
166
		$view = $this->object->getView();
167
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, [] );
168
		$view->addHelper( 'param', $helper );
169
170
		$this->object->setView( $this->object->addData( $view ) );
171
		$output = $this->object->getBody();
172
173
		$this->assertContains( '<span class="value" itemprop="sku">CNE</span>', $output );
174
	}
175
176
177
	public function testGetBodyCsrf()
178
	{
179
		$view = $this->object->getView();
180
		$view->detailProductItem = $this->getProductItem();
181
182
		$output = $this->object->getBody( 1 );
183
		$output = str_replace( '_csrf_value', '_csrf_new', $output );
184
185
		$this->assertContains( '<input class="csrf-token" type="hidden" name="_csrf_token" value="_csrf_new" />', $output );
186
187
		$output = $this->object->modifyBody( $output, 1 );
188
189
		$this->assertContains( '<input class="csrf-token" type="hidden" name="_csrf_token" value="_csrf_value" />', $output );
190
	}
191
192
193
	public function testGetBodyAttributes()
194
	{
195
		$product = $this->getProductItem( 'U:TESTP', array( 'attribute' ) );
196
197
		$view = $this->object->getView();
198
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'd_prodid' => $product->getId() ) );
199
		$view->addHelper( 'param', $helper );
200
201
		$configAttr = $product->getRefItems( 'attribute', null, 'config' );
202
203
		$this->assertGreaterThan( 0, count( $configAttr ) );
204
205
		$output = $this->object->getBody();
206
		$this->assertContains( '<div class="catalog-detail-basket-attribute', $output );
207
208
		foreach( $configAttr as $id => $item ) {
209
			$this->assertRegexp( '#<option class="select-option".*value="' . $id . '">#smU', $output );
210
		}
211
	}
212
213
214
	public function testGetBodySelection()
215
	{
216
		$view = $this->object->getView();
217
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'd_prodid' => $this->getProductItem( 'U:TEST' )->getId() ) );
218
		$view->addHelper( 'param', $helper );
219
220
		$variantAttr1 = $this->getProductItem( 'U:TESTSUB02', array( 'attribute' ) )->getRefItems( 'attribute', null, 'variant' );
221
		$variantAttr2 = $this->getProductItem( 'U:TESTSUB04', array( 'attribute' ) )->getRefItems( 'attribute', null, 'variant' );
222
223
		$this->assertGreaterThan( 0, count( $variantAttr1 ) );
224
		$this->assertGreaterThan( 0, count( $variantAttr2 ) );
225
226
		$tags = [];
227
		$expire = null;
228
229
		$this->object->setView( $this->object->addData( $view, $tags, $expire ) );
230
		$output = $this->object->getBody( 1, $tags, $expire );
231
232
		$this->assertContains( '<div class="catalog-detail-basket-selection', $output );
233
234
		foreach( $variantAttr1 as $id => $item ) {
235
			$this->assertRegexp( '#<option class="select-option" value="' . $id . '">#', $output );
236
		}
237
238
		foreach( $variantAttr2 as $id => $item ) {
239
			$this->assertRegexp( '#<option class="select-option" value="' . $id . '">#', $output );
240
		}
241
242
		$this->assertEquals( null, $expire );
243
		$this->assertEquals( 6, count( $tags ) );
244
	}
245
246
247
	public function testGetBodyClientHtmlException()
248
	{
249
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Detail\Standard::class )
250
			->setConstructorArgs( array( $this->context, \TestHelperHtml::getHtmlTemplatePaths() ) )
251
			->setMethods( array( 'addData' ) )
252
			->getMock();
253
254
		$mock->setView( \TestHelperHtml::getView() );
255
256
		$mock->expects( $this->once() )->method( 'addData' )
257
			->will( $this->throwException( new \Aimeos\Client\Html\Exception() ) );
258
259
		$mock->getBody();
260
	}
261
262
263
	public function testGetBodyControllerFrontendException()
264
	{
265
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Detail\Standard::class )
266
			->setConstructorArgs( array( $this->context, \TestHelperHtml::getHtmlTemplatePaths() ) )
267
			->setMethods( array( 'addData' ) )
268
			->getMock();
269
270
		$mock->setView( \TestHelperHtml::getView() );
271
272
		$mock->expects( $this->once() )->method( 'addData' )
273
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
274
275
		$mock->getBody();
276
	}
277
278
279
	public function testGetBodyMShopException()
280
	{
281
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Detail\Standard::class )
282
			->setConstructorArgs( array( $this->context, \TestHelperHtml::getHtmlTemplatePaths() ) )
283
			->setMethods( array( 'addData' ) )
284
			->getMock();
285
286
		$mock->setView( \TestHelperHtml::getView() );
287
288
		$mock->expects( $this->once() )->method( 'addData' )
289
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
290
291
		$mock->getBody();
292
	}
293
294
295
	public function testGetBodyException()
296
	{
297
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Detail\Standard::class )
298
			->setConstructorArgs( array( $this->context, \TestHelperHtml::getHtmlTemplatePaths() ) )
299
			->setMethods( array( 'addData' ) )
300
			->getMock();
301
302
		$mock->setView( \TestHelperHtml::getView() );
303
304
		$mock->expects( $this->once() )->method( 'addData' )
305
			->will( $this->throwException( new \RuntimeException() ) );
306
307
		$mock->getBody();
308
	}
309
310
311
	public function testGetSubClient()
312
	{
313
		$client = $this->object->getSubClient( 'service', 'Standard' );
314
		$this->assertInstanceOf( '\\Aimeos\\Client\\HTML\\Iface', $client );
315
	}
316
317
318
	public function testGetSubClientInvalid()
319
	{
320
		$this->setExpectedException( '\\Aimeos\\Client\\Html\\Exception' );
321
		$this->object->getSubClient( 'invalid', 'invalid' );
322
	}
323
324
325
	public function testGetSubClientInvalidName()
326
	{
327
		$this->setExpectedException( '\\Aimeos\\Client\\Html\\Exception' );
328
		$this->object->getSubClient( '$$$', '$$$' );
329
	}
330
331
332
	public function testProcess()
333
	{
334
		$this->object->process();
335
	}
336
337
338
	public function testProcessClientHtmlException()
339
	{
340
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Detail\Standard::class )
341
			->setConstructorArgs( array( $this->context, [] ) )
342
			->setMethods( array( 'getClientParams' ) )
343
			->getMock();
344
345
		$mock->setView( \TestHelperHtml::getView() );
346
347
		$mock->expects( $this->once() )->method( 'getClientParams' )
348
			->will( $this->throwException( new \Aimeos\Client\Html\Exception() ) );
349
350
		$mock->process();
351
	}
352
353
354
	public function testProcessControllerFrontendException()
355
	{
356
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Detail\Standard::class )
357
			->setConstructorArgs( array( $this->context, [] ) )
358
			->setMethods( array( 'getClientParams' ) )
359
			->getMock();
360
361
		$mock->setView( \TestHelperHtml::getView() );
362
363
		$mock->expects( $this->once() )->method( 'getClientParams' )
364
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
365
366
		$mock->process();
367
	}
368
369
370
	public function testProcessMShopException()
371
	{
372
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Detail\Standard::class )
373
			->setConstructorArgs( array( $this->context, [] ) )
374
			->setMethods( array( 'getClientParams' ) )
375
			->getMock();
376
377
		$mock->setView( \TestHelperHtml::getView() );
378
379
		$mock->expects( $this->once() )->method( 'getClientParams' )
380
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
381
382
		$mock->process();
383
	}
384
385
386
	public function testProcessException()
387
	{
388
		$mock = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Detail\Standard::class )
389
			->setConstructorArgs( array( $this->context, [] ) )
390
			->setMethods( array( 'getClientParams' ) )
391
			->getMock();
392
393
		$mock->setView( \TestHelperHtml::getView() );
394
395
		$mock->expects( $this->once() )->method( 'getClientParams' )
396
			->will( $this->throwException( new \RuntimeException() ) );
397
398
		$mock->process();
399
	}
400
401
402
	protected function getProductItem( $code = 'CNE', $domains = [] )
403
	{
404
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
405
		$search = $manager->createSearch();
406
		$search->setConditions( $search->compare( '==', 'product.code', $code ) );
407
		$items = $manager->searchItems( $search, $domains );
408
409
		if( ( $item = reset( $items ) ) === false ) {
410
			throw new \RuntimeException( sprintf( 'No product item with code "%1$s" found', $code ) );
411
		}
412
413
		return $item;
414
	}
415
}
416