@@ -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