Skip to content

Commit

Permalink
Fix test compatibility with PHP 7.3+
Browse files Browse the repository at this point in the history
  • Loading branch information
michaljurecko committed Jul 27, 2020
1 parent a6d0826 commit 86f24a0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ protected function openCsvFile($fileName)
*/
protected function detectLineBreak()
{
rewind($this->getFilePointer());
$sample = fread($this->getFilePointer(), 10000);
@rewind($this->getFilePointer());
$sample = @fread($this->getFilePointer(), 10000);

return LineBreaksHelper::detectLineBreaks($sample, $this->getEnclosure(), $this->getEscapedBy());
}
Expand All @@ -129,7 +129,7 @@ protected function readLine()
// allow empty enclosure hack
$enclosure = !$this->getEnclosure() ? chr(0) : $this->getEnclosure();
$escapedBy = !$this->getEscapedBy() ? chr(0) : $this->getEscapedBy();
return fgetcsv($this->getFilePointer(), null, $this->getDelimiter(), $enclosure, $escapedBy);
return @fgetcsv($this->getFilePointer(), null, $this->getDelimiter(), $enclosure, $escapedBy);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function writeRow(array $row)
"Cannot write to CSV file " . $this->fileName .
($ret === false && error_get_last() ? 'Error: ' . error_get_last()['message'] : '') .
' Return: ' . json_encode($ret) .
' To write: ' . strlen($str) . ' Written: ' . $ret,
' To write: ' . strlen($str) . ' Written: ' . (int) $ret,
Exception::WRITE_ERROR
);
}
Expand Down
39 changes: 33 additions & 6 deletions tests/CsvWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Keboola\Csv\CsvWriter;
use Keboola\Csv\Exception;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_Constraint_Or;
use PHPUnit_Framework_Constraint_StringContains;

class CsvWriteTest extends TestCase
{
Expand Down Expand Up @@ -87,9 +89,19 @@ public function testWriteInvalidObject()
];

$csvFile->writeRow($rows[0]);
self::expectException(Exception::class);
self::expectExceptionMessage("Cannot write data into column: stdClass::");
$csvFile->writeRow($rows[1]);

try {
$csvFile->writeRow($rows[1]);
self::fail('Expected exception was not thrown.');
} catch (Exception $e) {
// Exception message differs between PHP versions.
$or = new PHPUnit_Framework_Constraint_Or();
$or->setConstraints([
new PHPUnit_Framework_Constraint_StringContains("Cannot write data into column: stdClass::"),
new PHPUnit_Framework_Constraint_StringContains("Cannot write data into column: (object) array(\n)")
]);
self::assertThat($e->getMessage(), $or);
}
}

public function testWriteValidObject()
Expand Down Expand Up @@ -182,9 +194,24 @@ public function testInvalidPointer()
$pointer = fopen($fileName, 'r');
$csvFile = new CsvWriter($pointer);
$rows = [['col1', 'col2']];
self::expectException(Exception::class);
self::expectExceptionMessage('Cannot write to CSV file Return: 0 To write: 14 Written: 0');
$csvFile->writeRow($rows[0]);

try {
$csvFile->writeRow($rows[0]);
self::fail('Expected exception was not thrown.');
} catch (Exception $e) {
// Exception message differs between PHP versions.
$or = new PHPUnit_Framework_Constraint_Or();
$or->setConstraints([
new PHPUnit_Framework_Constraint_StringContains(
'Cannot write to CSV file Return: 0 To write: 14 Written: 0'
),
new PHPUnit_Framework_Constraint_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);
}
}

public function testInvalidPointer2()
Expand Down

0 comments on commit 86f24a0

Please sign in to comment.