Skip to content

Commit

Permalink
Merge pull request #49 from magento-extensibility/MAGETWO-29705-Read-…
Browse files Browse the repository at this point in the history
…Write

[Extensibility] Magetwo 29705 read write
  • Loading branch information
vpelipenko committed Jan 22, 2015
2 parents b3c9fff + 3ad89fc commit 84ad348
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 167 deletions.
6 changes: 3 additions & 3 deletions app/code/Magento/Backup/Model/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Backup\Model;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\DriverPool;

/**
* Backup file item model
Expand Down Expand Up @@ -292,11 +293,10 @@ public function open($write = false)

try {
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $varDirectory */
$varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR, DriverPool::ZLIB);
$this->_stream = $varDirectory->openFile(
$this->_getFilePath(),
$mode,
\Magento\Framework\Filesystem\DriverPool::ZLIB
$mode
);
} catch (\Magento\Framework\Filesystem\FilesystemException $e) {
throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\Framework\App\View\Asset;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\DriverPool;

class PublisherTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -56,8 +57,8 @@ protected function setUp()
$this->filesystem->expects($this->any())
->method('getDirectoryWrite')
->will($this->returnValueMap([
[DirectoryList::ROOT, $this->rootDirWrite],
[DirectoryList::STATIC_VIEW, $this->staticDirWrite],
[DirectoryList::ROOT, DriverPool::FILE, $this->rootDirWrite],
[DirectoryList::STATIC_VIEW, DriverPool::FILE, $this->staticDirWrite],
]));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testStat()
$this->assertEquals(['some-stat-data'], $this->read->stat('correct-path'));
}

public function testReadFileNoProtocol()
public function testReadFile()
{
$path = 'filepath';
$flag = 'flag';
Expand All @@ -95,32 +95,4 @@ public function testReadFileNoProtocol()

$this->assertEquals($contents, $this->read->readFile($path, $flag, $context));
}

public function testReadFileCustomProtocol()
{
$path = 'filepath';
$flag = 'flag';
$context = 'context';
$protocol = 'ftp';
$contents = 'contents';

$fileMock = $this->getMock('Magento\Framework\Filesystem\File\Read', [], [], '', false);
$fileMock->expects($this->once())
->method('readAll')
->with($flag, $context)
->will($this->returnValue($contents));

$this->driver->expects($this->once())
->method('getAbsolutePath')
->with($this->path, $path, $protocol)
->will($this->returnValue($path));
$this->driver->expects($this->never())
->method('fileGetContents');
$this->fileFactory->expects($this->once())
->method('create')
->with($path, $protocol, $this->driver)
->will($this->returnValue($fileMock));

$this->assertEquals($contents, $this->read->readFile($path, $flag, $context, $protocol));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,25 @@
*/
class ReadFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @param string|null $protocol
* @param \PHPUnit_Framework_MockObject_MockObject|null $driver
* @dataProvider createProvider
*/
public function testCreate($protocol, $driver)
public function testCreate()
{
$driverPool = $this->getMock('Magento\Framework\Filesystem\DriverPool', ['getDriver']);
if ($protocol) {
$driverMock = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
$driverMock->expects($this->any())->method('isExists')->willReturn(true);
$driverPool->expects($this->once())->method('getDriver')->willReturn($driverMock);
} else {
$driverPool->expects($this->never())->method('getDriver');
}
$factory = new ReadFactory($driverPool);
$result = $factory->create('path', $protocol, $driver);
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Read', $result);
}

/**
* @return array
*/
public function createProvider()
{
$driverPool->expects($this->never())->method('getDriver');
$driver = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
$driver->expects($this->any())->method('isExists')->willReturn(true);
return [
[null, $driver],
['custom_protocol', null]
];
$factory = new ReadFactory($driverPool);
$result = $factory->create('path', $driver);
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Read', $result);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testCreateException()
public function testCreateWithDriverCode()
{
$factory = new ReadFactory(new DriverPool());
$factory->create('path');
$driverPool = $this->getMock('Magento\Framework\Filesystem\DriverPool', ['getDriver']);
$driverMock = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
$driverMock->expects($this->any())->method('isExists')->willReturn(true);
$driverPool->expects($this->once())->method('getDriver')->willReturn($driverMock);
$factory = new ReadFactory($driverPool);
$result = $factory->create('path', 'driverCode');
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Read', $result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,41 @@
*/
namespace Magento\Framework\Filesystem\File;

use Magento\Framework\Filesystem\DriverPool;

/**
* Class WriteFactoryTest
*/
class WriteFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @param string|null $protocol
* @param \PHPUnit_Framework_MockObject_MockObject|null $driver
* @dataProvider createProvider
*/
public function testCreate($protocol, $driver)
public function testCreate()
{
$driverPool = $this->getMock('Magento\Framework\Filesystem\DriverPool', ['getDriver']);
if ($protocol) {
$driverMock = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
$driverMock->expects($this->any())->method('isExists')->willReturn(true);
$driverPool->expects($this->once())->method('getDriver')->willReturn($driverMock);
} else {
$driverPool->expects($this->never())->method('getDriver');
}
$driverPool->expects($this->never())->method('getDriver');
$factory = new WriteFactory($driverPool);
$result = $factory->create('path', $protocol, $driver);
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Write', $result);
}

/**
* @return array
*/
public function createProvider()
{
$driver = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
$driver->expects($this->any())->method('isExists')->willReturn(true);
return [
[null, $driver],
['custom_protocol', null]
];
$result = $factory->create('path', $driver);
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Write', $result);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testCreateException()
public function testCreateWithDriverCode()
{
$factory = new WriteFactory(new DriverPool());
$factory->create('path');
$driverPool = $this->getMock('Magento\Framework\Filesystem\DriverPool', ['getDriver']);
$driverMock = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
$driverMock->expects($this->any())->method('isExists')->willReturn(true);
$driverPool->expects($this->once())->method('getDriver')->willReturn($driverMock);
$factory = new WriteFactory($driverPool);
$result = $factory->create('path', 'driverCode');
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Write', $result);
}

public function testCreateWithMode()
{
$driver = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
$driver->expects($this->any())->method('isExists')->willReturn(false);
$driverPool = $this->getMock('Magento\Framework\Filesystem\DriverPool', ['getDriver']);
$driverPool->expects($this->once())->method('getDriver')->with('protocol')->willReturn($driver);
$driverPool->expects($this->never())->method('getDriver');
$driver = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
$driver->expects($this->any())->method('isExists')->willReturn(true);
$factory = new WriteFactory($driverPool);
$result = $factory->create('path', 'protocol', null, 'a+');
$result = $factory->create('path', $driver, 'a+');
$this->assertInstanceOf('Magento\Framework\Filesystem\File\Write', $result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public function setup()
->will(
$this->returnValueMap(
[
[DirectoryList::LIB_WEB, $this->libraryDirectoryMock],
[DirectoryList::THEMES, $this->themesDirectoryMock],
[DirectoryList::LIB_WEB, Filesystem\DriverPool::FILE, $this->libraryDirectoryMock],
[DirectoryList::THEMES, Filesystem\DriverPool::FILE, $this->themesDirectoryMock],
]
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Framework\View\Asset;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\DriverPool;

class MinifiedTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -62,8 +63,8 @@ protected function setUp()
$this->_filesystem->expects($this->any())
->method('getDirectoryRead')
->will($this->returnValueMap([
[DirectoryList::STATIC_VIEW, $this->_staticViewDir],
[DirectoryList::ROOT, $this->_rootDir],
[DirectoryList::STATIC_VIEW, DriverPool::FILE, $this->_staticViewDir],
[DirectoryList::ROOT, DriverPool::FILE, $this->_rootDir],
]));
$this->_filesystem->expects($this->any())
->method('getDirectoryWrite')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\Framework\View\Asset;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\DriverPool;

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

$readDirMap = [
[DirectoryList::ROOT, $this->rootDirRead],
[DirectoryList::STATIC_VIEW, $this->staticDirRead],
[DirectoryList::VAR_DIR, $this->varDir],
[DirectoryList::ROOT, DriverPool::FILE, $this->rootDirRead],
[DirectoryList::STATIC_VIEW, DriverPool::FILE, $this->staticDirRead],
[DirectoryList::VAR_DIR, DriverPool::FILE, $this->varDir],
];

$this->filesystem->expects($this->any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Framework\View\Element;

use Magento\Framework\Filesystem\DriverPool;

class TemplateTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -50,9 +52,9 @@ protected function setUp()
$this->_filesystem->expects($this->any())
->method('getDirectoryRead')
->will($this->returnValueMap([
[\Magento\Framework\App\Filesystem\DirectoryList::THEMES, $themesDirMock],
[\Magento\Framework\App\Filesystem\DirectoryList::APP, $appDirMock],
[\Magento\Framework\App\Filesystem\DirectoryList::ROOT, $this->rootDirMock],
[\Magento\Framework\App\Filesystem\DirectoryList::THEMES, DriverPool::FILE, $themesDirMock],
[\Magento\Framework\App\Filesystem\DirectoryList::APP, DriverPool::FILE, $appDirMock],
[\Magento\Framework\App\Filesystem\DirectoryList::ROOT, DriverPool::FILE, $this->rootDirMock],
]));

$this->_templateEngine = $this->getMock(
Expand Down
22 changes: 13 additions & 9 deletions lib/internal/Magento/Framework/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
namespace Magento\Framework;

use Magento\Framework\Filesystem\File\ReadInterface;
use Magento\Framework\Filesystem\DriverPool;

class Filesystem
{
Expand Down Expand Up @@ -52,30 +52,34 @@ public function __construct(
}

/**
* Create an instance of directory with write permissions
* Create an instance of directory with read permissions
*
* @param string $code
* @param string $directoryCode
* @param string $driverCode
* @return \Magento\Framework\Filesystem\Directory\ReadInterface
*/
public function getDirectoryRead($code)
public function getDirectoryRead($directoryCode, $driverCode = DriverPool::FILE)
{
$code = $directoryCode . '_' . $driverCode;
if (!array_key_exists($code, $this->readInstances)) {
$this->readInstances[$code] = $this->readFactory->create($this->getDirPath($code));
$this->readInstances[$code] = $this->readFactory->create($this->getDirPath($directoryCode), $driverCode);
}
return $this->readInstances[$code];
}

/**
* Create an instance of directory with read permissions
* Create an instance of directory with write permissions
*
* @param string $code
* @param string $directoryCode
* @param string $driverCode
* @return \Magento\Framework\Filesystem\Directory\WriteInterface
* @throws \Magento\Framework\Filesystem\FilesystemException
*/
public function getDirectoryWrite($code)
public function getDirectoryWrite($directoryCode, $driverCode = DriverPool::FILE)
{
$code = $directoryCode . '_' . $driverCode;
if (!array_key_exists($code, $this->writeInstances)) {
$this->writeInstances[$code] = $this->writeFactory->create($this->getDirPath($code));
$this->writeInstances[$code] = $this->writeFactory->create($this->getDirPath($directoryCode), $driverCode);
}
return $this->writeInstances[$code];
}
Expand Down
17 changes: 4 additions & 13 deletions lib/internal/Magento/Framework/Filesystem/Directory/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,13 @@ public function isReadable($path = null)
* Open file in read mode
*
* @param string $path
* @param string|null $protocol
*
* @return \Magento\Framework\Filesystem\File\ReadInterface
*/
public function openFile($path, $protocol = null)
public function openFile($path)
{
return $this->fileFactory->create(
$this->driver->getAbsolutePath($this->path, $path),
$protocol,
$this->driver
);
}
Expand All @@ -200,21 +198,14 @@ public function openFile($path, $protocol = null)
* @param string $path
* @param string|null $flag
* @param resource|null $context
* @param string|null $protocol
* @return string
* @throws FilesystemException
*/
public function readFile($path, $flag = null, $context = null, $protocol = null)
public function readFile($path, $flag = null, $context = null)
{
$absolutePath = $this->driver->getAbsolutePath($this->path, $path, $protocol);
$absolutePath = $this->driver->getAbsolutePath($this->path, $path);
return $this->driver->fileGetContents($absolutePath, $flag, $context);

if (is_null($protocol)) {
return $this->driver->fileGetContents($absolutePath, $flag, $context);
}

/** @var \Magento\Framework\Filesystem\File\Read $fileReader */
$fileReader = $this->fileFactory->create($absolutePath, $protocol, $this->driver);
return $fileReader->readAll($flag, $context);
}

/**
Expand Down
Loading

0 comments on commit 84ad348

Please sign in to comment.