Skip to content

Commit

Permalink
Fix tests on PHP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Nov 8, 2022
1 parent ea1e00e commit 151d3e0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Keboola\Csv;

use Iterator;
use ReturnTypeWillChange;

class CsvReader extends AbstractCsvFile implements Iterator
{
Expand Down Expand Up @@ -169,6 +170,7 @@ public function getLineBreak()
/**
* @inheritdoc
*/
#[ReturnTypeWillChange]
public function rewind()
{
rewind($this->getFilePointer());
Expand Down Expand Up @@ -217,6 +219,7 @@ public function getLineBreakAsText()
/**
* @inheritdoc
*/
#[ReturnTypeWillChange]
public function current()
{
return $this->currentRow;
Expand All @@ -225,6 +228,7 @@ public function current()
/**
* @inheritdoc
*/
#[ReturnTypeWillChange]
public function next()
{
$this->currentRow = $this->readLine();
Expand All @@ -234,6 +238,7 @@ public function next()
/**
* @inheritdoc
*/
#[ReturnTypeWillChange]
public function key()
{
return $this->rowCounter;
Expand All @@ -242,6 +247,7 @@ public function key()
/**
* @inheritdoc
*/
#[ReturnTypeWillChange]
public function valid()
{
return $this->currentRow !== false;
Expand Down
27 changes: 25 additions & 2 deletions src/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Keboola\Csv;

use TypeError;
use ValueError;

class CsvWriter extends AbstractCsvFile
{
/**
Expand Down Expand Up @@ -63,7 +66,16 @@ private function validateLineBreak($lineBreak)
*/
protected function openCsvFile($fileName)
{
$this->filePointer = @fopen($fileName, 'w');
try {
$this->filePointer = @fopen($fileName, 'w');
} catch (ValueError $e) {
throw new Exception(
"Cannot open file {$fileName} " . $e->getMessage(),
Exception::FILE_NOT_EXISTS,
$e
);
}

if (!$this->filePointer) {
throw new Exception(
"Cannot open file {$fileName} " . error_get_last()['message'],
Expand All @@ -79,7 +91,18 @@ protected function openCsvFile($fileName)
public function writeRow(array $row)
{
$str = $this->rowToStr($row);
$ret = @fwrite($this->getFilePointer(), $str);
try {
$ret = @fwrite($this->getFilePointer(), $str);
} catch (TypeError $e) {
throw new Exception(
'Cannot write to CSV file ' . $this->fileName .
'Error: ' . $e->getMessage() .
' Return: false' .
' To write: ' . strlen($str) . ' Written: 0',
Exception::WRITE_ERROR,
$e
);
}

/* According to http://php.net/fwrite the fwrite() function
should return false on error. However not writing the full
Expand Down
15 changes: 13 additions & 2 deletions tests/CsvWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,16 @@ public function testInvalidFileName($filename, $message)

public function invalidFileNameProvider()
{
if (PHP_VERSION_ID < 80000) {
return [
['', 'Filename cannot be empty'],
["\0", 'fopen() expects parameter 1 to be a valid path, string given'],
];
}

return [
['', 'Filename cannot be empty'],
["\0", 'fopen() expects parameter 1 to be a valid path, string given'],
['', 'Path cannot be empty'],
["\0", 'Argument #1 ($filename) must not contain any null bytes'],
];
}

Expand Down Expand Up @@ -212,6 +219,10 @@ public function testInvalidPointer()
'Cannot write to CSV file Error: fwrite(): ' .
'write of 14 bytes failed with errno=9 Bad file descriptor Return: false To write: 14 Written: 0'
),
new StringContains(
'Cannot write to CSV file Error: fwrite(): ' .
'Write of 14 bytes failed with errno=9 Bad file descriptor Return: false To write: 14 Written: 0'
)
]);
self::assertThat($e->getMessage(), $or);
}
Expand Down

0 comments on commit 151d3e0

Please sign in to comment.