|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use SilverStripe\SsPak\DataExtractor\CsvTableWriter; |
|
4
|
|
|
|
|
5
|
|
|
class CsvTableWriterTest extends PHPUnit_Framework_TestCase |
|
6
|
|
|
{ |
|
7
|
|
|
function testCsvReading() { |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
if (file_exists('/tmp/output.csv')) { |
|
10
|
|
|
unlink('/tmp/output.csv'); |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
$csv = new CsvTableWriter('/tmp/output.csv'); |
|
14
|
|
|
|
|
15
|
|
|
$csv->start(['Col1', 'Col2', 'Col3']); |
|
16
|
|
|
$csv->writeRecord([ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ]); |
|
17
|
|
|
$csv->writeRecord([ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ]); |
|
18
|
|
|
$csv->finish(); |
|
19
|
|
|
|
|
20
|
|
|
$csvContent = file_get_contents('/tmp/output.csv'); |
|
21
|
|
|
unlink('/tmp/output.csv'); |
|
22
|
|
|
|
|
23
|
|
|
$fixture = file_get_contents(__DIR__ . '/fixture/input.csv'); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertEquals($fixture, $csvContent); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
function testNoStartCall() { |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
if (file_exists('/tmp/output.csv')) { |
|
31
|
|
|
unlink('/tmp/output.csv'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$csv = new CsvTableWriter('/tmp/output.csv'); |
|
35
|
|
|
|
|
36
|
|
|
$csv->writeRecord([ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ]); |
|
37
|
|
|
$csv->writeRecord([ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ]); |
|
38
|
|
|
$csv->finish(); |
|
39
|
|
|
|
|
40
|
|
|
$csvContent = file_get_contents('/tmp/output.csv'); |
|
41
|
|
|
unlink('/tmp/output.csv'); |
|
42
|
|
|
|
|
43
|
|
|
$fixture = file_get_contents(__DIR__ . '/fixture/input.csv'); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertEquals($fixture, $csvContent); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.