Skip to content

Commit

Permalink
ENGCOM-8127: \Magento\Framework\Filesystem\Io\File::read() should be …
Browse files Browse the repository at this point in the history
…able to accept resource in second argument #27869
  • Loading branch information
sidolov committed Sep 24, 2020
2 parents 6250fd7 + 29ea25c commit b2712d9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/internal/Magento/Framework/Filesystem/Io/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,16 @@ public function cd($dir)
*/
public function read($filename, $dest = null)
{
$result = false;

$this->_cwd();
if ($dest !== null) {
$result = @copy($filename, $dest);
} else {
if ($dest === null) {
$result = @file_get_contents($filename);
} elseif (is_resource($dest)) {
$result = @file_get_contents($filename);
fwrite($dest, $result);
} elseif (is_string($dest)) {
$result = @copy($filename, $dest);
}
$this->_iwd();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Filesystem\Test\Unit\Io;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\Io\File;
use PHPUnit\Framework\TestCase;

class FileTest extends TestCase
{
private function getTmpDir()
{
$tmpDir = '/tmp/magento-' . \microtime(true);
if (!\file_exists($tmpDir)) {
\mkdir($tmpDir, 0777, true);
}
return $tmpDir;
}

/**
* To cover the issue on GitHub: #27866
* @throws LocalizedException
*/
public function testReadShouldCopyTheSourceFileToTheGivenFileResource()
{
$content = \random_int(0, 1000);
$sourceFileName = "source-file.txt";
$tmpDir = $this->getTmpDir();
\file_put_contents("{$tmpDir}/{$sourceFileName}", $content);

$file = new File();
$targetFileName = "target-file.txt";
$targetFileHandle = \fopen("{$tmpDir}/{$targetFileName}", 'w');
$file->cd($tmpDir);
$file->read($sourceFileName, $targetFileHandle);

$targetContent = file_get_contents("{$tmpDir}/{$targetFileName}");
$this->assertEquals($content, $targetContent);
}
}

0 comments on commit b2712d9

Please sign in to comment.