Skip to content

Commit 8e6c1ac

Browse files
committed
Merge remote-tracking branch 'l3/ACP2E-52' into PR_L3_22_04_2022
2 parents 689396c + 9258dc3 commit 8e6c1ac

File tree

2 files changed

+160
-2
lines changed

2 files changed

+160
-2
lines changed

app/code/Magento/ConfigurableProduct/Model/Plugin/Frontend/UsedProductsCache.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
*/
2525
class UsedProductsCache
2626
{
27+
/**
28+
* Default cache life time: 1 year
29+
*/
30+
private const DEFAULT_CACHE_LIFE_TIME = 31536000;
31+
2732
/**
2833
* @var MetadataPool
2934
*/
@@ -49,25 +54,33 @@ class UsedProductsCache
4954
*/
5055
private $customerSession;
5156

57+
/**
58+
* @var int
59+
*/
60+
private $cacheLifeTime;
61+
5262
/**
5363
* @param MetadataPool $metadataPool
5464
* @param FrontendInterface $cache
5565
* @param SerializerInterface $serializer
5666
* @param ProductInterfaceFactory $productFactory
5767
* @param Session $customerSession
68+
* @param int $cacheLifeTime
5869
*/
5970
public function __construct(
6071
MetadataPool $metadataPool,
6172
FrontendInterface $cache,
6273
SerializerInterface $serializer,
6374
ProductInterfaceFactory $productFactory,
64-
Session $customerSession
75+
Session $customerSession,
76+
int $cacheLifeTime = self::DEFAULT_CACHE_LIFE_TIME
6577
) {
6678
$this->metadataPool = $metadataPool;
6779
$this->cache = $cache;
6880
$this->serializer = $serializer;
6981
$this->productFactory = $productFactory;
7082
$this->customerSession = $customerSession;
83+
$this->cacheLifeTime = $cacheLifeTime;
7184
}
7285

7386
/**
@@ -183,7 +196,7 @@ function ($item) {
183196
Configurable::TYPE_CODE . '_' . $product->getData($metadata->getLinkField()),
184197
]
185198
);
186-
$result = $this->cache->save($data, $cacheKey, $tags);
199+
$result = $this->cache->save($data, $cacheKey, $tags, $this->cacheLifeTime);
187200

188201
return (bool) $result;
189202
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ConfigurableProduct\Test\Unit\Model\Plugin\Frontend;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\ConfigurableProduct\Model\Plugin\Frontend\UsedProductsCache;
14+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
15+
use Magento\Customer\Model\Session;
16+
use Magento\Framework\Cache\FrontendInterface;
17+
use Magento\Framework\EntityManager\EntityMetadataInterface;
18+
use Magento\Framework\EntityManager\MetadataPool;
19+
use Magento\Framework\Serialize\SerializerInterface;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* Test case for Magento\ConfigurableProduct\Model\Plugin\Frontend\UsedProductsCache
25+
*/
26+
class UsedProductsCacheTest extends TestCase
27+
{
28+
/**
29+
* @var MetadataPool|MockObject
30+
*/
31+
private $metadataPool;
32+
33+
/**
34+
* @var FrontendInterface|MockObject
35+
*/
36+
private $cache;
37+
38+
/**
39+
* @var SerializerInterface|MockObject
40+
*/
41+
private $serializer;
42+
43+
/**
44+
* @var ProductInterfaceFactory|MockObject
45+
*/
46+
private $productFactory;
47+
48+
/**
49+
* @var Session|MockObject
50+
*/
51+
private $customerSession;
52+
53+
/**
54+
* @var UsedProductsCache
55+
*/
56+
private $model;
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
protected function setUp(): void
62+
{
63+
parent::setUp();
64+
$this->metadataPool = $this->createMock(MetadataPool::class);
65+
$this->cache = $this->getMockForAbstractClass(FrontendInterface::class);
66+
$this->serializer = $this->getMockForAbstractClass(SerializerInterface::class);
67+
$this->productFactory = $this->createMock(ProductInterfaceFactory::class);
68+
$this->customerSession = $this->createMock(Session::class);
69+
}
70+
71+
/**
72+
* Test that cache is saved with default expiration time
73+
*
74+
* @return void
75+
*/
76+
public function testDefaultLifeTime(): void
77+
{
78+
$lifeTime = 31536000;
79+
$this->model = new UsedProductsCache(
80+
$this->metadataPool,
81+
$this->cache,
82+
$this->serializer,
83+
$this->productFactory,
84+
$this->customerSession
85+
);
86+
$configurable = $this->createMock(Configurable::class);
87+
$configurableProduct = $this->createMock(Product::class);
88+
$configurableProduct->method('getIdentities')
89+
->willReturn([]);
90+
$this->metadataPool
91+
->method('getMetadata')
92+
->with(ProductInterface::class)
93+
->willReturn($this->getMockForAbstractClass(EntityMetadataInterface::class));
94+
$this->cache->expects($this->once())
95+
->method('save')
96+
->with($this->anything(), $this->anything(), $this->anything(), $lifeTime)
97+
->willReturn(true);
98+
$this->model->aroundGetUsedProducts(
99+
$configurable,
100+
function () {
101+
$product = $this->createMock(Product::class);
102+
return [$product];
103+
},
104+
$configurableProduct
105+
);
106+
}
107+
108+
/**
109+
* Test that cache is saved with custom expiration time
110+
*
111+
* @return void
112+
*/
113+
public function testCustomLifeTime(): void
114+
{
115+
$lifeTime = 60;
116+
$this->model = new UsedProductsCache(
117+
$this->metadataPool,
118+
$this->cache,
119+
$this->serializer,
120+
$this->productFactory,
121+
$this->customerSession,
122+
$lifeTime
123+
);
124+
$configurable = $this->createMock(Configurable::class);
125+
$configurableProduct = $this->createMock(Product::class);
126+
$configurableProduct->method('getIdentities')
127+
->willReturn([]);
128+
$this->metadataPool
129+
->method('getMetadata')
130+
->with(ProductInterface::class)
131+
->willReturn($this->getMockForAbstractClass(EntityMetadataInterface::class));
132+
$this->cache->expects($this->once())
133+
->method('save')
134+
->with($this->anything(), $this->anything(), $this->anything(), $lifeTime)
135+
->willReturn(true);
136+
$this->model->aroundGetUsedProducts(
137+
$configurable,
138+
function () {
139+
$product = $this->createMock(Product::class);
140+
return [$product];
141+
},
142+
$configurableProduct
143+
);
144+
}
145+
}

0 commit comments

Comments
 (0)