mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Merge pull request #186 from parsecsv/bu-object-test
Added test for the case of objects as cells
This commit is contained in:
@@ -1137,7 +1137,7 @@ class Csv {
|
||||
: '';
|
||||
$enclosure_quoted = preg_quote($this->enclosure, '/');
|
||||
$pattern = "/" . $delimiter_quoted . $enclosure_quoted . "|\n|\r/i";
|
||||
if ($this->enclose_all || preg_match($pattern, $value) || (strpos($value, ' ') === 0 || substr($value, -1) == ' ')) {
|
||||
if ($this->enclose_all || preg_match($pattern, $value) || strpos($value, ' ') === 0 || substr($value, -1) == ' ') {
|
||||
$value = str_replace($this->enclosure, $this->enclosure . $this->enclosure, $value);
|
||||
$value = $this->enclosure . $value . $this->enclosure;
|
||||
}
|
||||
|
||||
13
tests/methods/ObjectThatHasToStringMethod.php
Normal file
13
tests/methods/ObjectThatHasToStringMethod.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\methods;
|
||||
|
||||
/**
|
||||
* Class HasToString is just a helper to test if cells can be objects.
|
||||
*/
|
||||
class ObjectThatHasToStringMethod {
|
||||
|
||||
public function __toString() {
|
||||
return 'some value';
|
||||
}
|
||||
}
|
||||
@@ -88,6 +88,18 @@ class UnparseTest extends Testcase {
|
||||
$this->unparseAndCompare($expected);
|
||||
}
|
||||
|
||||
public function testObjectCells() {
|
||||
$this->csv->data = [
|
||||
[
|
||||
'column1' => new ObjectThatHasToStringMethod(),
|
||||
'column2' => 'boring',
|
||||
],
|
||||
];
|
||||
$this->csv->linefeed = "\n";
|
||||
$expected = "column1,column2\nsome value,boring\n";
|
||||
$this->unparseAndCompare($expected);
|
||||
}
|
||||
|
||||
private function unparseAndCompare($expected, $fields = array()) {
|
||||
$str = $this->csv->unparse($this->csv->data, $fields);
|
||||
$this->assertEquals($expected, $str);
|
||||
|
||||
Reference in New Issue
Block a user