Skip to content

Commit

Permalink
Cover change tests, fix static
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Echo committed Feb 13, 2020
1 parent 781e5f4 commit 66d2cbc
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
* 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;

class FileTest extends \PHPUnit\Framework\TestCase
/**
* Verify File class
*/
class FileTest extends TestCase
{
/**
* @var File
Expand Down Expand Up @@ -50,16 +56,20 @@ public function setUp()

/**
* @inheritdoc
*
* @return void
*/
protected function tearDown()
protected function tearDown(): void
{
$this->removeGeneratedDirectory();
}

/**
* Tests directory recursive read.
*
* @return void
*/
public function testReadDirectoryRecursively()
public function testReadDirectoryRecursively(): void
{
$paths = [
'foo/bar',
Expand All @@ -77,9 +87,10 @@ public function testReadDirectoryRecursively()
/**
* Tests directory reading exception.
*
* @return void
* @expectedException \Magento\Framework\Exception\FileSystemException
*/
public function testReadDirectoryRecursivelyFailure()
public function testReadDirectoryRecursivelyFailure(): void
{
$this->driver->readDirectoryRecursively($this->getTestPath('not-existing-directory'));
}
Expand All @@ -88,8 +99,9 @@ public function testReadDirectoryRecursivelyFailure()
* Tests of directory creating.
*
* @throws FileSystemException
* @return void
*/
public function testCreateDirectory()
public function testCreateDirectory(): void
{
$generatedPath = $this->getTestPath('generated/roo/bar/baz/foo');
$generatedPathBase = $this->getTestPath('generated');
Expand Down Expand Up @@ -123,6 +135,18 @@ public function testSymlinks(): void
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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
*/
public function setUp()
{
$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->assertEquals(false, 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);
}
}
}
8 changes: 5 additions & 3 deletions lib/internal/Magento/Framework/Filesystem/Driver/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
use Magento\Framework\Filesystem\Glob;

/**
* Class File
* Driver file class
*
* @package Magento\Framework\Filesystem\Driver
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class File implements DriverInterface
Expand Down Expand Up @@ -593,12 +592,15 @@ public function fileOpen($path, $mode)
*/
public function fileReadLine($resource, $length, $ending = null)
{
// phpcs:disable
$result = @stream_get_line($resource, $length, $ending);
// phpcs:enable
if (false === $result) {
throw new FileSystemException(
new \Magento\Framework\Phrase('File cannot be read %1', [$this->getWarningMessage()])
);
}

return $result;
}

Expand Down Expand Up @@ -976,7 +978,7 @@ public function getRealPathSafety($path)

//Removing redundant directory separators.
$path = preg_replace(
'/\\' .DIRECTORY_SEPARATOR .'\\' .DIRECTORY_SEPARATOR .'+/',
'/\\' . DIRECTORY_SEPARATOR . '\\' . DIRECTORY_SEPARATOR . '+/',
DIRECTORY_SEPARATOR,
$path
);
Expand Down
11 changes: 8 additions & 3 deletions lib/internal/Magento/Framework/Filesystem/Driver/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Magento\Framework\Exception\FileSystemException;

/**
* Class Http
* Http driver file class.
*/
class Http extends File
{
Expand Down Expand Up @@ -196,8 +196,13 @@ public function fileOpen($path, $mode)
*/
public function fileReadLine($resource, $length, $ending = null)
{
$result = @stream_get_line($resource, $length, $ending);

try {
$result = @stream_get_line($resource, $length, $ending);
} catch (\Exception $e) {
throw new FileSystemException(
new \Magento\Framework\Phrase('Stream get line failed %1', [$e->getMessage()])
);
}
return $result;
}

Expand Down
31 changes: 23 additions & 8 deletions lib/internal/Magento/Framework/Filesystem/Io/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Filesystem\Io;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;

/**
* Filesystem client
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class File extends AbstractIo
Expand Down Expand Up @@ -186,7 +191,7 @@ public function streamWriteCsv(array $row, $delimiter = ',', $enclosure = '"')
* Security enhancement for CSV data processing by Excel-like applications.
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=1054702
*
* @var $value string|\Magento\Framework\Phrase
* @var $value string|Phrase
*/
foreach ($row as $key => $value) {
if (!is_string($value)) {
Expand All @@ -201,6 +206,7 @@ public function streamWriteCsv(array $row, $delimiter = ',', $enclosure = '"')

/**
* Close an open file pointer
*
* Set chmod on a file
*
* @return bool
Expand Down Expand Up @@ -328,6 +334,7 @@ public function rmdir($dir, $recursive = false)

/**
* Delete a directory recursively
*
* @param string $dir
* @param bool $recursive
* @return bool
Expand Down Expand Up @@ -405,8 +412,8 @@ public function pwd()
*
* @param string $dir
* @return true
* @throws \Exception
* @SuppressWarnings(PHPMD.ShortMethodName)
* @throws LocalizedException
*/
public function cd($dir)
{
Expand All @@ -415,7 +422,9 @@ public function cd($dir)
$this->_cwd = realpath($dir);
return true;
} else {
throw new \Exception('Unable to list current working directory.');
throw new LocalizedException(
new Phrase('Unable to list current working directory.')
);
}
}

Expand Down Expand Up @@ -553,7 +562,7 @@ public function createDestinationDir($path)
* @param string $folder
* @param int $mode
* @return true
* @throws \Exception
* @throws LocalizedException
*/
public function checkAndCreateFolder($folder, $mode = 0777)
{
Expand All @@ -564,7 +573,9 @@ public function checkAndCreateFolder($folder, $mode = 0777)
$this->checkAndCreateFolder(dirname($folder), $mode);
}
if (!is_dir($folder) && !$this->mkdir($folder, $mode)) {
throw new \Exception("Unable to create directory '{$folder}'. Access forbidden.");
throw new LocalizedException(
new Phrase("Unable to create directory '{$folder}'. Access forbidden.")
);
}
return true;
}
Expand Down Expand Up @@ -672,7 +683,7 @@ public static function chmodRecursive($dir, $mode)
*
* @param string|null $grep
* @return array
* @throws \Exception
* @throws LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ShortMethodName)
*/
Expand All @@ -685,7 +696,9 @@ public function ls($grep = null)
} elseif (is_dir($this->_iwd)) {
$dir = $this->_iwd;
} else {
throw new \Exception('Unable to list current working directory.');
throw new LocalizedException(
new Phrase('Unable to list current working directory.')
);
}

$list = [];
Expand Down Expand Up @@ -742,7 +755,9 @@ public function ls($grep = null)
}
closedir($dirHandler);
} else {
throw new \Exception('Unable to list current working directory. Access forbidden.');
throw new LocalizedException(
new Phrase('Unable to list current working directory. Access forbidden.')
);
}

return $list;
Expand Down
Loading

0 comments on commit 66d2cbc

Please sign in to comment.