Passed
Push — master ( f9b95f...90584d )
by Aimeos
04:14
created

StandardTest::testGetBodySearchStock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
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-2020
7
 */
8
9
10
namespace Aimeos\Client\Html\Catalog\Lists;
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
		$this->object = new \Aimeos\Client\Html\Catalog\Lists\Standard( $this->context );
24
		$this->object->setView( \TestHelperHtml::getView() );
25
	}
26
27
28
	protected function tearDown() : void
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( 'f_catid' => $this->getCatalogItem()->getId() ) );
38
		$view->addHelper( 'param', $helper );
39
40
		$tags = [];
41
		$expire = null;
42
43
		$this->object->setView( $this->object->addData( $view, $tags, $expire ) );
44
		$output = $this->object->getHeader();
45
46
		$this->assertStringContainsString( '<title>Kaffee</title>', $output );
47
		$this->assertEquals( '2098-01-01 00:00:00', $expire );
48
		$this->assertEquals( 5, count( $tags ) );
49
	}
50
51
52
	public function testGetHeaderSearch()
53
	{
54
		$view = $this->object->getView();
55
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'f_search' => '<b>Search result</b>' ) );
56
		$view->addHelper( 'param', $helper );
57
58
		$tags = [];
59
		$expire = null;
60
61
		$this->object->setView( $this->object->addData( $view, $tags, $expire ) );
62
		$output = $this->object->getHeader();
63
64
		$this->assertRegexp( '#<title>[^>]*Search result[^<]*</title>#', $output );
65
		$this->assertEquals( null, $expire );
66
		$this->assertEquals( 1, count( $tags ) );
67
	}
68
69
70
	public function testGetHeaderException()
71
	{
72
		$object = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Lists\Standard::class )
73
			->setConstructorArgs( array( $this->context, [] ) )
74
			->setMethods( array( 'addData' ) )
75
			->getMock();
76
77
		$object->expects( $this->once() )->method( 'addData' )
78
			->will( $this->throwException( new \RuntimeException() ) );
79
80
		$object->setView( \TestHelperHtml::getView() );
81
82
		$this->assertEmpty( $object->getHeader() );
83
	}
84
85
86
	public function testGetBody()
87
	{
88
		$view = $this->object->getView();
89
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'f_catid' => $this->getCatalogItem()->getId() ) );
90
		$view->addHelper( 'param', $helper );
91
92
		$tags = [];
93
		$expire = null;
94
95
		$this->object->setView( $this->object->addData( $view, $tags, $expire ) );
96
		$output = $this->object->getBody();
97
98
		$this->assertStringStartsWith( '<section class="aimeos catalog-list home categories coffee"', $output );
99
100
		$this->assertStringContainsString( '<div class="catalog-list-head">', $output );
101
		$this->assertRegExp( '#<h1>Kaffee</h1>#', $output );
102
103
		$this->assertEquals( '2098-01-01 00:00:00', $expire );
104
		$this->assertEquals( 5, count( $tags ) );
105
	}
106
107
108
	public function testGetBodyPagination()
109
	{
110
		$view = $this->object->getView();
111
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, ['l_size' => 2] );
112
		$view->addHelper( 'param', $helper );
113
114
		$output = $this->object->getBody();
115
116
		$this->assertStringStartsWith( '<section class="aimeos catalog-list', $output );
117
		$this->assertStringContainsString( '<nav class="pagination">', $output );
118
	}
119
120
121
	public function testGetBodyNoDefaultCat()
122
	{
123
		$view = $this->object->getView();
124
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, [] );
125
		$view->addHelper( 'param', $helper );
126
127
		$output = $this->object->getBody();
128
129
		$this->assertStringStartsWith( '<section class="aimeos catalog-list', $output );
130
		$this->assertNotRegExp( '#.*U:TESTPSUB01.*#smu', $output );
131
		$this->assertNotRegExp( '#.*U:TESTSUB03.*#smu', $output );
132
		$this->assertNotRegExp( '#.*U:TESTSUB04.*#smu', $output );
133
		$this->assertNotRegExp( '#.*U:TESTSUB05.*#smu', $output );
134
	}
135
136
137
	public function testGetBodyDefaultCat()
138
	{
139
		$context = clone $this->context;
140
		$context->getConfig()->set( 'client/html/catalog/lists/catid-default', $this->getCatalogItem()->getId() );
141
142
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
143
		$this->object = new \Aimeos\Client\Html\Catalog\Lists\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\Lists\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
		$output = $this->object->getBody();
151
152
		$this->assertStringStartsWith( '<section class="aimeos catalog-list home categories coffee"', $output );
153
	}
154
155
156
	public function testGetBodyMultipleDefaultCat()
157
	{
158
		$context = clone $this->context;
159
		$catid = $this->getCatalogItem()->getId();
160
		$context->getConfig()->set( 'client/html/catalog/lists/catid-default', array( $catid, $catid ) );
161
162
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
163
		$this->object = new \Aimeos\Client\Html\Catalog\Lists\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\Lists\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
		$output = $this->object->getBody();
171
172
		$this->assertStringStartsWith( '<section class="aimeos catalog-list home categories coffee"', $output );
173
	}
174
175
176
	public function testGetBodyMultipleDefaultCatString()
177
	{
178
		$context = clone $this->context;
179
		$catid = $this->getCatalogItem()->getId();
180
		$context->getConfig()->set( 'client/html/catalog/lists/catid-default', $catid . ',' . $catid );
181
182
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
183
		$this->object = new \Aimeos\Client\Html\Catalog\Lists\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

183
		$this->object = /** @scrutinizer ignore-call */ new \Aimeos\Client\Html\Catalog\Lists\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...
184
		$this->object->setView( \TestHelperHtml::getView() );
185
186
		$view = $this->object->getView();
187
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, [] );
188
		$view->addHelper( 'param', $helper );
189
190
		$output = $this->object->getBody();
191
192
		$this->assertStringStartsWith( '<section class="aimeos catalog-list home categories coffee"', $output );
193
	}
194
195
196
	public function testGetBodyCategoryLevels()
197
	{
198
		$context = clone $this->context;
199
		$context->getConfig()->set( 'client/html/catalog/lists/levels', \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE );
200
201
		$paths = \TestHelperHtml::getHtmlTemplatePaths();
202
		$this->object = new \Aimeos\Client\Html\Catalog\Lists\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

202
		$this->object = /** @scrutinizer ignore-call */ new \Aimeos\Client\Html\Catalog\Lists\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...
203
		$this->object->setView( \TestHelperHtml::getView() );
204
205
		$view = $this->object->getView();
206
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'f_catid' => $this->getCatalogItem( 'root' )->getId() ) );
207
		$view->addHelper( 'param', $helper );
208
209
		$output = $this->object->getBody();
210
211
		$this->assertRegExp( '#.*Cafe Noire Cappuccino.*#smu', $output );
212
		$this->assertRegExp( '#.*Cafe Noire Expresso.*#smu', $output );
213
		$this->assertRegExp( '#.*Unittest: Bundle.*#smu', $output );
214
		$this->assertRegExp( '#.*Unittest: Test priced Selection.*#smu', $output );
215
	}
216
217
218
	public function testGetBodySearchText()
219
	{
220
		$view = $this->object->getView();
221
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'f_search' => '<b>Search result</b>' ) );
222
		$view->addHelper( 'param', $helper );
223
224
		$output = $this->object->getBody();
225
226
		$this->assertStringStartsWith( '<section class="aimeos catalog-list', $output );
227
		$this->assertStringContainsString( '&lt;b&gt;Search result&lt;/b&gt;', $output );
228
	}
229
230
231
	public function testGetBodySearchAttribute()
232
	{
233
		$view = $this->object->getView();
234
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'f_attrid' => array( -1, -2 ) ) );
235
		$view->addHelper( 'param', $helper );
236
237
		$output = $this->object->getBody();
238
239
		$this->assertStringStartsWith( '<section class="aimeos catalog-list', $output );
240
	}
241
242
243
	public function testGetBodySearchStock()
244
	{
245
		$view = $this->object->getView();
246
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'f_stock' => 1 ) );
247
		$view->addHelper( 'param', $helper );
248
249
		$output = $this->object->getBody();
250
251
		$this->assertStringStartsWith( '<section class="aimeos catalog-list', $output );
252
		$this->assertStringNotContainsString( '"U:TEST"', $output );
253
		$this->assertStringContainsString( 'CNE', $output );
254
	}
255
256
257
	public function testGetBodySearchSupplier()
258
	{
259
		$view = $this->object->getView();
260
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'f_supid' => array( -1, -2 ) ) );
261
		$view->addHelper( 'param', $helper );
262
263
		$output = $this->object->getBody();
264
265
		$this->assertStringStartsWith( '<section class="aimeos catalog-list', $output );
266
	}
267
268
269
	public function testGetBodyHtmlException()
270
	{
271
		$object = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Lists\Standard::class )
272
			->setConstructorArgs( array( $this->context, [] ) )
273
			->setMethods( array( 'addData' ) )
274
			->getMock();
275
276
		$object->expects( $this->once() )->method( 'addData' )
277
			->will( $this->throwException( new \Aimeos\Client\Html\Exception( 'test exception' ) ) );
278
279
		$object->setView( \TestHelperHtml::getView() );
280
281
		$this->assertStringContainsString( 'test exception', $object->getBody() );
282
	}
283
284
285
	public function testGetBodyFrontendException()
286
	{
287
		$object = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Lists\Standard::class )
288
			->setConstructorArgs( array( $this->context, [] ) )
289
			->setMethods( array( 'addData' ) )
290
			->getMock();
291
292
		$object->expects( $this->once() )->method( 'addData' )
293
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Exception( 'test exception' ) ) );
294
295
		$object->setView( \TestHelperHtml::getView() );
296
297
		$this->assertStringContainsString( 'test exception', $object->getBody() );
298
	}
299
300
301
	public function testGetBodyMShopException()
302
	{
303
		$object = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Lists\Standard::class )
304
			->setConstructorArgs( array( $this->context, [] ) )
305
			->setMethods( array( 'addData' ) )
306
			->getMock();
307
308
		$object->expects( $this->once() )->method( 'addData' )
309
			->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) );
310
311
		$object->setView( \TestHelperHtml::getView() );
312
313
		$this->assertStringContainsString( 'test exception', $object->getBody() );
314
	}
315
316
317
	public function testGetBodyException()
318
	{
319
		$object = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Lists\Standard::class )
320
			->setConstructorArgs( array( $this->context, [] ) )
321
			->setMethods( array( 'addData' ) )
322
			->getMock();
323
324
		$object->expects( $this->once() )->method( 'addData' )
325
			->will( $this->throwException( new \RuntimeException( 'test exception' ) ) );
326
327
		$object->setView( \TestHelperHtml::getView() );
328
329
		$this->assertStringContainsString( 'A non-recoverable error occured', $object->getBody() );
330
	}
331
332
333
	public function testGetSubClient()
334
	{
335
		$client = $this->object->getSubClient( 'items', 'Standard' );
336
		$this->assertInstanceOf( '\\Aimeos\\Client\\HTML\\Iface', $client );
337
	}
338
339
340
	public function testGetSubClientInvalid()
341
	{
342
		$this->expectException( '\\Aimeos\\Client\\Html\\Exception' );
343
		$this->object->getSubClient( 'invalid', 'invalid' );
344
	}
345
346
347
	public function testGetSubClientInvalidName()
348
	{
349
		$this->expectException( '\\Aimeos\\Client\\Html\\Exception' );
350
		$this->object->getSubClient( '$$$', '$$$' );
351
	}
352
353
354
	public function testProcess()
355
	{
356
		$view = $this->object->getView();
357
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'l_type' => 'list' ) );
358
		$view->addHelper( 'param', $helper );
359
360
		$this->object->process();
361
362
		$this->assertEmpty( $this->object->getView()->get( 'listErrorList' ) );
363
	}
364
365
366
	public function testProcessHtmlException()
367
	{
368
		$object = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Lists\Standard::class )
369
			->setConstructorArgs( array( $this->context, [] ) )
370
			->setMethods( array( 'getClientParams' ) )
371
			->getMock();
372
373
		$object->expects( $this->once() )->method( 'getClientParams' )
374
			->will( $this->throwException( new \Aimeos\Client\Html\Exception( 'text exception' ) ) );
375
376
		$object->setView( \TestHelperHtml::getView() );
377
378
		$object->process();
379
380
		$this->assertIsArray( $object->getView()->listErrorList );
381
	}
382
383
384
	public function testProcessFrontendException()
385
	{
386
		$object = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Lists\Standard::class )
387
			->setConstructorArgs( array( $this->context, [] ) )
388
			->setMethods( array( 'getClientParams' ) )
389
			->getMock();
390
391
		$object->expects( $this->once() )->method( 'getClientParams' )
392
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Exception( 'text exception' ) ) );
393
394
		$object->setView( \TestHelperHtml::getView() );
395
396
		$object->process();
397
398
		$this->assertIsArray( $object->getView()->listErrorList );
399
	}
400
401
402
	public function testProcessMShopException()
403
	{
404
		$object = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Lists\Standard::class )
405
			->setConstructorArgs( array( $this->context, [] ) )
406
			->setMethods( array( 'getClientParams' ) )
407
			->getMock();
408
409
		$object->expects( $this->once() )->method( 'getClientParams' )
410
			->will( $this->throwException( new \Aimeos\MShop\Exception( 'text exception' ) ) );
411
412
		$object->setView( \TestHelperHtml::getView() );
413
414
		$object->process();
415
416
		$this->assertIsArray( $object->getView()->listErrorList );
417
	}
418
419
420
	public function testProcessException()
421
	{
422
		$object = $this->getMockBuilder( \Aimeos\Client\Html\Catalog\Lists\Standard::class )
423
		->setConstructorArgs( array( $this->context, [] ) )
424
		->setMethods( array( 'getClientParams' ) )
425
		->getMock();
426
427
		$object->expects( $this->once() )->method( 'getClientParams' )
428
		->will( $this->throwException( new \RuntimeException( 'text exception' ) ) );
429
430
		$object->setView( \TestHelperHtml::getView() );
431
432
		$object->process();
433
434
		$this->assertIsArray( $object->getView()->listErrorList );
435
	}
436
437
438
	protected function getCatalogItem( $code = 'cafe' )
439
	{
440
		$catalogManager = \Aimeos\MShop\Catalog\Manager\Factory::create( $this->context );
441
		$search = $catalogManager->filter();
442
		$search->setConditions( $search->compare( '==', 'catalog.code', $code ) );
0 ignored issues
show
Bug introduced by
The method compare() does not exist on Aimeos\MW\Criteria\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MW\Criteria\Base. Are you sure you never get one of those? ( Ignorable by Annotation )

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

442
		$search->setConditions( $search->/** @scrutinizer ignore-call */ compare( '==', 'catalog.code', $code ) );
Loading history...
Bug introduced by
The method setConditions() does not exist on Aimeos\MW\Criteria\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MW\Criteria\Base. Are you sure you never get one of those? ( Ignorable by Annotation )

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

442
		$search->/** @scrutinizer ignore-call */ 
443
           setConditions( $search->compare( '==', 'catalog.code', $code ) );
Loading history...
443
444
		if( ( $item = $catalogManager->search( $search )->first() ) === null ) {
445
			throw new \RuntimeException( sprintf( 'No catalog item with code "%1$s" found', $code ) );
446
		}
447
448
		return $item;
449
	}
450
}
451