Skip to content

Commit 1af98b5

Browse files
author
Karpenko, Oleksandr
committed
Merge remote-tracking branch 'origin/MAGETWO-60611' into pr-develop
2 parents 4bc615f + 8becbc8 commit 1af98b5

File tree

4 files changed

+96
-14
lines changed

4 files changed

+96
-14
lines changed

lib/internal/Magento/Framework/Filesystem/Directory/Write.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function renameFile($path, $newPath, WriteInterface $targetDirectory = nu
106106
$targetDirectory->create($this->driver->getParentDirectory($newPath));
107107
}
108108
$absolutePath = $this->driver->getAbsolutePath($this->path, $path);
109-
$absoluteNewPath = $targetDirectory->driver->getAbsolutePath($this->path, $newPath);
109+
$absoluteNewPath = $targetDirectory->getAbsolutePath($newPath);
110110
return $this->driver->rename($absolutePath, $absoluteNewPath, $targetDirectory->driver);
111111
}
112112

lib/internal/Magento/Framework/Filesystem/Test/Unit/Directory/WriteTest.php

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
*/
88
namespace Magento\Framework\Filesystem\Test\Unit\Directory;
99

10+
use Magento\Framework\Filesystem\Directory\WriteInterface;
11+
use Magento\Framework\Filesystem\DriverInterface;
12+
1013
class WriteTest extends \PHPUnit_Framework_TestCase
1114
{
1215
/**
@@ -68,7 +71,7 @@ protected function tearDown()
6871
public function testGetDriver()
6972
{
7073
$this->assertInstanceOf(
71-
\Magento\Framework\Filesystem\DriverInterface::class,
74+
DriverInterface::class,
7275
$this->write->getDriver(),
7376
'getDriver method expected to return instance of Magento\Framework\Filesystem\DriverInterface'
7477
);
@@ -90,8 +93,7 @@ public function testIsWritable()
9093

9194
public function testCreateSymlinkTargetDirectoryExists()
9295
{
93-
$targetDir = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
94-
->getMock();
96+
$targetDir = $this->getMockBuilder(WriteInterface::class)->getMock();
9597
$targetDir->driver = $this->driver;
9698
$sourcePath = 'source/path/file';
9799
$destinationDirectory = 'destination/path';
@@ -159,4 +161,66 @@ private function getAbsolutePath($path)
159161
{
160162
return $this->path . $path;
161163
}
164+
165+
/**
166+
* @param string $sourcePath
167+
* @param string $targetPath
168+
* @param WriteInterface $targetDir
169+
* @dataProvider getFilePathsDataProvider
170+
*/
171+
public function testRenameFile($sourcePath, $targetPath, $targetDir)
172+
{
173+
if ($targetDir !== null) {
174+
$targetDir->driver = $this->getMockBuilder(DriverInterface::class)->getMockForAbstractClass();
175+
$targetDirPath = 'TARGET_PATH/';
176+
$targetDir->expects($this->once())
177+
->method('getAbsolutePath')
178+
->with($targetPath)
179+
->willReturn($targetDirPath . $targetPath);
180+
$targetDir->expects($this->once())
181+
->method('isExists')
182+
->with(dirname($targetPath))
183+
->willReturn(false);
184+
$targetDir->expects($this->once())
185+
->method('create')
186+
->with(dirname($targetPath));
187+
}
188+
189+
$this->driver->expects($this->any())
190+
->method('getAbsolutePath')
191+
->willReturnMap([
192+
[$this->path, $sourcePath, null, $this->getAbsolutePath($sourcePath)],
193+
[$this->path, $targetPath, null, $this->getAbsolutePath($targetPath)],
194+
]);
195+
$this->driver->expects($this->any())
196+
->method('isFile')
197+
->willReturnMap([
198+
[$this->getAbsolutePath($sourcePath), true],
199+
[$this->getAbsolutePath($targetPath), true],
200+
]);
201+
$this->driver->expects($this->any())
202+
->method('getParentDirectory')
203+
->with($targetPath)
204+
->willReturn(dirname($targetPath));
205+
$this->write->renameFile($sourcePath, $targetPath, $targetDir);
206+
}
207+
208+
/**
209+
* @return array
210+
*/
211+
public function getFilePathsDataProvider()
212+
{
213+
return [
214+
[
215+
'path/to/source.file',
216+
'path/to/target.file',
217+
null,
218+
],
219+
[
220+
'path/to/source.file',
221+
'path/to/target.file',
222+
$this->getMockBuilder(WriteInterface::class)->getMockForAbstractClass(),
223+
],
224+
];
225+
}
162226
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ public function __construct(
5050
public function merge(array $assetsToMerge, Asset\LocalInterface $resultAsset)
5151
{
5252
$mergedContent = $this->composeMergedContent($assetsToMerge, $resultAsset);
53-
$dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
54-
$dir->writeFile($resultAsset->getPath(), $mergedContent);
53+
$filePath = $resultAsset->getPath();
54+
$staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
55+
$tmpDir = $this->filesystem->getDirectoryWrite(DirectoryList::TMP);
56+
$tmpDir->writeFile($filePath, $mergedContent);
57+
$tmpDir->renameFile($filePath, $filePath, $staticDir);
5558
}
5659

5760
/**

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Framework\View\Test\Unit\Asset\MergeStrategy;
77

8+
use Magento\Framework\Filesystem\Directory\WriteInterface;
89
use \Magento\Framework\View\Asset\MergeStrategy\Direct;
910

1011
use Magento\Framework\App\Filesystem\DirectoryList;
@@ -22,9 +23,14 @@ class DirectTest extends \PHPUnit_Framework_TestCase
2223
protected $cssUrlResolver;
2324

2425
/**
25-
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem\Directory\WriteInterface
26+
* @var \PHPUnit_Framework_MockObject_MockObject|WriteInterface
2627
*/
27-
protected $writeDir;
28+
protected $staticDir;
29+
30+
/**
31+
* @var \PHPUnit_Framework_MockObject_MockObject|WriteInterface
32+
*/
33+
protected $tmpDir;
2834

2935
/**
3036
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Asset\LocalInterface
@@ -35,27 +41,34 @@ protected function setUp()
3541
{
3642
$this->cssUrlResolver = $this->getMock(\Magento\Framework\View\Url\CssResolver::class);
3743
$filesystem = $this->getMock(\Magento\Framework\Filesystem::class, [], [], '', false);
38-
$this->writeDir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
44+
$this->staticDir = $this->getMockBuilder(WriteInterface::class)->getMockForAbstractClass();
45+
$this->tmpDir = $this->getMockBuilder(WriteInterface::class)->getMockForAbstractClass();
3946
$filesystem->expects($this->any())
4047
->method('getDirectoryWrite')
41-
->with(DirectoryList::STATIC_VIEW)
42-
->will($this->returnValue($this->writeDir));
48+
->willReturnMap([
49+
[DirectoryList::STATIC_VIEW, \Magento\Framework\Filesystem\DriverPool::FILE, $this->staticDir],
50+
[DirectoryList::TMP, \Magento\Framework\Filesystem\DriverPool::FILE, $this->tmpDir],
51+
]);
4352
$this->resultAsset = $this->getMock(\Magento\Framework\View\Asset\File::class, [], [], '', false);
4453
$this->object = new Direct($filesystem, $this->cssUrlResolver);
4554
}
4655

4756
public function testMergeNoAssets()
4857
{
4958
$this->resultAsset->expects($this->once())->method('getPath')->will($this->returnValue('foo/result'));
50-
$this->writeDir->expects($this->once())->method('writeFile')->with('foo/result', '');
59+
$this->staticDir->expects($this->never())->method('writeFile');
60+
$this->tmpDir->expects($this->once())->method('writeFile')->with('foo/result', '');
61+
$this->tmpDir->expects($this->once())->method('renameFile')->with('foo/result', 'foo/result', $this->staticDir);
5162
$this->object->merge([], $this->resultAsset);
5263
}
5364

5465
public function testMergeGeneric()
5566
{
5667
$this->resultAsset->expects($this->once())->method('getPath')->will($this->returnValue('foo/result'));
5768
$assets = $this->prepareAssetsToMerge([' one', 'two']); // note leading space intentionally
58-
$this->writeDir->expects($this->once())->method('writeFile')->with('foo/result', 'onetwo');
69+
$this->staticDir->expects($this->never())->method('writeFile');
70+
$this->tmpDir->expects($this->once())->method('writeFile')->with('foo/result', 'onetwo');
71+
$this->tmpDir->expects($this->once())->method('renameFile')->with('foo/result', 'foo/result', $this->staticDir);
5972
$this->object->merge($assets, $this->resultAsset);
6073
}
6174

@@ -73,7 +86,9 @@ public function testMergeCss()
7386
->method('aggregateImportDirectives')
7487
->with('12')
7588
->will($this->returnValue('1020'));
76-
$this->writeDir->expects($this->once())->method('writeFile')->with('foo/result', '1020');
89+
$this->staticDir->expects($this->never())->method('writeFile');
90+
$this->tmpDir->expects($this->once())->method('writeFile')->with('foo/result', '1020');
91+
$this->tmpDir->expects($this->once())->method('renameFile')->with('foo/result', 'foo/result', $this->staticDir);
7792
$this->object->merge($assets, $this->resultAsset);
7893
}
7994

0 commit comments

Comments
 (0)