Skip to content

Commit 315027f

Browse files
committed
ACPT-867: STRUCTURE LAYOUT cache generates
- Cover logic with unit tests;
1 parent e4ca3a4 commit 315027f

File tree

2 files changed

+156
-3
lines changed

2 files changed

+156
-3
lines changed

lib/internal/Magento/Framework/App/Cache/Type/Layout.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Framework\App\Cache\Type;
79

810
/**
@@ -13,17 +15,17 @@ class Layout extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
1315
/**
1416
* Prefix for hash kay and hash data
1517
*/
16-
private const HASH_PREFIX = 'l:';
18+
public const HASH_PREFIX = 'l:';
1719

1820
/**
1921
* Hash type, not used for security, only for uniqueness
2022
*/
21-
private const HASH_TYPE = 'xxh3';
23+
public const HASH_TYPE = 'xxh3';
2224

2325
/**
2426
* Data lifetime in milliseconds
2527
*/
26-
private const DATA_LIFETIME = 86_400_000; // "1 day" milliseconds
28+
public const DATA_LIFETIME = 86_400_000; // "1 day" milliseconds
2729

2830
/**
2931
* Cache type code unique among all cache types
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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\Framework\App\Test\Unit\Cache;
9+
10+
use Magento\Framework\App\Cache\Type\FrontendPool;
11+
use Magento\Framework\App\Cache\Type\Layout;
12+
use Magento\Framework\Cache\FrontendInterface;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class LayoutTest extends TestCase
17+
{
18+
/**
19+
* @var FrontendInterface|MockObject
20+
*/
21+
private $cacheFrontendMock;
22+
23+
/**
24+
* @var Layout
25+
*/
26+
private $layoutCacheType;
27+
28+
protected function setUp(): void
29+
{
30+
$this->cacheFrontendMock = $this->createMock(FrontendInterface::class);
31+
$frontendPoolMock = $this->createMock(FrontendPool::class);
32+
$frontendPoolMock->method('get')
33+
->with(Layout::TYPE_IDENTIFIER)
34+
->willReturn($this->cacheFrontendMock);
35+
36+
$this->layoutCacheType = new Layout($frontendPoolMock);
37+
}
38+
39+
/**
40+
* @param string $data
41+
* @param string $identifier
42+
* @return void
43+
* @dataProvider dataProviderForSaveDataTest
44+
*/
45+
public function testSaveData(string $data, string $identifier)
46+
{
47+
$tags = ['tag1', 'tag2'];
48+
$lifeTime = 1000;
49+
50+
$dataHash = hash(Layout::HASH_TYPE, $data);
51+
$identifierForHash = Layout::HASH_PREFIX . $dataHash;
52+
53+
$expectedTags = array_merge($tags, [Layout::CACHE_TAG]);
54+
$invocation = 0;
55+
56+
$this->cacheFrontendMock->method('save')
57+
->willReturnCallback(function ($passedData, $passedIdentifier, $passedTags, $passedLifeTime)
58+
use (&$invocation, $data, $identifierForHash, $expectedTags, $lifeTime, $identifier) {
59+
if ($invocation === 0) {
60+
$this->assertEquals($data, $passedData);
61+
$this->assertEquals($identifierForHash, $passedIdentifier);
62+
$this->assertEquals($expectedTags, $passedTags);
63+
$this->assertEquals(Layout::DATA_LIFETIME, $passedLifeTime);
64+
} elseif ($invocation === 1) {
65+
$this->assertEquals($identifierForHash, $passedData);
66+
$this->assertEquals($identifier, $passedIdentifier);
67+
$this->assertEquals($expectedTags, $passedTags);
68+
$this->assertEquals($lifeTime, $passedLifeTime);
69+
}
70+
$invocation++;
71+
return true;
72+
});
73+
74+
$result = $this->layoutCacheType->save($data, $identifier, $tags, $lifeTime);
75+
$this->assertTrue($result);
76+
}
77+
78+
/**
79+
* Data provider for the testSaveData test.
80+
*/
81+
public function dataProviderForSaveDataTest(): array
82+
{
83+
return [
84+
'Test with normal data' => ['test-data', 'test-identifier'],
85+
'Test with empty string' => ['', 'empty-string-identifier'],
86+
];
87+
}
88+
89+
/**
90+
* @param string $identifier
91+
* @param bool|string|null $firstLoadReturn
92+
* @param string|null $secondLoadReturn
93+
* @param bool|string|null $expectedResult
94+
* @return void
95+
* @dataProvider loadDataProvider
96+
*/
97+
public function testLoadData(
98+
string $identifier,
99+
bool|string|null $firstLoadReturn,
100+
?string $secondLoadReturn,
101+
bool|string|null $expectedResult
102+
) {
103+
$this->cacheFrontendMock->method('load')
104+
->willReturnMap([
105+
[$identifier, $firstLoadReturn],
106+
[$firstLoadReturn, $secondLoadReturn]
107+
]);
108+
109+
$result = $this->layoutCacheType->load($identifier);
110+
$this->assertEquals($expectedResult, $result);
111+
}
112+
113+
public function loadDataProvider(): array
114+
{
115+
$identifier = 'test-identifier';
116+
$hashedValue = Layout::HASH_PREFIX . hash(Layout::HASH_TYPE, 'test-data');
117+
$rawData = 'raw-test-data';
118+
$nonExistentIdentifier = 'non-existent-identifier';
119+
120+
return [
121+
// Test with hash prefix and successful nested load
122+
[
123+
'identifier' => $identifier,
124+
'firstLoadReturn' => $hashedValue,
125+
'secondLoadReturn' => 'test-data',
126+
'expectedResult' => 'test-data',
127+
],
128+
// Test when identifier does not exist
129+
[
130+
'identifier' => $nonExistentIdentifier,
131+
'firstLoadReturn' => false,
132+
'secondLoadReturn' => null, // Not used, since first load returns false
133+
'expectedResult' => false,
134+
],
135+
// Test with null value, assuming null is an acceptable return value
136+
[
137+
'identifier' => $nonExistentIdentifier,
138+
'firstLoadReturn' => null,
139+
'secondLoadReturn' => null, // Not used, since first load returns null
140+
'expectedResult' => null,
141+
],
142+
// Test without hash prefix
143+
[
144+
'identifier' => $identifier,
145+
'firstLoadReturn' => $rawData,
146+
'secondLoadReturn' => null, // Not used, since no hash prefix is present
147+
'expectedResult' => $rawData,
148+
],
149+
];
150+
}
151+
}

0 commit comments

Comments
 (0)