Skip to content

Commit 66d2cbc

Browse files
committed
Cover change tests, fix static
1 parent 781e5f4 commit 66d2cbc

File tree

6 files changed

+233
-31
lines changed

6 files changed

+233
-31
lines changed

dev/tests/integration/testsuite/Magento/Framework/Filesystem/Driver/FileTest.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
declare(strict_types=1);
9+
810
namespace Magento\Framework\Filesystem\Driver;
911

1012
use Magento\Framework\Exception\FileSystemException;
13+
use PHPUnit\Framework\TestCase;
1114

12-
class FileTest extends \PHPUnit\Framework\TestCase
15+
/**
16+
* Verify File class
17+
*/
18+
class FileTest extends TestCase
1319
{
1420
/**
1521
* @var File
@@ -50,16 +56,20 @@ public function setUp()
5056

5157
/**
5258
* @inheritdoc
59+
*
60+
* @return void
5361
*/
54-
protected function tearDown()
62+
protected function tearDown(): void
5563
{
5664
$this->removeGeneratedDirectory();
5765
}
5866

5967
/**
6068
* Tests directory recursive read.
69+
*
70+
* @return void
6171
*/
62-
public function testReadDirectoryRecursively()
72+
public function testReadDirectoryRecursively(): void
6373
{
6474
$paths = [
6575
'foo/bar',
@@ -77,9 +87,10 @@ public function testReadDirectoryRecursively()
7787
/**
7888
* Tests directory reading exception.
7989
*
90+
* @return void
8091
* @expectedException \Magento\Framework\Exception\FileSystemException
8192
*/
82-
public function testReadDirectoryRecursivelyFailure()
93+
public function testReadDirectoryRecursivelyFailure(): void
8394
{
8495
$this->driver->readDirectoryRecursively($this->getTestPath('not-existing-directory'));
8596
}
@@ -88,8 +99,9 @@ public function testReadDirectoryRecursivelyFailure()
8899
* Tests of directory creating.
89100
*
90101
* @throws FileSystemException
102+
* @return void
91103
*/
92-
public function testCreateDirectory()
104+
public function testCreateDirectory(): void
93105
{
94106
$generatedPath = $this->getTestPath('generated/roo/bar/baz/foo');
95107
$generatedPathBase = $this->getTestPath('generated');
@@ -123,6 +135,18 @@ public function testSymlinks(): void
123135
self::assertTrue($this->driver->deleteDirectory($linkName));
124136
}
125137

138+
/**
139+
* Verify file put content without content.
140+
*
141+
* @return void
142+
* @throws FileSystemException
143+
*/
144+
public function testFilePutWithoutContents(): void
145+
{
146+
$path = $this->absolutePath . 'foo/file_three.txt';
147+
$this->assertEquals(0, $this->driver->filePutContents($path, ''));
148+
}
149+
126150
/**
127151
* Remove generated directories.
128152
*
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Test for \Magento\Framework\Filesystem\Io\File
4+
*
5+
* Copyright © Magento, Inc. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
declare(strict_types=1);
9+
10+
namespace Magento\Framework\Filesystem\Io;
11+
12+
use Magento\Framework\Exception\FileSystemException;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Verify filesystem client
18+
*/
19+
class FileTest extends TestCase
20+
{
21+
22+
/**
23+
* @var File
24+
*/
25+
private $io;
26+
27+
/**
28+
* @var String
29+
*/
30+
private $absolutePath;
31+
32+
/**
33+
* @var String
34+
*/
35+
private $generatedPath;
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function setUp()
41+
{
42+
$this->io = new File();
43+
$this->absolutePath = Bootstrap::getInstance()->getAppTempDir();
44+
$this->generatedPath = $this->getTestPath('/rollback_test_');
45+
$this->io->mkdir($this->generatedPath);
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*
51+
* @return void
52+
*/
53+
protected function tearDown(): void
54+
{
55+
$this->removeGeneratedDirectory();
56+
}
57+
58+
/**
59+
* Verify file put without content.
60+
*
61+
* @return void
62+
*/
63+
public function testWrite(): void
64+
{
65+
$path = $this->generatedPath . '/file_three.txt';
66+
$this->assertEquals(0, $this->io->write($path, '', 0444));
67+
$this->assertEquals(false, is_writable($path));
68+
}
69+
70+
/**
71+
* Returns relative path for the test.
72+
*
73+
* @param $relativePath
74+
* @return string
75+
*/
76+
protected function getTestPath($relativePath): string
77+
{
78+
return $this->absolutePath . $relativePath . time();
79+
}
80+
81+
/**
82+
* Remove generated directories.
83+
*
84+
* @return void
85+
*/
86+
private function removeGeneratedDirectory(): void
87+
{
88+
if (is_dir($this->generatedPath)) {
89+
$this->io->rmdir($this->generatedPath, true);
90+
}
91+
}
92+
}

lib/internal/Magento/Framework/Filesystem/Driver/File.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
use Magento\Framework\Filesystem\Glob;
1414

1515
/**
16-
* Class File
16+
* Driver file class
1717
*
18-
* @package Magento\Framework\Filesystem\Driver
1918
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2019
*/
2120
class File implements DriverInterface
@@ -593,12 +592,15 @@ public function fileOpen($path, $mode)
593592
*/
594593
public function fileReadLine($resource, $length, $ending = null)
595594
{
595+
// phpcs:disable
596596
$result = @stream_get_line($resource, $length, $ending);
597+
// phpcs:enable
597598
if (false === $result) {
598599
throw new FileSystemException(
599600
new \Magento\Framework\Phrase('File cannot be read %1', [$this->getWarningMessage()])
600601
);
601602
}
603+
602604
return $result;
603605
}
604606

@@ -976,7 +978,7 @@ public function getRealPathSafety($path)
976978

977979
//Removing redundant directory separators.
978980
$path = preg_replace(
979-
'/\\' .DIRECTORY_SEPARATOR .'\\' .DIRECTORY_SEPARATOR .'+/',
981+
'/\\' . DIRECTORY_SEPARATOR . '\\' . DIRECTORY_SEPARATOR . '+/',
980982
DIRECTORY_SEPARATOR,
981983
$path
982984
);

lib/internal/Magento/Framework/Filesystem/Driver/Http.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\Exception\FileSystemException;
1212

1313
/**
14-
* Class Http
14+
* Http driver file class.
1515
*/
1616
class Http extends File
1717
{
@@ -196,8 +196,13 @@ public function fileOpen($path, $mode)
196196
*/
197197
public function fileReadLine($resource, $length, $ending = null)
198198
{
199-
$result = @stream_get_line($resource, $length, $ending);
200-
199+
try {
200+
$result = @stream_get_line($resource, $length, $ending);
201+
} catch (\Exception $e) {
202+
throw new FileSystemException(
203+
new \Magento\Framework\Phrase('Stream get line failed %1', [$e->getMessage()])
204+
);
205+
}
201206
return $result;
202207
}
203208

lib/internal/Magento/Framework/Filesystem/Io/File.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Framework\Filesystem\Io;
78

9+
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\Phrase;
11+
812
/**
913
* Filesystem client
14+
*
1015
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
1116
*/
1217
class File extends AbstractIo
@@ -186,7 +191,7 @@ public function streamWriteCsv(array $row, $delimiter = ',', $enclosure = '"')
186191
* Security enhancement for CSV data processing by Excel-like applications.
187192
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=1054702
188193
*
189-
* @var $value string|\Magento\Framework\Phrase
194+
* @var $value string|Phrase
190195
*/
191196
foreach ($row as $key => $value) {
192197
if (!is_string($value)) {
@@ -201,6 +206,7 @@ public function streamWriteCsv(array $row, $delimiter = ',', $enclosure = '"')
201206

202207
/**
203208
* Close an open file pointer
209+
*
204210
* Set chmod on a file
205211
*
206212
* @return bool
@@ -328,6 +334,7 @@ public function rmdir($dir, $recursive = false)
328334

329335
/**
330336
* Delete a directory recursively
337+
*
331338
* @param string $dir
332339
* @param bool $recursive
333340
* @return bool
@@ -405,8 +412,8 @@ public function pwd()
405412
*
406413
* @param string $dir
407414
* @return true
408-
* @throws \Exception
409415
* @SuppressWarnings(PHPMD.ShortMethodName)
416+
* @throws LocalizedException
410417
*/
411418
public function cd($dir)
412419
{
@@ -415,7 +422,9 @@ public function cd($dir)
415422
$this->_cwd = realpath($dir);
416423
return true;
417424
} else {
418-
throw new \Exception('Unable to list current working directory.');
425+
throw new LocalizedException(
426+
new Phrase('Unable to list current working directory.')
427+
);
419428
}
420429
}
421430

@@ -553,7 +562,7 @@ public function createDestinationDir($path)
553562
* @param string $folder
554563
* @param int $mode
555564
* @return true
556-
* @throws \Exception
565+
* @throws LocalizedException
557566
*/
558567
public function checkAndCreateFolder($folder, $mode = 0777)
559568
{
@@ -564,7 +573,9 @@ public function checkAndCreateFolder($folder, $mode = 0777)
564573
$this->checkAndCreateFolder(dirname($folder), $mode);
565574
}
566575
if (!is_dir($folder) && !$this->mkdir($folder, $mode)) {
567-
throw new \Exception("Unable to create directory '{$folder}'. Access forbidden.");
576+
throw new LocalizedException(
577+
new Phrase("Unable to create directory '{$folder}'. Access forbidden.")
578+
);
568579
}
569580
return true;
570581
}
@@ -672,7 +683,7 @@ public static function chmodRecursive($dir, $mode)
672683
*
673684
* @param string|null $grep
674685
* @return array
675-
* @throws \Exception
686+
* @throws LocalizedException
676687
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
677688
* @SuppressWarnings(PHPMD.ShortMethodName)
678689
*/
@@ -685,7 +696,9 @@ public function ls($grep = null)
685696
} elseif (is_dir($this->_iwd)) {
686697
$dir = $this->_iwd;
687698
} else {
688-
throw new \Exception('Unable to list current working directory.');
699+
throw new LocalizedException(
700+
new Phrase('Unable to list current working directory.')
701+
);
689702
}
690703

691704
$list = [];
@@ -742,7 +755,9 @@ public function ls($grep = null)
742755
}
743756
closedir($dirHandler);
744757
} else {
745-
throw new \Exception('Unable to list current working directory. Access forbidden.');
758+
throw new LocalizedException(
759+
new Phrase('Unable to list current working directory. Access forbidden.')
760+
);
746761
}
747762

748763
return $list;

0 commit comments

Comments
 (0)