Skip to content

Commit

Permalink
MAGETWO-67745: The "Catalog Search indexer process unknown error" app…
Browse files Browse the repository at this point in the history
…ears after reindex
  • Loading branch information
arnie1947 authored and nikshostko committed May 18, 2017
1 parent f8c75a8 commit 76de251
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private function cacheProduct($cacheKey, \Magento\Catalog\Api\Data\ProductInterf

if ($this->cacheLimit && count($this->instances) > $this->cacheLimit) {
$offset = round($this->cacheLimit / -2);
$this->instancesById = array_slice($this->instancesById, $offset);
$this->instancesById = array_slice($this->instancesById, $offset, null, true);
$this->instances = array_slice($this->instances, $offset);
}
}
Expand Down
62 changes: 62 additions & 0 deletions app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ class ProductRepositoryTest extends \PHPUnit_Framework_TestCase
*/
private $serializerMock;

/**
* Product repository cache limit.
*
* @var int
*/
private $cacheLimit = 2;

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
Expand Down Expand Up @@ -337,6 +344,7 @@ function ($value) {
'mediaGalleryProcessor' => $this->mediaGalleryProcessor,
'collectionProcessor' => $this->collectionProcessorMock,
'serializer' => $this->serializerMock,
'cacheLimit' => $this->cacheLimit
]
);
}
Expand Down Expand Up @@ -469,6 +477,60 @@ public function testGetByIdForcedReload()
$this->assertEquals($this->productMock, $this->model->getById($identifier, $editMode, $storeId, true));
}

/**
* Test for getById() method if we try to get products when cache is already filled and is reduced.
*
* @return void
*/
public function testGetByIdWhenCacheReduced()
{
$result = [];
$expectedResult = [];
$productsCount = $this->cacheLimit * 2;

$productMocks = $this->getProductMocksForReducedCache();
$productFactoryInvMock = $this->productFactoryMock->expects($this->exactly($productsCount))
->method('create');
call_user_func_array([$productFactoryInvMock, 'willReturnOnConsecutiveCalls'], $productMocks);
$this->serializerMock->expects($this->atLeastOnce())->method('serialize');

for ($i = 1; $i <= $productsCount; $i ++) {
$product = $this->model->getById($i, false, 0);
$result[] = $product->getId();
$expectedResult[] = $i;
}

$this->assertEquals($expectedResult, $result);
}

/**
* Get product mocks for testGetByIdWhenCacheReduced() method.
*
* @return array
*/
private function getProductMocksForReducedCache()
{
$productMocks = [];

for ($i = 1; $i <= $this->cacheLimit * 2; $i ++) {
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->setMethods([
'getId',
'getSku',
'load',
'setData',
])
->getMock();
$productMock->expects($this->once())->method('load');
$productMock->expects($this->atLeastOnce())->method('getId')->willReturn($i);
$productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($i . uniqid());
$productMocks[] = $productMock;
}

return $productMocks;
}

/**
* Test forceReload parameter
*
Expand Down

0 comments on commit 76de251

Please sign in to comment.