Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

\Magento\Framework\Filesystem\Io\File::read() should be able to accept resource in second argument #27869

Merged
merged 9 commits into from
Sep 25, 2020
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);
}
}