Skip to content

Commit 76de251

Browse files
arnie1947nikshostko
authored andcommitted
MAGETWO-67745: The "Catalog Search indexer process unknown error" appears after reindex
1 parent f8c75a8 commit 76de251

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private function cacheProduct($cacheKey, \Magento\Catalog\Api\Data\ProductInterf
308308

309309
if ($this->cacheLimit && count($this->instances) > $this->cacheLimit) {
310310
$offset = round($this->cacheLimit / -2);
311-
$this->instancesById = array_slice($this->instancesById, $offset);
311+
$this->instancesById = array_slice($this->instancesById, $offset, null, true);
312312
$this->instances = array_slice($this->instances, $offset);
313313
}
314314
}

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ class ProductRepositoryTest extends \PHPUnit_Framework_TestCase
153153
*/
154154
private $serializerMock;
155155

156+
/**
157+
* Product repository cache limit.
158+
*
159+
* @var int
160+
*/
161+
private $cacheLimit = 2;
162+
156163
/**
157164
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
158165
*/
@@ -337,6 +344,7 @@ function ($value) {
337344
'mediaGalleryProcessor' => $this->mediaGalleryProcessor,
338345
'collectionProcessor' => $this->collectionProcessorMock,
339346
'serializer' => $this->serializerMock,
347+
'cacheLimit' => $this->cacheLimit
340348
]
341349
);
342350
}
@@ -469,6 +477,60 @@ public function testGetByIdForcedReload()
469477
$this->assertEquals($this->productMock, $this->model->getById($identifier, $editMode, $storeId, true));
470478
}
471479

480+
/**
481+
* Test for getById() method if we try to get products when cache is already filled and is reduced.
482+
*
483+
* @return void
484+
*/
485+
public function testGetByIdWhenCacheReduced()
486+
{
487+
$result = [];
488+
$expectedResult = [];
489+
$productsCount = $this->cacheLimit * 2;
490+
491+
$productMocks = $this->getProductMocksForReducedCache();
492+
$productFactoryInvMock = $this->productFactoryMock->expects($this->exactly($productsCount))
493+
->method('create');
494+
call_user_func_array([$productFactoryInvMock, 'willReturnOnConsecutiveCalls'], $productMocks);
495+
$this->serializerMock->expects($this->atLeastOnce())->method('serialize');
496+
497+
for ($i = 1; $i <= $productsCount; $i ++) {
498+
$product = $this->model->getById($i, false, 0);
499+
$result[] = $product->getId();
500+
$expectedResult[] = $i;
501+
}
502+
503+
$this->assertEquals($expectedResult, $result);
504+
}
505+
506+
/**
507+
* Get product mocks for testGetByIdWhenCacheReduced() method.
508+
*
509+
* @return array
510+
*/
511+
private function getProductMocksForReducedCache()
512+
{
513+
$productMocks = [];
514+
515+
for ($i = 1; $i <= $this->cacheLimit * 2; $i ++) {
516+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
517+
->disableOriginalConstructor()
518+
->setMethods([
519+
'getId',
520+
'getSku',
521+
'load',
522+
'setData',
523+
])
524+
->getMock();
525+
$productMock->expects($this->once())->method('load');
526+
$productMock->expects($this->atLeastOnce())->method('getId')->willReturn($i);
527+
$productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($i . uniqid());
528+
$productMocks[] = $productMock;
529+
}
530+
531+
return $productMocks;
532+
}
533+
472534
/**
473535
* Test forceReload parameter
474536
*

0 commit comments

Comments
 (0)