Skip to content

Commit 3e9f4f8

Browse files
authored
Merge pull request #48 from derrabus/bugfix/php-8
Fix tests on PHP 8
2 parents ea1e00e + 151d3e0 commit 3e9f4f8

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/CsvReader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Keboola\Csv;
66

77
use Iterator;
8+
use ReturnTypeWillChange;
89

910
class CsvReader extends AbstractCsvFile implements Iterator
1011
{
@@ -169,6 +170,7 @@ public function getLineBreak()
169170
/**
170171
* @inheritdoc
171172
*/
173+
#[ReturnTypeWillChange]
172174
public function rewind()
173175
{
174176
rewind($this->getFilePointer());
@@ -217,6 +219,7 @@ public function getLineBreakAsText()
217219
/**
218220
* @inheritdoc
219221
*/
222+
#[ReturnTypeWillChange]
220223
public function current()
221224
{
222225
return $this->currentRow;
@@ -225,6 +228,7 @@ public function current()
225228
/**
226229
* @inheritdoc
227230
*/
231+
#[ReturnTypeWillChange]
228232
public function next()
229233
{
230234
$this->currentRow = $this->readLine();
@@ -234,6 +238,7 @@ public function next()
234238
/**
235239
* @inheritdoc
236240
*/
241+
#[ReturnTypeWillChange]
237242
public function key()
238243
{
239244
return $this->rowCounter;
@@ -242,6 +247,7 @@ public function key()
242247
/**
243248
* @inheritdoc
244249
*/
250+
#[ReturnTypeWillChange]
245251
public function valid()
246252
{
247253
return $this->currentRow !== false;

src/CsvWriter.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Keboola\Csv;
44

5+
use TypeError;
6+
use ValueError;
7+
58
class CsvWriter extends AbstractCsvFile
69
{
710
/**
@@ -63,7 +66,16 @@ private function validateLineBreak($lineBreak)
6366
*/
6467
protected function openCsvFile($fileName)
6568
{
66-
$this->filePointer = @fopen($fileName, 'w');
69+
try {
70+
$this->filePointer = @fopen($fileName, 'w');
71+
} catch (ValueError $e) {
72+
throw new Exception(
73+
"Cannot open file {$fileName} " . $e->getMessage(),
74+
Exception::FILE_NOT_EXISTS,
75+
$e
76+
);
77+
}
78+
6779
if (!$this->filePointer) {
6880
throw new Exception(
6981
"Cannot open file {$fileName} " . error_get_last()['message'],
@@ -79,7 +91,18 @@ protected function openCsvFile($fileName)
7991
public function writeRow(array $row)
8092
{
8193
$str = $this->rowToStr($row);
82-
$ret = @fwrite($this->getFilePointer(), $str);
94+
try {
95+
$ret = @fwrite($this->getFilePointer(), $str);
96+
} catch (TypeError $e) {
97+
throw new Exception(
98+
'Cannot write to CSV file ' . $this->fileName .
99+
'Error: ' . $e->getMessage() .
100+
' Return: false' .
101+
' To write: ' . strlen($str) . ' Written: 0',
102+
Exception::WRITE_ERROR,
103+
$e
104+
);
105+
}
83106

84107
/* According to http://php.net/fwrite the fwrite() function
85108
should return false on error. However not writing the full

tests/CsvWriteTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,16 @@ public function testInvalidFileName($filename, $message)
150150

151151
public function invalidFileNameProvider()
152152
{
153+
if (PHP_VERSION_ID < 80000) {
154+
return [
155+
['', 'Filename cannot be empty'],
156+
["\0", 'fopen() expects parameter 1 to be a valid path, string given'],
157+
];
158+
}
159+
153160
return [
154-
['', 'Filename cannot be empty'],
155-
["\0", 'fopen() expects parameter 1 to be a valid path, string given'],
161+
['', 'Path cannot be empty'],
162+
["\0", 'Argument #1 ($filename) must not contain any null bytes'],
156163
];
157164
}
158165

@@ -212,6 +219,10 @@ public function testInvalidPointer()
212219
'Cannot write to CSV file Error: fwrite(): ' .
213220
'write of 14 bytes failed with errno=9 Bad file descriptor Return: false To write: 14 Written: 0'
214221
),
222+
new StringContains(
223+
'Cannot write to CSV file Error: fwrite(): ' .
224+
'Write of 14 bytes failed with errno=9 Bad file descriptor Return: false To write: 14 Written: 0'
225+
)
215226
]);
216227
self::assertThat($e->getMessage(), $or);
217228
}

0 commit comments

Comments
 (0)