ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,587 @@
|
||||
<?php
|
||||
/**
|
||||
* Test for \Magento\Framework\Filesystem\Directory\Read
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Filesystem\Directory;
|
||||
|
||||
use Magento\Framework\Exception\ValidatorException;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Class ReadTest
|
||||
* Test for Magento\Framework\Filesystem\Directory\Read class
|
||||
*/
|
||||
class ReadTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Test instance of Read
|
||||
*/
|
||||
public function testInstance()
|
||||
{
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
$this->assertTrue($dir instanceof ReadInterface);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for getAbsolutePath method
|
||||
*/
|
||||
public function testGetAbsolutePath()
|
||||
{
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
$this->assertStringContainsString('_files/foo', $dir->getAbsolutePath());
|
||||
$this->assertStringContainsString('_files/foo/bar', $dir->getAbsolutePath('bar'));
|
||||
}
|
||||
|
||||
public function testGetAbsolutePathOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->getAbsolutePath('../../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->getAbsolutePath('//./..///../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->getAbsolutePath('\..\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
public function testGetRelativePath()
|
||||
{
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
$this->assertEquals(
|
||||
'file_three.txt',
|
||||
$dir->getRelativePath('file_three.txt')
|
||||
);
|
||||
$this->assertEquals('', $dir->getRelativePath());
|
||||
$this->assertEquals('bar', $dir->getRelativePath(__DIR__ . '/../_files/foo/bar'));
|
||||
}
|
||||
|
||||
public function testGetRelativePathOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->getRelativePath(__DIR__ . '/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->getRelativePath(__DIR__ . '//./..////Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->getRelativePath(__DIR__ . '\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->getRelativePath('../../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(4, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for read method
|
||||
*
|
||||
* @dataProvider readProvider
|
||||
* @param string $dirPath
|
||||
* @param string $readPath
|
||||
* @param array $expectedResult
|
||||
*/
|
||||
public function testRead($dirPath, $readPath, $expectedResult)
|
||||
{
|
||||
$dir = $this->getDirectoryInstance($dirPath);
|
||||
$result = $dir->read($readPath);
|
||||
foreach ($expectedResult as $path) {
|
||||
$this->assertTrue(in_array($path, $result));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testRead
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function readProvider()
|
||||
{
|
||||
return [
|
||||
['foo', null, ['bar', 'file_three.txt']],
|
||||
['foo/bar', null, ['baz', 'file_two.txt']],
|
||||
['foo', 'bar', ['bar/baz', 'bar/file_two.txt']]
|
||||
];
|
||||
}
|
||||
|
||||
public function testReadOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->read('../../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->read('//./..///../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->read('\..\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for search method
|
||||
*
|
||||
* @dataProvider searchProvider
|
||||
* @param string $dirPath
|
||||
* @param string $pattern
|
||||
* @param array $expectedResult
|
||||
*/
|
||||
public function testSearch($dirPath, $pattern, $expectedResult)
|
||||
{
|
||||
$dir = $this->getDirectoryInstance($dirPath);
|
||||
$result = $dir->search($pattern);
|
||||
foreach ($expectedResult as $path) {
|
||||
$this->assertTrue(in_array($path, $result));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testSearch
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function searchProvider()
|
||||
{
|
||||
return [
|
||||
['foo', 'bar/*', ['bar/file_two.txt', 'bar/baz']],
|
||||
['foo', '/*/*.txt', ['bar/file_two.txt']],
|
||||
['foo', '/notfound/', []]
|
||||
];
|
||||
}
|
||||
|
||||
public function testSearchOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->search('/*/*.txt', '../../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->search('/*/*.txt', '//./..///../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->search('/*/*.txt', '\..\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for isExist method
|
||||
*
|
||||
* @dataProvider existsProvider
|
||||
* @param string $dirPath
|
||||
* @param string $path
|
||||
* @param bool $exists
|
||||
*/
|
||||
public function testIsExist($dirPath, $path, $exists)
|
||||
{
|
||||
$dir = $this->getDirectoryInstance($dirPath);
|
||||
$this->assertEquals($exists, $dir->isExist($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testIsExist
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function existsProvider()
|
||||
{
|
||||
return [
|
||||
['foo', 'bar', true],
|
||||
['foo', 'bar/baz', true],
|
||||
['foo', 'bar/notexists', false],
|
||||
['foo', 'foo/../bar', true],
|
||||
['foo', 'foo/../notexists', false]
|
||||
];
|
||||
}
|
||||
|
||||
public function testIsExistOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->isExist('../../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->isExist('//./..///../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->isExist('\..\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for stat method
|
||||
*
|
||||
* @dataProvider statProvider
|
||||
* @param string $dirPath
|
||||
* @param string $path
|
||||
*/
|
||||
public function testStat($dirPath, $path)
|
||||
{
|
||||
$dir = $this->getDirectoryInstance($dirPath);
|
||||
$expectedInfo = [
|
||||
'dev',
|
||||
'ino',
|
||||
'mode',
|
||||
'nlink',
|
||||
'uid',
|
||||
'gid',
|
||||
'rdev',
|
||||
'size',
|
||||
'atime',
|
||||
'mtime',
|
||||
'ctime',
|
||||
'blksize',
|
||||
'blocks',
|
||||
];
|
||||
$result = $dir->stat($path);
|
||||
foreach ($expectedInfo as $key) {
|
||||
$this->assertArrayHasKey($key, $result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testStat
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function statProvider()
|
||||
{
|
||||
return [['foo', 'bar'], ['foo', 'file_three.txt']];
|
||||
}
|
||||
|
||||
public function testStatOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->stat('bar/../../../Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->stat('bar//./..///../../Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->stat('bar\..\..\..\Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for isReadable method
|
||||
*
|
||||
* @dataProvider isReadableProvider
|
||||
* @param string $dirPath
|
||||
* @param string $path
|
||||
* @param bool $readable
|
||||
*/
|
||||
public function testIsReadable($dirPath, $path, $readable)
|
||||
{
|
||||
$dir = $this->getDirectoryInstance($dirPath);
|
||||
$this->assertEquals($readable, $dir->isReadable($path));
|
||||
}
|
||||
|
||||
public function testIsReadableOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->isReadable('../../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->isReadable('//./..///../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->isReadable('\..\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for isFile method
|
||||
*
|
||||
* @dataProvider isFileProvider
|
||||
* @param string $path
|
||||
* @param bool $isFile
|
||||
*/
|
||||
public function testIsFile($path, $isFile)
|
||||
{
|
||||
$this->assertEquals($isFile, $this->getDirectoryInstance('foo')->isFile($path));
|
||||
}
|
||||
|
||||
public function testIsFileOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->isFile('../../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->isFile('//./..///../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->isFile('\..\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for isDirectory method
|
||||
*
|
||||
* @dataProvider isDirectoryProvider
|
||||
* @param string $path
|
||||
* @param bool $isDirectory
|
||||
*/
|
||||
public function testIsDirectory($path, $isDirectory)
|
||||
{
|
||||
$this->assertEquals($isDirectory, $this->getDirectoryInstance('foo')->isDirectory($path));
|
||||
}
|
||||
|
||||
public function testIsDirectoryOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->isDirectory('../../Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->isDirectory('//./..///../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->isDirectory('\..\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testIsReadable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function isReadableProvider()
|
||||
{
|
||||
return [['foo', 'bar', true], ['foo', 'file_three.txt', true]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testIsFile
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function isFileProvider()
|
||||
{
|
||||
return [['bar', false], ['file_three.txt', true]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testIsDirectory
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function isDirectoryProvider()
|
||||
{
|
||||
return [['bar', true], ['file_three.txt', false]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for openFile method
|
||||
*/
|
||||
public function testOpenFile()
|
||||
{
|
||||
$file = $this->getDirectoryInstance('foo')->openFile('file_three.txt');
|
||||
$file->close();
|
||||
$this->assertTrue($file instanceof \Magento\Framework\Filesystem\File\ReadInterface);
|
||||
}
|
||||
|
||||
public function testOpenFileOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->openFile('../../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->openFile('//./..///../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->openFile('\..\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test readFile
|
||||
*
|
||||
* @dataProvider readFileProvider
|
||||
* @param string $path
|
||||
* @param string $content
|
||||
*/
|
||||
public function testReadFile($path, $content)
|
||||
{
|
||||
$directory = $this->getDirectoryInstance('');
|
||||
$this->assertEquals($content, $directory->readFile($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testReadFile
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function readFileProvider()
|
||||
{
|
||||
return [
|
||||
['popup.csv', 'var myData = 5;'],
|
||||
[
|
||||
'data.csv',
|
||||
'"field1", "field2"' . PHP_EOL . '"field3", "field4"' . PHP_EOL
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function testReadFileOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->readFile('../../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->readFile('//./..///../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->readFile('\..\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get readable file instance
|
||||
* Get full path for files located in _files directory
|
||||
*
|
||||
* @param string $path
|
||||
* @return Read
|
||||
*/
|
||||
private function getDirectoryInstance($path)
|
||||
{
|
||||
$fullPath = __DIR__ . '/../_files/' . $path;
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
/** @var \Magento\Framework\Filesystem\Directory\ReadFactory $directoryFactory */
|
||||
$directoryFactory = $objectManager->create(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
|
||||
return $directoryFactory->create($fullPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* test read recursively read
|
||||
*/
|
||||
public function testReadRecursively()
|
||||
{
|
||||
$expected = ['bar/baz/file_one.txt', 'bar', 'bar/baz', 'bar/file_two.txt', 'file_three.txt'];
|
||||
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
$actual = $dir->readRecursively('');
|
||||
$this->assertNotEquals($expected, $actual);
|
||||
sort($expected);
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function testReadRecursivelyOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('foo');
|
||||
try {
|
||||
$dir->readRecursively('../../Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->readRecursively('//./..///../Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->readRecursively('\..\..\Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,723 @@
|
||||
<?php
|
||||
/**
|
||||
* Test for \Magento\Framework\Filesystem\Directory\Write
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Filesystem\Directory;
|
||||
|
||||
use Magento\Framework\Exception\FileSystemException;
|
||||
use Magento\Framework\Exception\ValidatorException;
|
||||
use Magento\Framework\Filesystem\DriverPool;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class ReadTest
|
||||
* Test for Magento\Framework\Filesystem\Directory\Read class
|
||||
*/
|
||||
class WriteTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test data to be cleaned
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $testDirectories = [];
|
||||
|
||||
/**
|
||||
* Test instance of Read
|
||||
*/
|
||||
public function testInstance()
|
||||
{
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
$this->assertTrue($dir instanceof ReadInterface);
|
||||
$this->assertTrue($dir instanceof WriteInterface);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for create method
|
||||
*
|
||||
* @dataProvider createProvider
|
||||
* @param string $basePath
|
||||
* @param int $permissions
|
||||
* @param string $path
|
||||
* @throws FileSystemException
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function testCreate($basePath, $permissions, $path)
|
||||
{
|
||||
$directory = $this->getDirectoryInstance($basePath, $permissions);
|
||||
$this->assertTrue($directory->create($path));
|
||||
$this->assertTrue($directory->isExist($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testCreate
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function createProvider()
|
||||
{
|
||||
return [
|
||||
['newDir1', 0777, "newDir1"],
|
||||
['newDir1', 0777, "root_dir1/subdir1/subdir2"],
|
||||
['newDir2', 0777, "root_dir2/subdir"],
|
||||
['newDir1', 0777, "."]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for create outside
|
||||
*
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testCreateOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
try {
|
||||
$dir->create('../../outsideDir');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->create('//./..///../outsideDir');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->create('\..\..\outsideDir');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for delete method
|
||||
*
|
||||
* @dataProvider deleteProvider
|
||||
* @param string $path
|
||||
* @throws FileSystemException
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function testDelete($path)
|
||||
{
|
||||
$directory = $this->getDirectoryInstance('newDir', 0777);
|
||||
$directory->create($path);
|
||||
$this->assertTrue($directory->isExist($path));
|
||||
$directory->delete($path);
|
||||
$this->assertFalse($directory->isExist($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testDelete
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function deleteProvider()
|
||||
{
|
||||
return [['subdir'], ['subdir/subsubdir']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for delete outside
|
||||
*
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testDeleteOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
try {
|
||||
$dir->delete('../../Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->delete('//./..///../Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->delete('\..\..\Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for rename method (in scope of one directory instance)
|
||||
*
|
||||
* @dataProvider renameProvider
|
||||
* @param string $basePath
|
||||
* @param int $permissions
|
||||
* @param string $name
|
||||
* @param string $newName
|
||||
* @throws FileSystemException
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function testRename($basePath, $permissions, $name, $newName)
|
||||
{
|
||||
$directory = $this->getDirectoryInstance($basePath, $permissions);
|
||||
$directory->touch($name);
|
||||
$created = $directory->read();
|
||||
$directory->renameFile($name, $newName);
|
||||
$renamed = $directory->read();
|
||||
$this->assertTrue(in_array($name, $created));
|
||||
$this->assertTrue(in_array($newName, $renamed));
|
||||
$this->assertFalse(in_array($name, $renamed));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testRename
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function renameProvider()
|
||||
{
|
||||
return [['newDir1', 0777, 'first_name.txt', 'second_name.txt']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for rename outside
|
||||
*
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testRenameOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
try {
|
||||
$dir->renameFile('../../Directory/ReadTest.php', 'RenamedTest');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->renameFile(
|
||||
'//./..///../Directory/ReadTest.php',
|
||||
'RenamedTest'
|
||||
);
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->renameFile('\..\..\Directory\ReadTest.php', 'RenamedTest');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for rename method (moving to new directory instance)
|
||||
*
|
||||
* @dataProvider renameTargetDirProvider
|
||||
* @param string $firstDir
|
||||
* @param string $secondDir
|
||||
* @param int $permission
|
||||
* @param string $name
|
||||
* @param string $newName
|
||||
* @throws FileSystemException
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function testRenameTargetDir($firstDir, $secondDir, $permission, $name, $newName)
|
||||
{
|
||||
$dir1 = $this->getDirectoryInstance($firstDir, $permission);
|
||||
$dir2 = $this->getDirectoryInstance($secondDir, $permission);
|
||||
|
||||
$dir1->touch($name);
|
||||
$created = $dir1->read();
|
||||
$dir1->renameFile($name, $newName, $dir2);
|
||||
$oldPlace = $dir1->read();
|
||||
|
||||
$this->assertTrue(in_array($name, $created));
|
||||
$this->assertFalse(in_array($name, $oldPlace));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testRenameTargetDir
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function renameTargetDirProvider()
|
||||
{
|
||||
return [['dir1', 'dir2', 0777, 'first_name.txt', 'second_name.txt']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for copy method (copy in scope of one directory instance)
|
||||
*
|
||||
* @dataProvider renameProvider
|
||||
* @param string $basePath
|
||||
* @param int $permissions
|
||||
* @param string $name
|
||||
* @param string $newName
|
||||
* @throws ValidatorException
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testCopy($basePath, $permissions, $name, $newName)
|
||||
{
|
||||
$directory = $this->getDirectoryInstance($basePath, $permissions);
|
||||
$file = $directory->openFile($name, 'w+');
|
||||
$file->close();
|
||||
$directory->copyFile($name, $newName);
|
||||
$this->assertTrue($directory->isExist($name));
|
||||
$this->assertTrue($directory->isExist($newName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testCopy
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function copyProvider()
|
||||
{
|
||||
return [
|
||||
['newDir1', 0777, 'first_name.txt', 'second_name.txt'],
|
||||
['newDir1', 0777, 'subdir/first_name.txt', 'subdir/second_name.txt']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for copy outside
|
||||
*
|
||||
* @throws FileSystemException|ValidatorException
|
||||
*/
|
||||
public function testCopyOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
$dir->touch('test_file_for_copy_outside.txt');
|
||||
try {
|
||||
$dir->copyFile('../../Directory/ReadTest.php', 'CopiedTest');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->copyFile(
|
||||
'//./..///../Directory/ReadTest.php',
|
||||
'CopiedTest'
|
||||
);
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->copyFile('\..\..\Directory\ReadTest.php', 'CopiedTest');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->copyFile(
|
||||
'test_file_for_copy_outside.txt',
|
||||
'../../Directory/copied_outside.txt'
|
||||
);
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(4, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for copy method (copy to another directory instance)
|
||||
*
|
||||
* @dataProvider copyTargetDirProvider
|
||||
* @param string $firstDir
|
||||
* @param string $secondDir
|
||||
* @param int $permission
|
||||
* @param string $name
|
||||
* @param string $newName
|
||||
* @throws FileSystemException
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function testCopyTargetDir($firstDir, $secondDir, $permission, $name, $newName)
|
||||
{
|
||||
$dir1 = $this->getDirectoryInstance($firstDir, $permission);
|
||||
$dir2 = $this->getDirectoryInstance($secondDir, $permission);
|
||||
|
||||
$file = $dir1->openFile($name, 'w+');
|
||||
$file->close();
|
||||
$dir1->copyFile($name, $newName, $dir2);
|
||||
|
||||
$this->assertTrue($dir1->isExist($name));
|
||||
$this->assertTrue($dir2->isExist($newName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testCopyTargetDir
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function copyTargetDirProvider()
|
||||
{
|
||||
return [
|
||||
['dir1', 'dir2', 0777, 'first_name.txt', 'second_name.txt'],
|
||||
['dir1', 'dir2', 0777, 'subdir/first_name.txt', 'subdir/second_name.txt']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for changePermissions method
|
||||
*
|
||||
* @throws FileSystemException|ValidatorException
|
||||
*/
|
||||
public function testChangePermissions()
|
||||
{
|
||||
$directory = $this->getDirectoryInstance('newDir1', 0777);
|
||||
$directory->create('test_directory');
|
||||
$this->assertTrue($directory->changePermissions('test_directory', 0644));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for changePermissions outside
|
||||
*
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testChangePermissionsOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
try {
|
||||
$dir->changePermissions('../../Directory', 0777);
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->changePermissions('//./..///../Directory', 0777);
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->changePermissions('\..\..\Directory', 0777);
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for changePermissionsRecursively method
|
||||
*
|
||||
* @throws FileSystemException|ValidatorException
|
||||
*/
|
||||
public function testChangePermissionsRecursively()
|
||||
{
|
||||
$directory = $this->getDirectoryInstance('newDir1', 0777);
|
||||
$directory->create('test_directory');
|
||||
$directory->create('test_directory/subdirectory');
|
||||
$directory->writeFile('test_directory/subdirectory/test_file.txt', 'Test Content');
|
||||
|
||||
$this->assertTrue($directory->changePermissionsRecursively('test_directory', 0777, 0644));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for changePermissionsRecursively outside
|
||||
*
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testChangePermissionsRecursivelyOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
try {
|
||||
$dir->changePermissionsRecursively('../foo', 0777, 0777);
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->changePermissionsRecursively('//./..///foo', 0777, 0777);
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->changePermissionsRecursively('\..\foo', 0777, 0777);
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for touch method
|
||||
*
|
||||
* @dataProvider touchProvider
|
||||
* @param string $basePath
|
||||
* @param int $permissions
|
||||
* @param string $path
|
||||
* @param int $time
|
||||
* @throws FileSystemException
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function testTouch($basePath, $permissions, $path, $time)
|
||||
{
|
||||
$directory = $this->getDirectoryInstance($basePath, $permissions);
|
||||
$directory->openFile($path);
|
||||
$this->assertTrue($directory->touch($path, $time));
|
||||
$this->assertEquals($time, $directory->stat($path)['mtime']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testTouch
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function touchProvider()
|
||||
{
|
||||
return [
|
||||
['test_directory', 0777, 'touch_file.txt', time() - 3600],
|
||||
['test_directory', 0777, 'subdirectory/touch_file.txt', time() - 3600]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for touch outside
|
||||
*
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testTouchOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
try {
|
||||
$dir->touch('../../foo.tst');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->touch('//./..///../foo.tst');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->touch('\..\..\foo.tst');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test isWritable method
|
||||
*
|
||||
* @throws FileSystemException|ValidatorException
|
||||
*/
|
||||
public function testIsWritable()
|
||||
{
|
||||
$directory = $this->getDirectoryInstance('newDir1', 0777);
|
||||
$directory->create('bar');
|
||||
$this->assertFalse($directory->isWritable('not_existing_dir'));
|
||||
$this->assertTrue($directory->isWritable('bar'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test isWritable method outside
|
||||
*
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testIsWritableOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
try {
|
||||
$dir->isWritable('../../Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->isWritable('//./..///../Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->isWritable('\..\..\Directory');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for openFile method
|
||||
*
|
||||
* @dataProvider openFileProvider
|
||||
* @param string $basePath
|
||||
* @param int $permissions
|
||||
* @param string $path
|
||||
* @param string $mode
|
||||
* @throws FileSystemException
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function testOpenFile($basePath, $permissions, $path, $mode)
|
||||
{
|
||||
$directory = $this->getDirectoryInstance($basePath, $permissions);
|
||||
$file = $directory->openFile($path, $mode);
|
||||
$this->assertTrue($file instanceof \Magento\Framework\Filesystem\File\WriteInterface);
|
||||
$file->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testOpenFile
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function openFileProvider()
|
||||
{
|
||||
return [
|
||||
['newDir1', 0777, 'newFile.txt', 'w+'],
|
||||
['newDir1', 0777, 'subdirectory/newFile.txt', 'w+']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for openFile outside
|
||||
*
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testOpenFileOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
try {
|
||||
$dir->openFile('../../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->openFile('//./..///../Directory/ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->openFile('\..\..\Directory\ReadTest.php');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test writeFile
|
||||
*
|
||||
* @dataProvider writeFileProvider
|
||||
* @param string $path
|
||||
* @param string $content
|
||||
* @param string $extraContent
|
||||
* @throws FileSystemException
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function testWriteFile($path, $content, $extraContent)
|
||||
{
|
||||
$directory = $this->getDirectoryInstance('writeFileDir', 0777);
|
||||
$directory->writeFile($path, $content);
|
||||
$this->assertEquals($content, $directory->readFile($path));
|
||||
$directory->writeFile($path, $extraContent);
|
||||
$this->assertEquals($extraContent, $directory->readFile($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test writeFile for append mode
|
||||
*
|
||||
* @dataProvider writeFileProvider
|
||||
* @param string $path
|
||||
* @param string $content
|
||||
* @param string $extraContent
|
||||
* @throws FileSystemException
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function testWriteFileAppend($path, $content, $extraContent)
|
||||
{
|
||||
$directory = $this->getDirectoryInstance('writeFileDir', 0777);
|
||||
$directory->writeFile($path, $content, 'a+');
|
||||
$this->assertEquals($content, $directory->readFile($path));
|
||||
$directory->writeFile($path, $extraContent, 'a+');
|
||||
$this->assertEquals($content . $extraContent, $directory->readFile($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testWriteFile and testWriteFileAppend
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function writeFileProvider()
|
||||
{
|
||||
return [['file1', '123', '456'], ['folder1/file1', '123', '456']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for writeFile outside
|
||||
*
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testWriteFileOutside()
|
||||
{
|
||||
$exceptions = 0;
|
||||
$dir = $this->getDirectoryInstance('newDir1', 0777);
|
||||
try {
|
||||
$dir->writeFile('../../Directory/ReadTest.php', 'tst');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->writeFile('//./..///../Directory/ReadTest.php', 'tst');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
try {
|
||||
$dir->writeFile('\..\..\Directory\ReadTest.php', 'tst');
|
||||
} catch (ValidatorException $exception) {
|
||||
$exceptions++;
|
||||
}
|
||||
$this->assertEquals(3, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for invalidDeletePath
|
||||
*
|
||||
* @throws ValidatorException
|
||||
*/
|
||||
public function testInvalidDeletePath()
|
||||
{
|
||||
$this->expectException(FileSystemException::class);
|
||||
$directory = $this->getDirectoryInstance('newDir', 0777);
|
||||
$invalidPath = 'invalidPath/../';
|
||||
$directory->create($invalidPath);
|
||||
$directory->delete($invalidPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down
|
||||
*
|
||||
* @throws ValidatorException|FileSystemException
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/** @var Write $directory */
|
||||
foreach ($this->testDirectories as $directory) {
|
||||
if ($directory->isExist()) {
|
||||
$directory->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get readable file instance
|
||||
* Get full path for files located in _files directory
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $permissions
|
||||
* @return Write
|
||||
*/
|
||||
private function getDirectoryInstance($path, $permissions)
|
||||
{
|
||||
$fullPath = __DIR__ . '/../_files/' . $path;
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
/** @var WriteFactory $directoryFactory */
|
||||
$directoryFactory = $objectManager->create(WriteFactory::class);
|
||||
$directory = $directoryFactory->create($fullPath, DriverPool::FILE, $permissions);
|
||||
$this->testDirectories[] = $directory;
|
||||
return $directory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Framework\Filesystem\Driver;
|
||||
|
||||
use Magento\Framework\Exception\FileSystemException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for \Magento\Framework\Filesystem\Driver\File
|
||||
* Verify File class
|
||||
*/
|
||||
class FileTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var File
|
||||
*/
|
||||
private $driver;
|
||||
|
||||
/**
|
||||
* @var String
|
||||
*/
|
||||
private $absolutePath;
|
||||
|
||||
/**
|
||||
* @var String
|
||||
*/
|
||||
private $generatedPath;
|
||||
|
||||
/**
|
||||
* Returns relative path for the test.
|
||||
*
|
||||
* @param $relativePath
|
||||
* @return string
|
||||
*/
|
||||
protected function getTestPath($relativePath)
|
||||
{
|
||||
return $this->absolutePath . $relativePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->driver = new File();
|
||||
$this->absolutePath = dirname(__DIR__) . '/_files/';
|
||||
$this->generatedPath = $this->getTestPath('generated');
|
||||
$this->removeGeneratedDirectory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->removeGeneratedDirectory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests read directory with symlynked folders.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReadDirectoryRecursivelyWithSymlinkedFolders(): void
|
||||
{
|
||||
$sourceDirectory = $this->generatedPath . '/source';
|
||||
$destinationDirectory = $this->generatedPath . '/destination';
|
||||
|
||||
$this->driver->createDirectory($sourceDirectory);
|
||||
$this->driver->createDirectory($sourceDirectory . '/directory1');
|
||||
$this->driver->createDirectory($destinationDirectory);
|
||||
|
||||
$linkName = $destinationDirectory . '/link';
|
||||
$this->driver->symlink($sourceDirectory, $linkName);
|
||||
|
||||
$paths = [
|
||||
$destinationDirectory . '/link' . '/directory1',
|
||||
$destinationDirectory . '/link'
|
||||
|
||||
];
|
||||
$actual = $this->driver->readDirectoryRecursively($destinationDirectory);
|
||||
$this->assertEquals($paths, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests directory recursive read.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReadDirectoryRecursively(): void
|
||||
{
|
||||
$paths = [
|
||||
'foo/bar',
|
||||
'foo/bar/baz',
|
||||
'foo/bar/baz/file_one.txt',
|
||||
'foo/bar/file_two.txt',
|
||||
'foo/file_three.txt',
|
||||
];
|
||||
$expected = array_map([self::class, 'getTestPath'], $paths);
|
||||
$actual = $this->driver->readDirectoryRecursively($this->getTestPath('foo'));
|
||||
sort($actual);
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests directory reading exception.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReadDirectoryRecursivelyFailure(): void
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\FileSystemException::class);
|
||||
|
||||
$this->driver->readDirectoryRecursively($this->getTestPath('not-existing-directory'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests of directory creating.
|
||||
*
|
||||
* @throws FileSystemException
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateDirectory(): void
|
||||
{
|
||||
$generatedPath = $this->getTestPath('generated/roo/bar/baz/foo');
|
||||
$generatedPathBase = $this->getTestPath('generated');
|
||||
// Delete the generated directory if it already exists
|
||||
if (is_dir($generatedPath)) {
|
||||
$this->assertTrue($this->driver->deleteDirectory($generatedPathBase));
|
||||
}
|
||||
$this->assertTrue($this->driver->createDirectory($generatedPath));
|
||||
$this->assertTrue(is_dir($generatedPath));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creation and removing of symlinks.
|
||||
*
|
||||
* @throws FileSystemException
|
||||
* @return void
|
||||
*/
|
||||
public function testSymlinks(): void
|
||||
{
|
||||
$sourceDirectory = $this->generatedPath . '/source';
|
||||
$destinationDirectory = $this->generatedPath . '/destination';
|
||||
|
||||
$this->driver->createDirectory($sourceDirectory);
|
||||
$this->driver->createDirectory($destinationDirectory);
|
||||
|
||||
$linkName = $destinationDirectory . '/link';
|
||||
|
||||
self::assertTrue($this->driver->isWritable($destinationDirectory));
|
||||
self::assertTrue($this->driver->symlink($sourceDirectory, $linkName));
|
||||
self::assertTrue($this->driver->isExists($linkName));
|
||||
self::assertTrue($this->driver->deleteDirectory($linkName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify file put content without content.
|
||||
*
|
||||
* @return void
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function testFilePutWithoutContents(): void
|
||||
{
|
||||
$path = $this->absolutePath . 'foo/file_three.txt';
|
||||
$this->assertEquals(0, $this->driver->filePutContents($path, ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove generated directories.
|
||||
*
|
||||
* @throws FileSystemException
|
||||
* @return void
|
||||
*/
|
||||
private function removeGeneratedDirectory(): void
|
||||
{
|
||||
if (is_dir($this->generatedPath)) {
|
||||
$this->driver->deleteDirectory($this->generatedPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
<?php
|
||||
/**
|
||||
* Test for \Magento\Framework\Filesystem\File\Read
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Filesystem\File;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class ReadTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Test instance of Read
|
||||
*/
|
||||
public function testInstance()
|
||||
{
|
||||
$file = $this->getFileInstance('popup.csv');
|
||||
$this->assertTrue($file instanceof ReadInterface);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for assertValid method
|
||||
* Expected exception for file that does not exist and file without access
|
||||
*
|
||||
* @dataProvider providerNotValidFiles
|
||||
* @param string $path
|
||||
*/
|
||||
public function testAssertValid($path)
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\FileSystemException::class);
|
||||
|
||||
$this->getFileInstance($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testAssertValid
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerNotValidFiles()
|
||||
{
|
||||
return [['invalid.csv']]; //File does not exist
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for read method
|
||||
*
|
||||
* @dataProvider providerRead
|
||||
* @param string $path
|
||||
* @param int $length
|
||||
* @param string $expectedResult
|
||||
*/
|
||||
public function testRead($path, $length, $expectedResult)
|
||||
{
|
||||
$file = $this->getFileInstance($path);
|
||||
$result = $file->read($length);
|
||||
$this->assertEquals($result, $expectedResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testRead
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerRead()
|
||||
{
|
||||
return [['popup.csv', 10, 'var myData'], ['popup.csv', 15, 'var myData = 5;']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test readAll
|
||||
*
|
||||
* @dataProvider readAllProvider
|
||||
* @param string $path
|
||||
* @param string $content
|
||||
*/
|
||||
public function testReadAll($path, $content)
|
||||
{
|
||||
$file = $this->getFileInstance($path);
|
||||
$this->assertEquals($content, $file->readAll($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testReadFile
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function readAllProvider()
|
||||
{
|
||||
return [
|
||||
['popup.csv', 'var myData = 5;'],
|
||||
['data.csv', '"field1", "field2"' . "\n" . '"field3", "field4"' . "\n"]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test readLine
|
||||
*
|
||||
* @dataProvider readLineProvider
|
||||
* @param string $path
|
||||
* @param array $lines
|
||||
* @param int $length
|
||||
*/
|
||||
public function testReadLine($path, $lines, $length)
|
||||
{
|
||||
$file = $this->getFileInstance($path);
|
||||
foreach ($lines as $line) {
|
||||
$this->assertEquals($line, $file->readLine($length, "\n"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testReadLine
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function readLineProvider()
|
||||
{
|
||||
return [
|
||||
['popup.csv', ['var myData = 5;'], 999],
|
||||
['data.csv', ['"field1", "field2"', '"field3", "field4"'], 999],
|
||||
['popup.csv', ['var'], 3],
|
||||
['data.csv', ['"f', 'ie', 'ld', '1"'], 2]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for stat method
|
||||
*
|
||||
* @dataProvider statProvider
|
||||
* @param string $path
|
||||
*/
|
||||
public function testStat($path)
|
||||
{
|
||||
$file = $this->getFileInstance($path);
|
||||
$expectedInfo = [
|
||||
'dev',
|
||||
'ino',
|
||||
'mode',
|
||||
'nlink',
|
||||
'uid',
|
||||
'gid',
|
||||
'rdev',
|
||||
'size',
|
||||
'atime',
|
||||
'mtime',
|
||||
'ctime',
|
||||
'blksize',
|
||||
'blocks',
|
||||
];
|
||||
$result = $file->stat();
|
||||
foreach ($expectedInfo as $key) {
|
||||
$this->assertArrayHasKey($key, $result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testStat
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function statProvider()
|
||||
{
|
||||
return [['popup.csv'], ['foo/file_three.txt']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for readCsv method
|
||||
*
|
||||
* @dataProvider providerCsv
|
||||
* @param string $path
|
||||
* @param int $length
|
||||
* @param string $delimiter
|
||||
* @param string $enclosure
|
||||
* @param string $escape
|
||||
* @param array $expectedRow1
|
||||
* @param array $expectedRow2
|
||||
*/
|
||||
public function testReadCsv($path, $length, $delimiter, $enclosure, $escape, $expectedRow1, $expectedRow2)
|
||||
{
|
||||
$file = $this->getFileInstance($path);
|
||||
$actualRow1 = $file->readCsv($length, $delimiter, $enclosure, $escape);
|
||||
$actualRow2 = $file->readCsv($length, $delimiter, $enclosure, $escape);
|
||||
$this->assertEquals($expectedRow1, $actualRow1);
|
||||
$this->assertEquals($expectedRow2, $actualRow2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testReadCsv
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerCsv()
|
||||
{
|
||||
return [['data.csv', 0, ',', '"', '\\', ['field1', 'field2'], ['field3', 'field4']]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tell method
|
||||
*
|
||||
* @dataProvider providerPosition
|
||||
* @param string $path
|
||||
* @param int $position
|
||||
*/
|
||||
public function testTell($path, $position)
|
||||
{
|
||||
$file = $this->getFileInstance($path);
|
||||
$file->read($position);
|
||||
$this->assertEquals($position, $file->tell());
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testTell
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerPosition()
|
||||
{
|
||||
return [['popup.csv', 5], ['popup.csv', 10]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for seek method
|
||||
*
|
||||
* @dataProvider providerSeek
|
||||
* @param string $path
|
||||
* @param int $position
|
||||
* @param int $whence
|
||||
* @param int $tell
|
||||
*/
|
||||
public function testSeek($path, $position, $whence, $tell)
|
||||
{
|
||||
$file = $this->getFileInstance($path);
|
||||
$file->seek($position, $whence);
|
||||
$this->assertEquals($tell, $file->tell());
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testSeek
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerSeek()
|
||||
{
|
||||
return [
|
||||
['popup.csv', 5, SEEK_SET, 5],
|
||||
['popup.csv', 10, SEEK_CUR, 10],
|
||||
['popup.csv', -10, SEEK_END, 5]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for eof method
|
||||
*
|
||||
* @dataProvider providerEof
|
||||
* @param string $path
|
||||
* @param int $position
|
||||
*/
|
||||
public function testEofFalse($path, $position)
|
||||
{
|
||||
$file = $this->getFileInstance($path);
|
||||
$file->seek($position);
|
||||
$this->assertFalse($file->eof());
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testEofTrue
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerEof()
|
||||
{
|
||||
return [['popup.csv', 5, false], ['popup.csv', 10, false]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for eof method
|
||||
*/
|
||||
public function testEofTrue()
|
||||
{
|
||||
$file = $this->getFileInstance('popup.csv');
|
||||
$file->seek(0, SEEK_END);
|
||||
$file->read(1);
|
||||
$this->assertTrue($file->eof());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for close method
|
||||
*/
|
||||
public function testClose()
|
||||
{
|
||||
$file = $this->getFileInstance('popup.csv');
|
||||
$this->assertTrue($file->close());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get readable file instance
|
||||
* Get full path for files located in _files directory
|
||||
*
|
||||
* @param $path
|
||||
* @return Read
|
||||
*/
|
||||
private function getFileInstance($path)
|
||||
{
|
||||
$fullPath = __DIR__ . '/../_files/' . $path;
|
||||
return Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Filesystem\File\Read::class,
|
||||
['path' => $fullPath, 'driver' => new \Magento\Framework\Filesystem\Driver\File()]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
<?php
|
||||
/**
|
||||
* Test for \Magento\Framework\Filesystem\File\Write
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Filesystem\File;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class WriteTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Current file path
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $currentFilePath;
|
||||
|
||||
/**
|
||||
* Test instance of Write.
|
||||
*/
|
||||
public function testInstance()
|
||||
{
|
||||
$file = $this->getFileInstance('popup.csv', 'r');
|
||||
$this->assertTrue($file instanceof ReadInterface);
|
||||
$this->assertTrue($file instanceof WriteInterface);
|
||||
$file->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test exceptions on attempt to open existing file with x mode
|
||||
*
|
||||
* @dataProvider fileExistProvider
|
||||
* @param $path
|
||||
* @param $mode
|
||||
*/
|
||||
public function testFileExistException($path, $mode)
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\FileSystemException::class);
|
||||
|
||||
$this->getFileInstance($path, $mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for modeProvider
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fileExistProvider()
|
||||
{
|
||||
return [['popup.csv', 'x'], ['popup.csv', 'x+']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for write method
|
||||
*
|
||||
* @dataProvider writeProvider
|
||||
* @param string $path
|
||||
* @param string $mode
|
||||
* @param string $write
|
||||
* @param string $expectedResult
|
||||
*/
|
||||
public function testWriteOnly($path, $mode, $write, $expectedResult)
|
||||
{
|
||||
$file = $this->getFileInstance($path, $mode);
|
||||
$result = $file->write($write);
|
||||
$file->close();
|
||||
$this->removeCurrentFile();
|
||||
$this->assertEquals($expectedResult, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for modeProvider
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function writeProvider()
|
||||
{
|
||||
return [
|
||||
['new1.csv', 'w', 'write check', 11],
|
||||
['new3.csv', 'a', 'write check', 11],
|
||||
['new5.csv', 'x', 'write check', 11],
|
||||
['new7.csv', 'c', 'write check', 11],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for write method
|
||||
*
|
||||
* @dataProvider writeAndReadProvider
|
||||
* @param string $path
|
||||
* @param string $mode
|
||||
* @param string $write
|
||||
* @param string $expectedResult
|
||||
*/
|
||||
public function testWriteAndRead($path, $mode, $write, $expectedResult)
|
||||
{
|
||||
$file = $this->getFileInstance($path, $mode);
|
||||
$result = $file->write($write);
|
||||
$file->seek(0);
|
||||
$read = $file->read($result);
|
||||
$file->close();
|
||||
$this->removeCurrentFile();
|
||||
$this->assertEquals($expectedResult, $result);
|
||||
$this->assertEquals($write, $read);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for modeProvider
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function writeAndReadProvider()
|
||||
{
|
||||
return [
|
||||
['new2.csv', 'w+', 'write check', 11],
|
||||
['new4.csv', 'a+', 'write check', 11],
|
||||
['new6.csv', 'x+', 'write check', 11],
|
||||
['new8.csv', 'c+', 'write check', 11],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes one CSV row to the file.
|
||||
*
|
||||
* @dataProvider csvDataProvider
|
||||
* @param array $expectedData
|
||||
* @param string $path
|
||||
* @param array $data
|
||||
* @param string $delimiter
|
||||
* @param string $enclosure
|
||||
*/
|
||||
public function testWriteCsv($expectedData, $path, array $data, $delimiter = ',', $enclosure = '"')
|
||||
{
|
||||
$file = $this->getFileInstance($path, 'w+');
|
||||
$result = $file->writeCsv($data, $delimiter, $enclosure);
|
||||
$file->seek(0);
|
||||
$read = $file->readCsv($result, $delimiter, $enclosure);
|
||||
$file->close();
|
||||
$this->removeCurrentFile();
|
||||
$this->assertEquals($expectedData, $read);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testWriteCsv
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function csvDataProvider()
|
||||
{
|
||||
return [
|
||||
[['field1', 'field2'], 'newcsv1.csv', ['field1', 'field2'], ',', '"'],
|
||||
[['field1', 'field2'], 'newcsv1.csv', ['field1', 'field2'], '%', '@'],
|
||||
[[' =field1', 'field2'], 'newcsv1.csv', ['=field1', 'field2'], '%', '@'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for lock and unlock functions
|
||||
*/
|
||||
public function testLockUnlock()
|
||||
{
|
||||
$file = $this->getFileInstance('locked.csv', 'w+');
|
||||
$this->assertTrue($file->lock());
|
||||
$this->assertTrue($file->unlock());
|
||||
$file->close();
|
||||
$this->removeCurrentFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for flush method
|
||||
*/
|
||||
public function testFlush()
|
||||
{
|
||||
$file = $this->getFileInstance('locked.csv', 'w+');
|
||||
$this->assertTrue($file->flush());
|
||||
$file->close();
|
||||
$this->removeCurrentFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove current file
|
||||
*/
|
||||
private function removeCurrentFile()
|
||||
{
|
||||
unlink($this->currentFilePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get readable file instance
|
||||
* Get full path for files located in _files directory
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $mode
|
||||
* @return Write
|
||||
*/
|
||||
private function getFileInstance($path, $mode)
|
||||
{
|
||||
$this->currentFilePath = __DIR__ . '/../_files/' . $path;
|
||||
return Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Filesystem\File\Write::class,
|
||||
[
|
||||
'path' => $this->currentFilePath,
|
||||
'driver' => new \Magento\Framework\Filesystem\Driver\File(),
|
||||
'mode' => $mode,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* Integration test for \Magento\Framework\Filesystem\FileResolver
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Filesystem;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class FileResolverTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Path to add to include path
|
||||
*/
|
||||
const FIRST_PATH = '/path/to/code/1/';
|
||||
|
||||
/**
|
||||
* Path to add to include path
|
||||
*/
|
||||
const SECOND_PATH = '/path/to/code/2/';
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Filesystem\FileResolver
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* @var string original include-path variable
|
||||
*/
|
||||
protected $originalPath;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = Bootstrap::getObjectManager()->create(\Magento\Framework\Filesystem\FileResolver::class);
|
||||
$this->originalPath = get_include_path();
|
||||
set_include_path('/pre/existing/paths/');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
set_include_path($this->originalPath);
|
||||
}
|
||||
|
||||
public function testAddIncludePathPrepend()
|
||||
{
|
||||
$this->model->addIncludePath(self::FIRST_PATH);
|
||||
$this->model->addIncludePath(self::SECOND_PATH);
|
||||
|
||||
$postIncludePath = get_include_path();
|
||||
$this->assertStringStartsWith(
|
||||
self::SECOND_PATH,
|
||||
$postIncludePath
|
||||
);
|
||||
}
|
||||
|
||||
public function testAddIncludePathAppend()
|
||||
{
|
||||
$this->model->addIncludePath(self::FIRST_PATH, false);
|
||||
$this->model->addIncludePath(self::SECOND_PATH, false);
|
||||
|
||||
$postIncludePath = get_include_path();
|
||||
$this->assertStringEndsWith(
|
||||
self::SECOND_PATH,
|
||||
$postIncludePath
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetFile()
|
||||
{
|
||||
$includePath = realpath(__DIR__ . '/_files/');
|
||||
$className = '\ClassToFind';
|
||||
|
||||
$this->model->addIncludePath($includePath);
|
||||
$this->assertFileExists($this->model->getFile($className));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* Test for \Magento\Framework\Filesystem
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Framework\Filesystem;
|
||||
|
||||
use Magento\Framework\App\Filesystem\DirectoryList as AppDirectoryList;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Class FilesystemTest
|
||||
* Test for Magento\Framework\Filesystem class
|
||||
*
|
||||
*/
|
||||
class FilesystemTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var Filesystem
|
||||
*/
|
||||
protected $filesystem;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->filesystem = Bootstrap::getObjectManager()->create(\Magento\Framework\Filesystem::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getDirectoryRead method return valid instance
|
||||
*/
|
||||
public function testGetDirectoryReadInstance()
|
||||
{
|
||||
$dir = $this->filesystem->getDirectoryRead(AppDirectoryList::VAR_DIR);
|
||||
$this->assertInstanceOf(\Magento\Framework\Filesystem\Directory\Read::class, $dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getDirectoryWrite method return valid instance
|
||||
*/
|
||||
public function testGetDirectoryWriteInstance()
|
||||
{
|
||||
$dir = $this->filesystem->getDirectoryWrite(AppDirectoryList::VAR_DIR);
|
||||
$this->assertInstanceOf(\Magento\Framework\Filesystem\Directory\Write::class, $dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getUri returns right uri
|
||||
*/
|
||||
public function testGetUri()
|
||||
{
|
||||
$this->assertStringContainsString(
|
||||
'media',
|
||||
$this->filesystem->getDirectoryRead(AppDirectoryList::MEDIA)->getAbsolutePath()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Test for \Magento\Framework\Filesystem\Io\File
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Framework\Filesystem\Io;
|
||||
|
||||
use Magento\Framework\Exception\FileSystemException;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Verify filesystem client
|
||||
*/
|
||||
class FileTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var File
|
||||
*/
|
||||
private $io;
|
||||
|
||||
/**
|
||||
* @var String
|
||||
*/
|
||||
private $absolutePath;
|
||||
|
||||
/**
|
||||
* @var String
|
||||
*/
|
||||
private $generatedPath;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->io = new File();
|
||||
$this->absolutePath = Bootstrap::getInstance()->getAppTempDir();
|
||||
$this->generatedPath = $this->getTestPath('/rollback_test_');
|
||||
$this->io->mkdir($this->generatedPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->removeGeneratedDirectory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify file put without content.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWrite(): void
|
||||
{
|
||||
$path = $this->generatedPath . '/file_three.txt';
|
||||
$this->assertEquals(0, $this->io->write($path, '', 0444));
|
||||
$this->assertFalse(is_writable($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns relative path for the test.
|
||||
*
|
||||
* @param $relativePath
|
||||
* @return string
|
||||
*/
|
||||
protected function getTestPath($relativePath): string
|
||||
{
|
||||
return $this->absolutePath . $relativePath . time();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove generated directories.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function removeGeneratedDirectory(): void
|
||||
{
|
||||
if (is_dir($this->generatedPath)) {
|
||||
$this->io->rmdir($this->generatedPath, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
* Exists for testing FileResolver.
|
||||
*
|
||||
* \Magento\Framework\Filesystem\FileResolverTest
|
||||
*/
|
||||
@@ -0,0 +1,2 @@
|
||||
"field1", "field2"
|
||||
"field3", "field4"
|
||||
|
@@ -0,0 +1 @@
|
||||
var myData = 5;
|
||||
|
Reference in New Issue
Block a user