I'm trying to unit test methods in isolation that work with csv files, using vsf-stream from here: https://github.com/mikey179/vfsStream
I've created a 2 dimensional data array to test, but when I try to add this to the mocked file it seems to parse the newline characters, instead of adding new lines. I've tried both fputcsv
and fputs
but both have the same results. Using the same data and php functions I can write successfully to a normal file, the issues seems to be with vfs-stream. Not much documentation on the net and completely lost:
$this->data = array(
array( 'col1', 'col2', 'col3', 'col4' ),
array( 'row 0 1', 'row 0 2', ' row 0 3', ' row 0 4' ),
array( 'row 1 1', 'row 1 2', ' row 1 3', ' row 1 4' ),
array( 'row 2 1', 'row 2 2', ' row 2 3', ' row 2 4' ),
array( 'row 3 1', 'row 3 2', ' row 3 3', ' row 3 4' ),
array( 'row 4 1', 'row 4 2', ' row 4 3', ' row 4 4' ),
array( 'row 5 1', 'row 5 2', ' row 5 3', ' row 5 4' ),
);
$this->root = vfsStream::setup('test-dir');
$file = vfsStream::url('test-dir/foo.csv');
$handle = fopen($file, "a+");
foreach( $this->data as $arr)
fputcsv($handle, $arr, "|"); //$content .= implode("|", $arr) . "\n";
The above code will produce the mocked file but without any newline characters, so parsing the mocked csv file later in the test won't work as it seems to all be written in the one long line:
var_dump( file_get_contents( $file ) );
will produce:
string(272) "col1|col2|col3|col4\n"row 0 1"|"row 0 2"|" row 0 3"|" row 0 4"\n"row 1 1"|"row 1 2"|" row 1 3"|" row 1 4"\n"row 2 1"|"row 2 2"|" row 2 3"|" row 2 4"\n"row 3 1"|"row 3 2"|" row 3 3"|" row 3 4"\n"row 4 1"|"row 4 2"|" row 4 3"|" row 4 4"\n"row 5 1"|"row 5 2"|" row 5 3"|" row 5 4"\n"
I'm hoping that I'm missing a flag for this and its not a bug. Any help appreciated, cheers.