Skip to content

Commit 84ad348

Browse files
committed
Merge pull request #49 from magento-extensibility/MAGETWO-29705-Read-Write
[Extensibility] Magetwo 29705 read write
2 parents b3c9fff + 3ad89fc commit 84ad348

File tree

17 files changed

+86
-167
lines changed

17 files changed

+86
-167
lines changed

app/code/Magento/Backup/Model/Backup.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Backup\Model;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Filesystem\DriverPool;
910

1011
/**
1112
* Backup file item model
@@ -292,11 +293,10 @@ public function open($write = false)
292293

293294
try {
294295
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $varDirectory */
295-
$varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
296+
$varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR, DriverPool::ZLIB);
296297
$this->_stream = $varDirectory->openFile(
297298
$this->_getFilePath(),
298-
$mode,
299-
\Magento\Framework\Filesystem\DriverPool::ZLIB
299+
$mode
300300
);
301301
} catch (\Magento\Framework\Filesystem\FilesystemException $e) {
302302
throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions(

dev/tests/unit/testsuite/Magento/Framework/App/View/Asset/PublisherTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Framework\App\View\Asset;
88

99
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\Filesystem\DriverPool;
1011

1112
class PublisherTest extends \PHPUnit_Framework_TestCase
1213
{
@@ -56,8 +57,8 @@ protected function setUp()
5657
$this->filesystem->expects($this->any())
5758
->method('getDirectoryWrite')
5859
->will($this->returnValueMap([
59-
[DirectoryList::ROOT, $this->rootDirWrite],
60-
[DirectoryList::STATIC_VIEW, $this->staticDirWrite],
60+
[DirectoryList::ROOT, DriverPool::FILE, $this->rootDirWrite],
61+
[DirectoryList::STATIC_VIEW, DriverPool::FILE, $this->staticDirWrite],
6162
]));
6263
}
6364

dev/tests/unit/testsuite/Magento/Framework/Filesystem/Directory/ReadTest.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function testStat()
7777
$this->assertEquals(['some-stat-data'], $this->read->stat('correct-path'));
7878
}
7979

80-
public function testReadFileNoProtocol()
80+
public function testReadFile()
8181
{
8282
$path = 'filepath';
8383
$flag = 'flag';
@@ -95,32 +95,4 @@ public function testReadFileNoProtocol()
9595

9696
$this->assertEquals($contents, $this->read->readFile($path, $flag, $context));
9797
}
98-
99-
public function testReadFileCustomProtocol()
100-
{
101-
$path = 'filepath';
102-
$flag = 'flag';
103-
$context = 'context';
104-
$protocol = 'ftp';
105-
$contents = 'contents';
106-
107-
$fileMock = $this->getMock('Magento\Framework\Filesystem\File\Read', [], [], '', false);
108-
$fileMock->expects($this->once())
109-
->method('readAll')
110-
->with($flag, $context)
111-
->will($this->returnValue($contents));
112-
113-
$this->driver->expects($this->once())
114-
->method('getAbsolutePath')
115-
->with($this->path, $path, $protocol)
116-
->will($this->returnValue($path));
117-
$this->driver->expects($this->never())
118-
->method('fileGetContents');
119-
$this->fileFactory->expects($this->once())
120-
->method('create')
121-
->with($path, $protocol, $this->driver)
122-
->will($this->returnValue($fileMock));
123-
124-
$this->assertEquals($contents, $this->read->readFile($path, $flag, $context, $protocol));
125-
}
12698
}

dev/tests/unit/testsuite/Magento/Framework/Filesystem/File/ReadFactoryTest.php

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,25 @@
1212
*/
1313
class ReadFactoryTest extends \PHPUnit_Framework_TestCase
1414
{
15-
/**
16-
* @param string|null $protocol
17-
* @param \PHPUnit_Framework_MockObject_MockObject|null $driver
18-
* @dataProvider createProvider
19-
*/
20-
public function testCreate($protocol, $driver)
15+
public function testCreate()
2116
{
2217
$driverPool = $this->getMock('Magento\Framework\Filesystem\DriverPool', ['getDriver']);
23-
if ($protocol) {
24-
$driverMock = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
25-
$driverMock->expects($this->any())->method('isExists')->willReturn(true);
26-
$driverPool->expects($this->once())->method('getDriver')->willReturn($driverMock);
27-
} else {
28-
$driverPool->expects($this->never())->method('getDriver');
29-
}
30-
$factory = new ReadFactory($driverPool);
31-
$result = $factory->create('path', $protocol, $driver);
32-
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Read', $result);
33-
}
34-
35-
/**
36-
* @return array
37-
*/
38-
public function createProvider()
39-
{
18+
$driverPool->expects($this->never())->method('getDriver');
4019
$driver = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
4120
$driver->expects($this->any())->method('isExists')->willReturn(true);
42-
return [
43-
[null, $driver],
44-
['custom_protocol', null]
45-
];
21+
$factory = new ReadFactory($driverPool);
22+
$result = $factory->create('path', $driver);
23+
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Read', $result);
4624
}
4725

48-
/**
49-
* @expectedException \InvalidArgumentException
50-
*/
51-
public function testCreateException()
26+
public function testCreateWithDriverCode()
5227
{
53-
$factory = new ReadFactory(new DriverPool());
54-
$factory->create('path');
28+
$driverPool = $this->getMock('Magento\Framework\Filesystem\DriverPool', ['getDriver']);
29+
$driverMock = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
30+
$driverMock->expects($this->any())->method('isExists')->willReturn(true);
31+
$driverPool->expects($this->once())->method('getDriver')->willReturn($driverMock);
32+
$factory = new ReadFactory($driverPool);
33+
$result = $factory->create('path', 'driverCode');
34+
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Read', $result);
5535
}
5636
}

dev/tests/unit/testsuite/Magento/Framework/Filesystem/File/WriteFactoryTest.php

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,41 @@
55
*/
66
namespace Magento\Framework\Filesystem\File;
77

8-
use Magento\Framework\Filesystem\DriverPool;
9-
108
/**
119
* Class WriteFactoryTest
1210
*/
1311
class WriteFactoryTest extends \PHPUnit_Framework_TestCase
1412
{
15-
/**
16-
* @param string|null $protocol
17-
* @param \PHPUnit_Framework_MockObject_MockObject|null $driver
18-
* @dataProvider createProvider
19-
*/
20-
public function testCreate($protocol, $driver)
13+
public function testCreate()
2114
{
2215
$driverPool = $this->getMock('Magento\Framework\Filesystem\DriverPool', ['getDriver']);
23-
if ($protocol) {
24-
$driverMock = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
25-
$driverMock->expects($this->any())->method('isExists')->willReturn(true);
26-
$driverPool->expects($this->once())->method('getDriver')->willReturn($driverMock);
27-
} else {
28-
$driverPool->expects($this->never())->method('getDriver');
29-
}
16+
$driverPool->expects($this->never())->method('getDriver');
3017
$factory = new WriteFactory($driverPool);
31-
$result = $factory->create('path', $protocol, $driver);
32-
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Write', $result);
33-
}
34-
35-
/**
36-
* @return array
37-
*/
38-
public function createProvider()
39-
{
4018
$driver = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
4119
$driver->expects($this->any())->method('isExists')->willReturn(true);
42-
return [
43-
[null, $driver],
44-
['custom_protocol', null]
45-
];
20+
$result = $factory->create('path', $driver);
21+
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Write', $result);
4622
}
4723

48-
/**
49-
* @expectedException \InvalidArgumentException
50-
*/
51-
public function testCreateException()
24+
public function testCreateWithDriverCode()
5225
{
53-
$factory = new WriteFactory(new DriverPool());
54-
$factory->create('path');
26+
$driverPool = $this->getMock('Magento\Framework\Filesystem\DriverPool', ['getDriver']);
27+
$driverMock = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
28+
$driverMock->expects($this->any())->method('isExists')->willReturn(true);
29+
$driverPool->expects($this->once())->method('getDriver')->willReturn($driverMock);
30+
$factory = new WriteFactory($driverPool);
31+
$result = $factory->create('path', 'driverCode');
32+
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Write', $result);
5533
}
5634

5735
public function testCreateWithMode()
5836
{
59-
$driver = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
60-
$driver->expects($this->any())->method('isExists')->willReturn(false);
6137
$driverPool = $this->getMock('Magento\Framework\Filesystem\DriverPool', ['getDriver']);
62-
$driverPool->expects($this->once())->method('getDriver')->with('protocol')->willReturn($driver);
38+
$driverPool->expects($this->never())->method('getDriver');
39+
$driver = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
40+
$driver->expects($this->any())->method('isExists')->willReturn(true);
6341
$factory = new WriteFactory($driverPool);
64-
$result = $factory->create('path', 'protocol', null, 'a+');
42+
$result = $factory->create('path', $driver, 'a+');
6543
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Write', $result);
6644
}
6745
}

dev/tests/unit/testsuite/Magento/Framework/Less/File/Collector/LibraryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public function setup()
7272
->will(
7373
$this->returnValueMap(
7474
[
75-
[DirectoryList::LIB_WEB, $this->libraryDirectoryMock],
76-
[DirectoryList::THEMES, $this->themesDirectoryMock],
75+
[DirectoryList::LIB_WEB, Filesystem\DriverPool::FILE, $this->libraryDirectoryMock],
76+
[DirectoryList::THEMES, Filesystem\DriverPool::FILE, $this->themesDirectoryMock],
7777
]
7878
)
7979
);

dev/tests/unit/testsuite/Magento/Framework/View/Asset/MinifiedTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Framework\View\Asset;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Filesystem\DriverPool;
910

1011
class MinifiedTest extends \PHPUnit_Framework_TestCase
1112
{
@@ -62,8 +63,8 @@ protected function setUp()
6263
$this->_filesystem->expects($this->any())
6364
->method('getDirectoryRead')
6465
->will($this->returnValueMap([
65-
[DirectoryList::STATIC_VIEW, $this->_staticViewDir],
66-
[DirectoryList::ROOT, $this->_rootDir],
66+
[DirectoryList::STATIC_VIEW, DriverPool::FILE, $this->_staticViewDir],
67+
[DirectoryList::ROOT, DriverPool::FILE, $this->_rootDir],
6768
]));
6869
$this->_filesystem->expects($this->any())
6970
->method('getDirectoryWrite')

dev/tests/unit/testsuite/Magento/Framework/View/Asset/SourceTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Framework\View\Asset;
88

99
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\Filesystem\DriverPool;
1011

1112
/**
1213
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -245,9 +246,9 @@ protected function initFilesystem()
245246
$this->varDir = $this->getMockForAbstractClass('Magento\Framework\Filesystem\Directory\WriteInterface');
246247

247248
$readDirMap = [
248-
[DirectoryList::ROOT, $this->rootDirRead],
249-
[DirectoryList::STATIC_VIEW, $this->staticDirRead],
250-
[DirectoryList::VAR_DIR, $this->varDir],
249+
[DirectoryList::ROOT, DriverPool::FILE, $this->rootDirRead],
250+
[DirectoryList::STATIC_VIEW, DriverPool::FILE, $this->staticDirRead],
251+
[DirectoryList::VAR_DIR, DriverPool::FILE, $this->varDir],
251252
];
252253

253254
$this->filesystem->expects($this->any())

dev/tests/unit/testsuite/Magento/Framework/View/Element/TemplateTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Framework\View\Element;
77

8+
use Magento\Framework\Filesystem\DriverPool;
9+
810
class TemplateTest extends \PHPUnit_Framework_TestCase
911
{
1012
/**
@@ -50,9 +52,9 @@ protected function setUp()
5052
$this->_filesystem->expects($this->any())
5153
->method('getDirectoryRead')
5254
->will($this->returnValueMap([
53-
[\Magento\Framework\App\Filesystem\DirectoryList::THEMES, $themesDirMock],
54-
[\Magento\Framework\App\Filesystem\DirectoryList::APP, $appDirMock],
55-
[\Magento\Framework\App\Filesystem\DirectoryList::ROOT, $this->rootDirMock],
55+
[\Magento\Framework\App\Filesystem\DirectoryList::THEMES, DriverPool::FILE, $themesDirMock],
56+
[\Magento\Framework\App\Filesystem\DirectoryList::APP, DriverPool::FILE, $appDirMock],
57+
[\Magento\Framework\App\Filesystem\DirectoryList::ROOT, DriverPool::FILE, $this->rootDirMock],
5658
]));
5759

5860
$this->_templateEngine = $this->getMock(

lib/internal/Magento/Framework/Filesystem.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
namespace Magento\Framework;
99

10-
use Magento\Framework\Filesystem\File\ReadInterface;
10+
use Magento\Framework\Filesystem\DriverPool;
1111

1212
class Filesystem
1313
{
@@ -52,30 +52,34 @@ public function __construct(
5252
}
5353

5454
/**
55-
* Create an instance of directory with write permissions
55+
* Create an instance of directory with read permissions
5656
*
57-
* @param string $code
57+
* @param string $directoryCode
58+
* @param string $driverCode
5859
* @return \Magento\Framework\Filesystem\Directory\ReadInterface
5960
*/
60-
public function getDirectoryRead($code)
61+
public function getDirectoryRead($directoryCode, $driverCode = DriverPool::FILE)
6162
{
63+
$code = $directoryCode . '_' . $driverCode;
6264
if (!array_key_exists($code, $this->readInstances)) {
63-
$this->readInstances[$code] = $this->readFactory->create($this->getDirPath($code));
65+
$this->readInstances[$code] = $this->readFactory->create($this->getDirPath($directoryCode), $driverCode);
6466
}
6567
return $this->readInstances[$code];
6668
}
6769

6870
/**
69-
* Create an instance of directory with read permissions
71+
* Create an instance of directory with write permissions
7072
*
71-
* @param string $code
73+
* @param string $directoryCode
74+
* @param string $driverCode
7275
* @return \Magento\Framework\Filesystem\Directory\WriteInterface
7376
* @throws \Magento\Framework\Filesystem\FilesystemException
7477
*/
75-
public function getDirectoryWrite($code)
78+
public function getDirectoryWrite($directoryCode, $driverCode = DriverPool::FILE)
7679
{
80+
$code = $directoryCode . '_' . $driverCode;
7781
if (!array_key_exists($code, $this->writeInstances)) {
78-
$this->writeInstances[$code] = $this->writeFactory->create($this->getDirPath($code));
82+
$this->writeInstances[$code] = $this->writeFactory->create($this->getDirPath($directoryCode), $driverCode);
7983
}
8084
return $this->writeInstances[$code];
8185
}

0 commit comments

Comments
 (0)