Skip to content

Commit 5b86795

Browse files
committed
FileMock: fread/fwrite in writeonly/readonly modes return false and trigger notices in PHP 7.4
1 parent c853ebe commit 5b86795

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/Framework/FileMock.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ public function stream_open(string $path, string $mode): bool
9393
}
9494

9595

96-
public function stream_read(int $length): string
96+
public function stream_read(int $length)
9797
{
9898
if (!$this->isReadable) {
99-
return '';
99+
return false;
100100
}
101101

102102
$result = substr($this->content, $this->readingPos, $length);
@@ -106,10 +106,10 @@ public function stream_read(int $length): string
106106
}
107107

108108

109-
public function stream_write(string $data): int
109+
public function stream_write(string $data)
110110
{
111111
if (!$this->isWritable) {
112-
return 0;
112+
return false;
113113
}
114114

115115
$length = strlen($data);

tests/Framework/FileMock.phpt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ test(function () {
101101
$cases = [
102102
'r' => ['ABC', 'ABC'],
103103
'r+' => ['ABC', 'ABC'],
104-
'w' => ['', ''],
104+
'w' => ['', PHP_VERSION_ID < 70400 ? '' : false],
105105
'w+' => ['', ''],
106-
'a' => ['ABC', ''],
106+
'a' => ['ABC', PHP_VERSION_ID < 70400 ? '' : false],
107107
'a+' => ['ABC', 'ABC'],
108-
'x' => ['', ''],
108+
'x' => ['', PHP_VERSION_ID < 70400 ? '' : false],
109109
'x+' => ['', ''],
110-
'c' => ['ABC', ''],
110+
'c' => ['ABC', PHP_VERSION_ID < 70400 ? '' : false],
111111
'c+' => ['ABC', 'ABC'],
112112
];
113113

@@ -129,13 +129,13 @@ test(function () {
129129
$cases = [
130130
'r' => ['ABC', 'ABC'],
131131
'r+' => ['_BC', '_BC'],
132-
'w' => ['_', ''],
132+
'w' => ['_', PHP_VERSION_ID < 70400 ? '' : false],
133133
'w+' => ['_', '_'],
134-
'a' => ['ABC_', ''],
134+
'a' => ['ABC_', PHP_VERSION_ID < 70400 ? '' : false],
135135
'a+' => ['ABC_', 'ABC_'],
136-
'x' => ['_', ''],
136+
'x' => ['_', PHP_VERSION_ID < 70400 ? '' : false],
137137
'x+' => ['_', '_'],
138-
'c' => ['_BC', ''],
138+
'c' => ['_BC', PHP_VERSION_ID < 70400 ? '' : false],
139139
'c+' => ['_BC', '_BC'],
140140
];
141141

@@ -237,20 +237,20 @@ test(function () {
237237
Assert::same(ftell($handleReal), ftell($handleMock));
238238
Assert::same(file_get_contents($pathReal), file_get_contents($pathMock));
239239

240-
Assert::same(fwrite($handleReal, 'World'), fwrite($handleMock, 'World'));
240+
Assert::same(@fwrite($handleReal, 'World'), fwrite($handleMock, 'World')); // @ - triggers E_NOTICE since PHP 7.4
241241
Assert::same(ftell($handleReal), ftell($handleMock));
242242
Assert::same(file_get_contents($pathReal), file_get_contents($pathMock));
243243

244244
Assert::same(ftruncate($handleReal, 2), ftruncate($handleMock, 2));
245245
Assert::same(ftell($handleReal), ftell($handleMock));
246246
Assert::same(file_get_contents($pathReal), file_get_contents($pathMock));
247247

248-
Assert::same(fwrite($handleReal, 'World'), fwrite($handleMock, 'World'));
248+
Assert::same(@fwrite($handleReal, 'World'), fwrite($handleMock, 'World')); // @ - triggers E_NOTICE since PHP 7.4
249249
Assert::same(ftell($handleReal), ftell($handleMock));
250250
Assert::same(file_get_contents($pathReal), file_get_contents($pathMock));
251251

252252
Assert::same(fseek($handleReal, 2), fseek($handleMock, 2));
253-
Assert::same(fread($handleReal, 7), fread($handleMock, 7));
253+
Assert::same(@fread($handleReal, 7), fread($handleMock, 7)); // @ - triggers E_NOTICE since PHP 7.4
254254
Assert::same(fclose($handleReal), fclose($handleMock));
255255
}
256256

0 commit comments

Comments
 (0)