Skip to content

Commit 47aafa3

Browse files
Merge remote-tracking branch '29173/hotfix/29172' into comm_voted
2 parents 23806a0 + fe6c056 commit 47aafa3

File tree

2 files changed

+79
-62
lines changed

2 files changed

+79
-62
lines changed

lib/internal/Magento/Framework/View/Asset/MergeStrategy/Direct.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,31 @@
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Filesystem;
1011
use Magento\Framework\Math\Random;
1112
use Magento\Framework\View\Asset;
13+
use Magento\Framework\View\Asset\MergeStrategyInterface;
14+
use Magento\Framework\View\Url\CssResolver;
1215

1316
/**
1417
* The actual merging service
1518
*/
16-
class Direct implements \Magento\Framework\View\Asset\MergeStrategyInterface
19+
class Direct implements MergeStrategyInterface
1720
{
18-
/**#@+
21+
/**
1922
* Delimiters for merging files of various content type
2023
*/
21-
const MERGE_DELIMITER_JS = ';';
22-
23-
const MERGE_DELIMITER_EMPTY = '';
24+
private const MERGE_DELIMITER_JS = ';';
2425

25-
/**#@-*/
26+
private const MERGE_DELIMITER_EMPTY = '';
2627

27-
/**#@-*/
28+
/**
29+
* @var Filesystem
30+
*/
2831
private $filesystem;
2932

3033
/**
31-
* @var \Magento\Framework\View\Url\CssResolver
34+
* @var CssResolver
3235
*/
3336
private $cssUrlResolver;
3437

@@ -38,13 +41,13 @@ class Direct implements \Magento\Framework\View\Asset\MergeStrategyInterface
3841
private $mathRandom;
3942

4043
/**
41-
* @param \Magento\Framework\Filesystem $filesystem
42-
* @param \Magento\Framework\View\Url\CssResolver $cssUrlResolver
44+
* @param Filesystem $filesystem
45+
* @param CssResolver $cssUrlResolver
4346
* @param Random|null $mathRandom
4447
*/
4548
public function __construct(
46-
\Magento\Framework\Filesystem $filesystem,
47-
\Magento\Framework\View\Url\CssResolver $cssUrlResolver,
49+
Filesystem $filesystem,
50+
CssResolver $cssUrlResolver,
4851
Random $mathRandom = null
4952
) {
5053
$this->filesystem = $filesystem;
@@ -61,9 +64,8 @@ public function merge(array $assetsToMerge, Asset\LocalInterface $resultAsset)
6164
$filePath = $resultAsset->getPath();
6265
$tmpFilePath = $filePath . $this->mathRandom->getUniqueHash('_');
6366
$staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
64-
$tmpDir = $this->filesystem->getDirectoryWrite(DirectoryList::TMP);
65-
$tmpDir->writeFile($tmpFilePath, $mergedContent);
66-
$tmpDir->renameFile($tmpFilePath, $filePath, $staticDir);
67+
$staticDir->writeFile($tmpFilePath, $mergedContent);
68+
$staticDir->renameFile($tmpFilePath, $filePath, $staticDir);
6769
}
6870

6971
/**

lib/internal/Magento/Framework/View/Test/Unit/Asset/MergeStrategy/DirectTest.php

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,108 +24,123 @@
2424
*/
2525
class DirectTest extends TestCase
2626
{
27-
/**
28-
* @var Random|MockObject
29-
*/
30-
protected $mathRandomMock;
3127
/**
3228
* @var Direct
3329
*/
34-
protected $object;
30+
private $model;
3531

3632
/**
37-
* @var MockObject|CssResolver
33+
* @var Random|MockObject
3834
*/
39-
protected $cssUrlResolver;
35+
private $mathRandomMock;
4036

4137
/**
42-
* @var MockObject|WriteInterface
38+
* @var MockObject|CssResolver
4339
*/
44-
protected $staticDir;
40+
private $cssUrlResolverMock;
4541

4642
/**
4743
* @var MockObject|WriteInterface
4844
*/
49-
protected $tmpDir;
45+
private $staticDirMock;
5046

5147
/**
5248
* @var MockObject|LocalInterface
5349
*/
54-
protected $resultAsset;
50+
private $resultAssetMock;
5551

52+
/**
53+
* @inheridoc
54+
*/
5655
protected function setUp(): void
5756
{
58-
$this->cssUrlResolver = $this->createMock(CssResolver::class);
59-
$filesystem = $this->createMock(Filesystem::class);
60-
$this->staticDir = $this->getMockBuilder(WriteInterface::class)
61-
->getMockForAbstractClass();
62-
$this->tmpDir = $this->getMockBuilder(WriteInterface::class)
63-
->getMockForAbstractClass();
64-
$filesystem->expects($this->any())
57+
$this->cssUrlResolverMock = $this->createMock(CssResolver::class);
58+
$this->staticDirMock = $this->getMockForAbstractClass(WriteInterface::class);
59+
$tmpDir = $this->getMockForAbstractClass(WriteInterface::class);
60+
61+
$filesystemMock = $this->createMock(Filesystem::class);
62+
$filesystemMock->expects($this->any())
6563
->method('getDirectoryWrite')
6664
->willReturnMap([
67-
[DirectoryList::STATIC_VIEW, DriverPool::FILE, $this->staticDir],
68-
[DirectoryList::TMP, DriverPool::FILE, $this->tmpDir],
65+
[DirectoryList::STATIC_VIEW, DriverPool::FILE, $this->staticDirMock],
66+
[DirectoryList::TMP, DriverPool::FILE, $tmpDir],
6967
]);
70-
$this->resultAsset = $this->createMock(File::class);
71-
$this->mathRandomMock = $this->getMockBuilder(Random::class)
72-
->disableOriginalConstructor()
73-
->getMock();
74-
$this->object = new Direct($filesystem, $this->cssUrlResolver, $this->mathRandomMock);
68+
69+
$this->resultAssetMock = $this->createMock(File::class);
70+
$this->mathRandomMock = $this->createMock(Random::class);
71+
$this->model = new Direct($filesystemMock, $this->cssUrlResolverMock, $this->mathRandomMock);
7572
}
7673

7774
public function testMergeNoAssets()
7875
{
7976
$uniqId = '_b3bf82fa6e140594420fa90982a8e877';
80-
$this->resultAsset->expects($this->once())->method('getPath')->willReturn('foo/result');
81-
$this->staticDir->expects($this->never())->method('writeFile');
77+
$this->resultAssetMock->expects($this->once())
78+
->method('getPath')
79+
->willReturn('foo/result');
8280
$this->mathRandomMock->expects($this->once())
8381
->method('getUniqueHash')
8482
->willReturn($uniqId);
85-
$this->tmpDir->expects($this->once())->method('writeFile')->with('foo/result' . $uniqId, '');
86-
$this->tmpDir->expects($this->once())->method('renameFile')
87-
->with('foo/result' . $uniqId, 'foo/result', $this->staticDir);
88-
$this->object->merge([], $this->resultAsset);
83+
$this->staticDirMock->expects($this->once())
84+
->method('writeFile')
85+
->with('foo/result' . $uniqId, '');
86+
$this->staticDirMock->expects($this->once())
87+
->method('renameFile')
88+
->with('foo/result' . $uniqId, 'foo/result', $this->staticDirMock);
89+
90+
$this->model->merge([], $this->resultAssetMock);
8991
}
9092

9193
public function testMergeGeneric()
9294
{
9395
$uniqId = '_be50ccf992fd81818c1a2645d1a29e92';
94-
$this->resultAsset->expects($this->once())->method('getPath')->willReturn('foo/result');
9596
$assets = $this->prepareAssetsToMerge([' one', 'two']); // note leading space intentionally
96-
$this->staticDir->expects($this->never())->method('writeFile');
97+
98+
$this->resultAssetMock->expects($this->once())
99+
->method('getPath')
100+
->willReturn('foo/result');
101+
97102
$this->mathRandomMock->expects($this->once())
98103
->method('getUniqueHash')
99104
->willReturn($uniqId);
100-
$this->tmpDir->expects($this->once())->method('writeFile')->with('foo/result' . $uniqId, 'onetwo');
101-
$this->tmpDir->expects($this->once())->method('renameFile')
102-
->with('foo/result' . $uniqId, 'foo/result', $this->staticDir);
103-
$this->object->merge($assets, $this->resultAsset);
105+
106+
$this->staticDirMock->expects($this->once())
107+
->method('writeFile')
108+
->with('foo/result' . $uniqId, 'onetwo');
109+
$this->staticDirMock->expects($this->once())
110+
->method('renameFile')
111+
->with('foo/result' . $uniqId, 'foo/result', $this->staticDirMock);
112+
113+
$this->model->merge($assets, $this->resultAssetMock);
104114
}
105115

106116
public function testMergeCss()
107117
{
108118
$uniqId = '_f929c374767e00712449660ea673f2f5';
109-
$this->resultAsset->expects($this->exactly(3))
119+
$this->resultAssetMock->expects($this->exactly(3))
110120
->method('getPath')
111121
->willReturn('foo/result');
112-
$this->resultAsset->expects($this->any())->method('getContentType')->willReturn('css');
122+
$this->resultAssetMock->expects($this->atLeastOnce())
123+
->method('getContentType')
124+
->willReturn('css');
113125
$assets = $this->prepareAssetsToMerge(['one', 'two']);
114-
$this->cssUrlResolver->expects($this->exactly(2))
126+
$this->cssUrlResolverMock->expects($this->exactly(2))
115127
->method('relocateRelativeUrls')
116128
->will($this->onConsecutiveCalls('1', '2'));
117-
$this->cssUrlResolver->expects($this->once())
129+
$this->cssUrlResolverMock->expects($this->once())
118130
->method('aggregateImportDirectives')
119131
->with('12')
120132
->willReturn('1020');
121133
$this->mathRandomMock->expects($this->once())
122134
->method('getUniqueHash')
123135
->willReturn($uniqId);
124-
$this->staticDir->expects($this->never())->method('writeFile');
125-
$this->tmpDir->expects($this->once())->method('writeFile')->with('foo/result' . $uniqId, '1020');
126-
$this->tmpDir->expects($this->once())->method('renameFile')
127-
->with('foo/result' . $uniqId, 'foo/result', $this->staticDir);
128-
$this->object->merge($assets, $this->resultAsset);
136+
$this->staticDirMock->expects($this->once())
137+
->method('writeFile')
138+
->with('foo/result' . $uniqId, '1020');
139+
$this->staticDirMock->expects($this->once())
140+
->method('renameFile')
141+
->with('foo/result' . $uniqId, 'foo/result', $this->staticDirMock);
142+
143+
$this->model->merge($assets, $this->resultAssetMock);
129144
}
130145

131146
/**
@@ -134,7 +149,7 @@ public function testMergeCss()
134149
* @param array $data
135150
* @return array
136151
*/
137-
private function prepareAssetsToMerge(array $data)
152+
private function prepareAssetsToMerge(array $data): array
138153
{
139154
$result = [];
140155
foreach ($data as $content) {

0 commit comments

Comments
 (0)