Completed
Push — master ( 7d4729...4f5586 )
by Aimeos
07:42
created

StandardTest   C

Complexity

Total Complexity 43

Size/Duplication

Total Lines 792
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 43
lcom 1
cbo 16
dl 0
loc 792
rs 6.5185
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
B setUpBeforeClass() 0 25 3
A setUp() 0 6 1
A tearDown() 0 4 1
A testCreateItem() 0 4 1
A testCreateSearch() 0 4 1
B testAggregate() 0 26 2
A testGetItem() 0 14 2
A testGetResourceType() 0 10 1
A testGetSearchAttributes() 0 6 2
B testSaveDeleteItem() 0 44 1
B testSaveDeleteItemNoName() 0 32 2
B testSearchItems() 0 45 2
B testSearchItemsAttribute() 0 89 3
B testSearchItemsCatalog() 0 94 3
A testSearchItemsPrice() 0 50 2
A testSearchItemsText() 0 70 2
B testSearchTexts() 0 34 2
A testOptimize() 0 4 1
A testCleanupIndex() 0 4 1
B testRebuildIndexAll() 0 29 1
B testRebuildIndexWithList() 0 46 2
B testRebuildIndexCategorizedOnly() 0 26 1
B getValue() 0 39 5
A getCatalogSubDomainItems() 0 14 1

How to fix   Complexity   

Complex Class

Complex classes like StandardTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use StandardTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 */
8
9
10
namespace Aimeos\MShop\Index\Manager;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private static $products;
16
	private $context;
17
	private $object;
18
	private $editor = '';
19
20
21
	public static function setUpBeforeClass()
22
	{
23
		$context = \TestHelperMShop::getContext();
24
25
		$manager = new \Aimeos\MShop\Index\Manager\Standard( $context );
26
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( $context );
27
28
		$search = $productManager->createSearch();
29
		$conditions = array(
30
			$search->compare( '==', 'product.code', array( 'CNC', 'CNE' ) ),
31
			$search->compare( '==', 'product.editor', $context->getEditor() ),
32
		);
33
		$search->setConditions( $search->combine( '&&', $conditions ) );
34
		$result = $productManager->searchItems( $search, array( 'attribute', 'price', 'text', 'product' ) );
35
36
		if( count( $result ) !== 2 ) {
37
			throw new \RuntimeException( 'Products not available' );
38
		}
39
40
		foreach( $result as $item )
41
		{
42
			self::$products[$item->getCode()] = $item;
43
			$manager->saveItem( $item );
44
		}
45
	}
46
47
48
	protected function setUp()
49
	{
50
		$this->context = \TestHelperMShop::getContext();
51
		$this->editor = $this->context->getEditor();
52
		$this->object = new \Aimeos\MShop\Index\Manager\Standard( $this->context );
53
	}
54
55
56
	protected function tearDown()
57
	{
58
		unset( $this->object );
59
	}
60
61
62
	public function testCreateItem()
63
	{
64
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Product\\Item\\Iface', $this->object->createItem() );
65
	}
66
67
68
	public function testCreateSearch()
69
	{
70
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $this->object->createSearch() );
71
	}
72
73
74
	public function testAggregate()
75
	{
76
		$manager = \Aimeos\MShop\Factory::createManager( \TestHelperMShop::getContext(), 'attribute' );
77
78
		$search = $manager->createSearch();
79
		$expr = array(
80
			$search->compare( '==', 'attribute.code', 'white' ),
81
			$search->compare( '==', 'attribute.domain', 'product' ),
82
			$search->compare( '==', 'attribute.type.code', 'color' ),
83
		);
84
		$search->setConditions( $search->combine( '&&', $expr ) );
85
86
		$items = $manager->searchItems( $search );
87
88
		if( ( $item = reset( $items ) ) === false ) {
89
			throw new \RuntimeException( 'No attribute found' );
90
		}
91
92
93
		$search = $this->object->createSearch( true );
94
		$result = $this->object->aggregate( $search, 'index.attribute.id' );
95
96
		$this->assertEquals( 14, count( $result ) );
97
		$this->assertArrayHasKey( $item->getId(), $result );
98
		$this->assertEquals( 4, $result[$item->getId()] );
99
	}
100
101
102
	public function testGetItem()
103
	{
104
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( $this->context );
105
		$search = $productManager->createSearch();
106
		$search->setSlice( 0, 1 );
107
		$result = $productManager->searchItems( $search );
108
109
		if( ( $expected = reset( $result ) ) === false ) {
110
			throw new \RuntimeException( 'No item found' );
111
		}
112
113
		$item = $this->object->getItem( $expected->getId() );
114
		$this->assertEquals( $expected, $item );
115
	}
116
117
118
	public function testGetResourceType()
119
	{
120
		$result = $this->object->getResourceType();
121
122
		$this->assertContains( 'index', $result );
123
		$this->assertContains( 'index/attribute', $result );
124
		$this->assertContains( 'index/catalog', $result );
125
		$this->assertContains( 'index/price', $result );
126
		$this->assertContains( 'index/text', $result );
127
	}
128
129
130
	public function testGetSearchAttributes()
131
	{
132
		foreach( $this->object->getSearchAttributes() as $attribute ) {
133
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
134
		}
135
	}
136
137
138
	public function testSaveDeleteItem()
139
	{
140
		$item = self::$products['CNE'];
141
142
		$context = $this->context;
143
		$dbm = $context->getDatabaseManager();
144
		$siteId = $context->getLocale()->getSiteId();
145
146
		$sqlAttribute = 'SELECT COUNT(*) as count FROM "mshop_index_attribute" WHERE "siteid" = ? AND "prodid" = ?';
147
		$sqlCatalog = 'SELECT COUNT(*) as count FROM "mshop_index_catalog" WHERE "siteid" = ? AND "prodid" = ?';
148
		$sqlPrice = 'SELECT COUNT(*) as count FROM "mshop_index_price" WHERE "siteid" = ? AND "prodid" = ?';
149
		$sqlText = 'SELECT COUNT(*) as count FROM "mshop_index_text" WHERE "siteid" = ? AND "prodid" = ?';
150
151
		$this->object->saveItem( $item );
152
153
		$cntAttributeA = $this->getValue( $dbm, $sqlAttribute, 'count', $siteId, $item->getId() );
154
		$cntCatalogA = $this->getValue( $dbm, $sqlCatalog, 'count', $siteId, $item->getId() );
155
		$cntPriceA = $this->getValue( $dbm, $sqlPrice, 'count', $siteId, $item->getId() );
156
		$cntTextA = $this->getValue( $dbm, $sqlText, 'count', $siteId, $item->getId() );
157
158
159
		$this->object->deleteItem( $item->getId() );
160
161
		$cntAttributeB = $this->getValue( $dbm, $sqlAttribute, 'count', $siteId, $item->getId() );
162
		$cntCatalogB = $this->getValue( $dbm, $sqlCatalog, 'count', $siteId, $item->getId() );
163
		$cntPriceB = $this->getValue( $dbm, $sqlPrice, 'count', $siteId, $item->getId() );
164
		$cntTextB = $this->getValue( $dbm, $sqlText, 'count', $siteId, $item->getId() );
165
166
167
		// recreate index for CNE
168
		$result = $this->object->saveItem( $item );
169
170
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $result );
171
172
		$this->assertEquals( 7, $cntAttributeA );
173
		$this->assertEquals( 5, $cntCatalogA );
174
		$this->assertEquals( 2, $cntPriceA );
175
		$this->assertEquals( 19, $cntTextA );
176
177
		$this->assertEquals( 0, $cntAttributeB );
178
		$this->assertEquals( 0, $cntCatalogB );
179
		$this->assertEquals( 0, $cntPriceB );
180
		$this->assertEquals( 0, $cntTextB );
181
	}
182
183
184
	public function testSaveDeleteItemNoName()
185
	{
186
		$context = $this->context;
187
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( $context );
188
189
		$search = $productManager->createSearch();
190
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
191
		$result = $productManager->searchItems( $search );
192
193
		if( ( $item = reset( $result ) ) === false ) {
194
			throw new \RuntimeException( 'Product not available' );
195
		}
196
197
198
		$dbm = $context->getDatabaseManager();
199
		$siteId = $context->getLocale()->getSiteId();
200
		$langid = $context->getLocale()->getLanguageId();
201
202
		$sqlProd = 'SELECT "value" FROM "mshop_index_text"
203
			WHERE "siteid" = ? AND "prodid" = ? AND "langid" = \'' . $langid . '\'
204
				AND "type" = \'name\' AND domain = \'product\'';
205
		$sqlAttr = 'SELECT "value" FROM "mshop_index_text"
206
			WHERE "siteid" = ? AND "prodid" = ? AND type = \'name\' AND domain = \'attribute\'';
207
208
		$this->object->saveItem( $item );
209
		$attrText = $this->getValue( $dbm, $sqlAttr, 'value', $siteId, $item->getId() );
210
		$prodText = $this->getValue( $dbm, $sqlProd, 'value', $siteId, $item->getId() );
211
		$this->object->deleteItem( $item->getId() );
212
213
		$this->assertEquals( 'Unterproduct 3', $prodText );
214
		$this->assertEquals( 'XL', $attrText );
215
	}
216
217
218
	public function testSearchItems()
219
	{
220
		$total = 0;
221
		$search = $this->object->createSearch();
222
		$search->setSlice( 0, 1 );
223
224
		$expr = array(
225
			$search->compare( '~=', 'product.label', 'Cafe Noire' ),
226
			$search->compare( '==', 'product.editor', $this->editor ),
227
			$search->compare( '!=', 'index.catalog.id', null ),
228
		);
229
		$search->setConditions( $search->combine( '&&', $expr ) );
230
231
		$result = $this->object->searchItems( $search, [], $total );
232
233
		$this->assertEquals( 1, count( $result ) );
234
		$this->assertEquals( 2, $total );
235
236
237
		// with base criteria
238
		$search = $this->object->createSearch( true );
239
		$conditions = array(
240
			$search->compare( '==', 'product.editor', $this->editor ),
241
			$search->getConditions()
242
		);
243
		$search->setConditions( $search->combine( '&&', $conditions ) );
244
		$products = $this->object->searchItems( $search );
245
		$this->assertEquals( 22, count( $products ) );
246
247
		foreach( $products as $itemId => $item ) {
248
			$this->assertEquals( $itemId, $item->getId() );
249
		}
250
251
252
		$search = $this->object->createSearch( true );
253
		$expr = array(
254
			$search->compare( '==', 'product.code', array( 'CNC', 'CNE' ) ),
255
			$search->compare( '==', 'product.editor', $this->editor ),
256
			$search->getConditions(),
257
		);
258
		$search->setConditions( $search->combine( '&&', $expr ) );
259
		$result = $this->object->searchItems( $search, array( 'media' ) );
260
261
		$this->assertEquals( 2, count( $result ) );
262
	}
263
264
265
	public function testSearchItemsAttribute()
266
	{
267
		$context = $this->context;
268
269
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( $context );
270
		$search = $attributeManager->createSearch();
271
		$conditions = array(
272
			$search->compare( '==', 'attribute.label', '29' ),
273
			$search->compare( '==', 'attribute.editor', $this->editor ),
274
			$search->compare( '==', 'attribute.type.domain', 'product' ),
275
			$search->compare( '==', 'attribute.type.code', 'width' ),
276
		);
277
		$search->setConditions( $search->combine( '&&', $conditions ) );
278
		$result = $attributeManager->searchItems( $search );
279
280
		if( ( $attrWidthItem = reset( $result ) ) === false ) {
281
			throw new \RuntimeException( 'No attribute item found' );
282
		}
283
284
		$expr = array(
285
			$search->compare( '==', 'attribute.label', '30' ),
286
			$search->compare( '==', 'attribute.editor', $this->editor ),
287
			$search->compare( '==', 'attribute.type.domain', 'product' ),
288
			$search->compare( '==', 'attribute.type.code', 'length' ),
289
		);
290
		$search->setConditions( $search->combine( '&&', $expr ) );
291
		$result = $attributeManager->searchItems( $search );
292
293
		if( ( $attrLenItem = reset( $result ) ) === false ) {
294
			throw new \RuntimeException( 'No attribute item found' );
295
		}
296
297
298
		$total = 0;
299
		$search = $this->object->createSearch();
300
		$search->setSlice( 0, 1 );
301
302
303
		$conditions = array(
304
			$search->compare( '==', 'index.attribute.id', $attrWidthItem->getId() ),
305
			$search->compare( '==', 'product.editor', $this->editor ),
306
		);
307
		$search->setConditions( $search->combine( '&&', $conditions ) );
308
		$result = $this->object->searchItems( $search, [], $total );
309
310
		$this->assertEquals( 1, count( $result ) );
311
		$this->assertEquals( 3, $total );
312
313
314
		$expr = array(
315
			$search->compare( '!=', 'index.attribute.id', null ),
316
			$search->compare( '!=', 'index.catalog.id', null ),
317
			$search->compare( '==', 'product.editor', $this->editor )
318
		);
319
320
		$search->setConditions( $search->combine( '&&', $expr ) );
321
		$result = $this->object->searchItems( $search, [], $total );
322
323
		$this->assertEquals( 1, count( $result ) );
324
		$this->assertEquals( 6, $total );
325
326
327
		$attrIds = array( (int) $attrWidthItem->getId(), (int) $attrLenItem->getId() );
328
		$func = $search->createFunction( 'index.attributecount', array( 'variant', $attrIds ) );
329
		$conditions = array(
330
			$search->compare( '==', $func, 2 ), // count attributes
331
			$search->compare( '==', 'product.editor', $this->editor )
332
		);
333
		$search->setConditions( $search->combine( '&&', $conditions ) );
334
335
		$result = $this->object->searchItems( $search, [], $total );
336
337
		$this->assertEquals( 1, count( $result ) );
338
		$this->assertEquals( 2, $total );
339
340
341
		$func = $search->createFunction( 'index.attribute.code', array( 'default', 'size' ) );
342
		$expr = array(
343
			$search->compare( '!=', 'index.catalog.id', null ),
344
			$search->compare( '~=', $func, 'x' ),
345
			$search->compare( '==', 'product.editor', $this->editor )
346
		);
347
		$search->setConditions( $search->combine( '&&', $expr ) );
348
349
		$result = $this->object->searchItems( $search, [], $total );
350
351
		$this->assertEquals( 1, count( $result ) );
352
		$this->assertEquals( 3, $total );
353
	}
354
355
356
	public function testSearchItemsCatalog()
357
	{
358
		$context = $this->context;
359
360
		$catalogManager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $context );
361
		$catSearch = $catalogManager->createSearch();
362
		$conditions = array(
363
			$catSearch->compare( '==', 'catalog.label', 'Kaffee' ),
364
			$catSearch->compare( '==', 'catalog.editor', $this->editor )
365
		);
366
		$catSearch->setConditions( $catSearch->combine( '&&', $conditions ) );
367
		$result = $catalogManager->searchItems( $catSearch );
368
369
		if( ( $catItem = reset( $result ) ) === false ) {
370
			throw new \RuntimeException( 'No catalog item found' );
371
		}
372
373
		$conditions = array(
374
			$catSearch->compare( '==', 'catalog.label', 'Neu' ),
375
			$catSearch->compare( '==', 'catalog.editor', $this->editor )
376
		);
377
		$catSearch->setConditions( $catSearch->combine( '&&', $conditions ) );
378
		$result = $catalogManager->searchItems( $catSearch );
379
380
		if( ( $catNewItem = reset( $result ) ) === false ) {
381
			throw new \RuntimeException( 'No catalog item found' );
382
		}
383
384
385
		$search = $this->object->createSearch();
386
		$search->setConditions( $search->compare( '==', 'product.editor', $this->editor ) );
387
		$sortfunc = $search->createFunction( 'sort:index.catalog.position', array( 'promotion', $catItem->getId() ) );
388
		$search->setSortations( array( $search->sort( '+', $sortfunc ) ) );
389
		$search->setSlice( 0, 1 );
390
391
		$this->assertEquals( 1, count( $this->object->searchItems( $search ) ) );
392
393
394
		$total = 0;
395
		$search = $this->object->createSearch();
396
		$search->setSlice( 0, 1 );
397
398
399
		$conditions = array(
400
			$search->compare( '==', 'index.catalog.id', $catItem->getId() ), // catalog ID
401
			$search->compare( '==', 'product.editor', $this->editor )
402
		);
403
		$search->setConditions( $search->combine( '&&', $conditions ) );
404
		$result = $this->object->searchItems( $search, [], $total );
405
406
		$this->assertEquals( 1, count( $result ) );
407
		$this->assertEquals( 2, $total );
408
409
410
		$conditions = array(
411
			$search->compare( '!=', 'index.catalog.id', null ), // catalog ID
412
			$search->compare( '==', 'product.editor', $this->editor )
413
		);
414
		$search->setConditions( $search->combine( '&&', $conditions ) );
415
		$result = $this->object->searchItems( $search, [], $total );
416
417
		$this->assertEquals( 1, count( $result ) );
418
		$this->assertEquals( 7, $total );
419
420
421
		$func = $search->createFunction( 'index.catalog.position', array( 'promotion', $catItem->getId() ) );
422
		$conditions = array(
423
			$search->compare( '>=', $func, 0 ), // position
424
			$search->compare( '==', 'product.editor', $this->editor )
425
		);
426
		$search->setConditions( $search->combine( '&&', $conditions ) );
427
428
		$sortfunc = $search->createFunction( 'sort:index.catalog.position', array( 'promotion', $catItem->getId() ) );
429
		$search->setSortations( array( $search->sort( '+', $sortfunc ) ) );
430
431
		$result = $this->object->searchItems( $search, [], $total );
432
433
		$this->assertEquals( 1, count( $result ) );
434
		$this->assertEquals( 2, $total );
435
436
437
		$catIds = array( (int) $catItem->getId(), (int) $catNewItem->getId() );
438
		$func = $search->createFunction( 'index.catalogcount', array( 'default', $catIds ) );
439
		$conditions = array(
440
			$search->compare( '==', $func, 2 ), // count categories
441
			$search->compare( '==', 'product.editor', $this->editor )
442
		);
443
		$search->setConditions( $search->combine( '&&', $conditions ) );
444
445
		$result = $this->object->searchItems( $search, [], $total );
446
447
		$this->assertEquals( 1, count( $result ) );
448
		$this->assertEquals( 1, $total );
449
	}
450
451
452
	public function testSearchItemsPrice()
453
	{
454
		$total = 0;
455
		$search = $this->object->createSearch();
456
		$search->setSlice( 0, 1 );
457
458
		$priceItems = self::$products['CNC']->getRefItems( 'price', 'default' );
459
		if( ( $priceItem = reset( $priceItems ) ) === false ) {
460
			throw new \RuntimeException( 'No price with type "default" available in product CNC' );
461
		}
462
463
		$conditions = array(
464
			$search->compare( '==', 'index.price.id', $priceItem->getId() ),
465
			$search->compare( '==', 'product.editor', $this->editor )
466
		);
467
		$search->setConditions( $search->combine( '&&', $conditions ) );
468
		$result = $this->object->searchItems( $search, [], $total );
469
470
		$this->assertEquals( 1, count( $result ) );
471
		$this->assertEquals( 2, $total );
472
473
474
		$expr = array(
475
			$search->compare( '!=', 'index.price.id', null ),
476
			$search->compare( '!=', 'index.catalog.id', null ),
477
			$search->compare( '==', 'product.editor', $this->editor )
478
		);
479
		$search->setConditions( $search->combine( '&&', $expr ) );
480
		$result = $this->object->searchItems( $search, [], $total );
481
482
		$this->assertEquals( 1, count( $result ) );
483
		$this->assertEquals( 6, $total );
484
485
486
		$func = $search->createFunction( 'index.price.value', array( 'default', 'EUR', 'default' ) );
487
		$expr = array(
488
			$search->compare( '!=', 'index.catalog.id', null ),
489
			$search->compare( '>=', $func, '18.00' ),
490
			$search->compare( '==', 'product.editor', $this->editor )
491
		);
492
		$search->setConditions( $search->combine( '&&', $expr ) );
493
494
		$sortfunc = $search->createFunction( 'sort:index.price.value', array( 'default', 'EUR', 'default' ) );
495
		$search->setSortations( array( $search->sort( '+', $sortfunc ) ) );
496
497
		$result = $this->object->searchItems( $search, [], $total );
498
499
		$this->assertEquals( 1, count( $result ) );
500
		$this->assertEquals( 5, $total );
501
	}
502
503
504
	public function testSearchItemsText()
505
	{
506
		$context = clone $this->context;
507
		$context->getConfig()->set( 'mshop/index/manager/text/name', 'Standard' );
508
		$object = new \Aimeos\MShop\Index\Manager\Standard( $context );
509
510
		$textItems = self::$products['CNC']->getRefItems( 'text', 'name' );
511
		if( ( $textItem = reset( $textItems ) ) === false ) {
512
			throw new \RuntimeException( 'No text with type "name" available in product CNC' );
513
		}
514
515
		$total = 0;
516
		$search = $this->object->createSearch();
517
		$search->setSlice( 0, 1 );
518
519
		$conditions = array(
520
			$search->compare( '==', 'index.text.id', $textItem->getId() ),
521
			$search->compare( '==', 'product.editor', $this->editor )
522
		);
523
		$search->setConditions( $search->combine( '&&', $conditions ) );
524
		$result = $object->searchItems( $search, [], $total );
525
526
		$this->assertEquals( 1, count( $result ) );
527
		$this->assertEquals( 1, $total );
528
529
530
		$expr = array(
531
			$search->compare( '!=', 'index.text.id', null ),
532
			$search->compare( '!=', 'index.catalog.id', null ),
533
			$search->compare( '==', 'product.editor', $this->editor )
534
		);
535
		$search->setConditions( $search->combine( '&&', $expr ) );
536
537
		$result = $object->searchItems( $search, [], $total );
538
539
		$this->assertEquals( 1, count( $result ) );
540
		$this->assertEquals( 6, $total );
541
542
543
		$func = $search->createFunction( 'index.text.relevance', array( 'unittype13', 'de', 'Expr' ) );
544
		$conditions = array(
545
			$search->compare( '>', $func, 0 ), // text relevance
546
			$search->compare( '==', 'product.editor', $this->editor )
547
		);
548
		$search->setConditions( $search->combine( '&&', $conditions ) );
549
550
		$sortfunc = $search->createFunction( 'sort:index.text.relevance', array( 'unittype13', 'de', 'Expr' ) );
551
		$search->setSortations( array( $search->sort( '+', $sortfunc ) ) );
552
553
		$result = $object->searchItems( $search, [], $total );
554
555
		$this->assertEquals( 1, count( $result ) );
556
		$this->assertEquals( 2, $total );
557
558
559
		$func = $search->createFunction( 'index.text.value', array( 'unittype13', 'de', 'name', 'product' ) );
560
		$conditions = array(
561
			$search->compare( '~=', $func, 'Expr' ), // text value
562
			$search->compare( '==', 'product.editor', $this->editor )
563
		);
564
		$search->setConditions( $search->combine( '&&', $conditions ) );
565
566
		$sortfunc = $search->createFunction( 'sort:index.text.value', array( 'default', 'de', 'name' ) );
567
		$search->setSortations( array( $search->sort( '+', $sortfunc ) ) );
568
569
		$result = $object->searchItems( $search, [], $total );
570
571
		$this->assertEquals( 1, count( $result ) );
572
		$this->assertEquals( 1, $total );
573
	}
574
575
576
	public function testSearchTexts()
577
	{
578
		$context = $this->context;
579
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( $context );
580
581
		$search = $productManager->createSearch();
582
		$conditions = array(
583
			$search->compare( '==', 'product.code', 'CNC' ),
584
			$search->compare( '==', 'product.editor', $this->editor )
585
		);
586
		$search->setConditions( $search->combine( '&&', $conditions ) );
587
		$result = $productManager->searchItems( $search );
588
589
		if( ( $product = reset( $result ) ) === false ) {
590
			throw new \RuntimeException( 'No product found' );
591
		}
592
593
594
		$langid = $context->getLocale()->getLanguageId();
595
596
		$textMgr = $this->object->getSubManager( 'text' );
597
598
		$search = $textMgr->createSearch();
599
		$expr = array(
600
			$search->compare( '>', $search->createFunction( 'index.text.relevance', array( 'unittype19', $langid, 'Cafe Noire Cap' ) ), 0 ),
601
			$search->compare( '>', $search->createFunction( 'index.text.value', array( 'unittype19', $langid, 'name', 'product' ) ), '' ),
602
		);
603
		$search->setConditions( $search->combine( '&&', $expr ) );
604
605
		$result = $textMgr->searchTexts( $search );
606
607
		$this->assertArrayHasKey( $product->getId(), $result );
608
		$this->assertContains( 'Cafe Noire Cappuccino', $result );
609
	}
610
611
612
	public function testOptimize()
613
	{
614
		$this->object->optimize();
615
	}
616
617
618
	public function testCleanupIndex()
619
	{
620
		$this->object->cleanupIndex( '1970-01-01 00:00:00' );
621
	}
622
623
624
	public function testRebuildIndexAll()
625
	{
626
		$config = $this->context->getConfig();
627
628
		$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $this->context );
629
		$search = $manager->createSearch( true );
630
		$search->setSlice( 0, 0x7fffffff );
631
632
		//delete whole catalog
633
		$this->object->deleteItems( array_keys( $manager->searchItems( $search ) ) );
634
635
		//build catalog with all products
636
		$config->set( 'mshop/index/manager/standard/index', 'all' );
637
		$this->object->rebuildIndex();
638
639
		$afterInsertAttr = $this->getCatalogSubDomainItems( 'index.attribute.id', 'attribute' );
640
		$afterInsertPrice = $this->getCatalogSubDomainItems( 'index.price.id', 'price' );
641
		$afterInsertText = $this->getCatalogSubDomainItems( 'index.text.id', 'text' );
642
		$afterInsertCat = $this->getCatalogSubDomainItems( 'index.catalog.id', 'catalog' );
643
644
		//restore index with categorized products only
645
		$config->set( 'mshop/index/manager/standard/index', 'categorized' );
646
		$this->object->rebuildIndex();
647
648
		$this->assertEquals( 13, count( $afterInsertAttr ) );
649
		$this->assertEquals( 11, count( $afterInsertPrice ) );
650
		$this->assertEquals( 13, count( $afterInsertText ) );
651
		$this->assertEquals( 8, count( $afterInsertCat ) );
652
	}
653
654
655
	public function testRebuildIndexWithList()
656
	{
657
		$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $this->context );
658
		$search = $manager->createSearch();
659
		$search->setSlice( 0, 0x7fffffff );
660
661
		//delete whole catalog
662
		$this->object->deleteItems( array_keys( $manager->searchItems( $search ) ) );
663
664
		$afterDeleteAttr = $this->getCatalogSubDomainItems( 'index.attribute.id', 'attribute' );
665
		$afterDeletePrice = $this->getCatalogSubDomainItems( 'index.price.id', 'price' );
666
		$afterDeleteText = $this->getCatalogSubDomainItems( 'index.text.id', 'text' );
667
		$afterDeleteCat = $this->getCatalogSubDomainItems( 'index.catalog.id', 'catalog' );
668
669
		//insert cne, cnc
670
		$search = $manager->createSearch();
671
		$search->setConditions( $search->compare( '==', 'product.code', array( 'CNE', 'CNC' ) ) );
672
		$items = $manager->searchItems( $search );
673
674
		$this->object->rebuildIndex( $items );
675
676
		$afterInsertAttr = $this->getCatalogSubDomainItems( 'index.attribute.id', 'attribute' );
677
		$afterInsertPrice = $this->getCatalogSubDomainItems( 'index.price.id', 'price' );
678
		$afterInsertText = $this->getCatalogSubDomainItems( 'index.text.id', 'text' );
679
		$afterInsertCat = $this->getCatalogSubDomainItems( 'index.catalog.id', 'catalog' );
680
681
		//delete cne, cnc
682
		foreach( $items as $item ) {
683
			$this->object->deleteItem( $item->getId() );
684
		}
685
686
		//restores catalog
687
		$this->object->rebuildIndex();
688
689
		//check delete
690
		$this->assertEquals( [], $afterDeleteAttr );
691
		$this->assertEquals( [], $afterDeletePrice );
692
		$this->assertEquals( [], $afterDeleteText );
693
		$this->assertEquals( [], $afterDeleteCat );
694
695
		//check inserted items
696
		$this->assertEquals( 2, count( $afterInsertAttr ) );
697
		$this->assertEquals( 2, count( $afterInsertPrice ) );
698
		$this->assertEquals( 2, count( $afterInsertText ) );
699
		$this->assertEquals( 2, count( $afterInsertCat ) );
700
	}
701
702
703
	public function testRebuildIndexCategorizedOnly()
704
	{
705
		$context = $this->context;
706
		$config = $context->getConfig();
707
708
		$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $context );
709
710
		//delete whole catalog
711
		$search = $manager->createSearch();
712
		$search->setSlice( 0, 0x7fffffff );
713
		$this->object->deleteItems( array_keys( $manager->searchItems( $search ) ) );
714
715
		$config->set( 'mshop/index/manager/standard/index', 'categorized' );
716
		$this->object->rebuildIndex();
717
718
		$afterInsertAttr = $this->getCatalogSubDomainItems( 'index.attribute.id', 'attribute' );
719
		$afterInsertPrice = $this->getCatalogSubDomainItems( 'index.price.id', 'price' );
720
		$afterInsertText = $this->getCatalogSubDomainItems( 'index.text.id', 'text' );
721
		$afterInsertCat = $this->getCatalogSubDomainItems( 'index.catalog.id', 'catalog' );
722
723
		//check inserted items
724
		$this->assertEquals( 7, count( $afterInsertAttr ) );
725
		$this->assertEquals( 7, count( $afterInsertPrice ) );
726
		$this->assertEquals( 7, count( $afterInsertText ) );
727
		$this->assertEquals( 8, count( $afterInsertCat ) );
728
	}
729
730
731
	/**
732
	 * Returns value of a catalog_index column.
733
	 *
734
	 * @param \Aimeos\MW\DB\Manager\Iface $dbm Database Manager for connection
735
	 * @param string $sql Specified db query to find only one value
736
	 * @param string $column Column where to search
737
	 * @param integer $siteId Siteid of the db entry
738
	 * @param integer $productId Product id
739
	 * @return string $value Value returned for specified sql statement
740
	 * @throws \Exception If column not available or error during a connection to db
741
	 */
742
	protected function getValue( \Aimeos\MW\DB\Manager\Iface $dbm, $sql, $column, $siteId, $productId )
743
	{
744
		$config = $this->context->getConfig();
745
746
		if( $config->get( 'resource/db-product' ) === null ) {
747
			$dbname = $config->get( 'resource/default', 'db' );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $dbname is correct as $config->get('resource/default', 'db') (which targets Aimeos\MW\Config\Iface::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
748
		} else {
749
			$dbname = 'db-product';
750
		}
751
752
		$conn = $dbm->acquire( $dbname );
753
754
		try
755
		{
756
			$stmt = $conn->create( $sql );
757
			$stmt->bind( 1, $siteId, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
758
			$stmt->bind( 2, $productId, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
759
			$result = $stmt->execute();
760
761
			if( ( $row = $result->fetch() ) === false ) {
762
				throw new \RuntimeException( 'No rows available' );
763
			}
764
765
			if( !isset( $row[$column] ) ) {
766
				throw new \RuntimeException( sprintf( 'Column "%1$s" not available for "%2$s"', $column, $sql ) );
767
			}
768
769
			$value = $row[$column];
770
771
			$dbm->release( $conn, $dbname );
772
		}
773
		catch( \Exception $e )
774
		{
775
			$dbm->release( $conn, $dbname );
776
			throw $e;
777
		}
778
779
		return $value;
780
	}
781
782
783
	/**
784
	 * Gets product items of index subdomains specified by the key.
785
	 *
786
	 * @param string $key Key for searchItems
787
	 * @param string $domain Subdomain of index manager
788
	 */
789
	protected function getCatalogSubDomainItems( $key, $domain )
790
	{
791
		$subIndex = $this->object->getSubManager( $domain );
792
		$search = $subIndex->createSearch();
793
794
		$expr = array(
795
			$search->compare( '!=', $key, null ),
796
			$search->compare( '==', 'product.editor', $this->editor )
797
		);
798
799
		$search->setConditions( $search->combine( '&&', $expr ) );
800
801
		return $subIndex->searchItems( $search );
802
	}
803
804
}
805