1 | <?php |
||
10 | class FileCsvIteratorTest extends PHPUnit_Framework_TestCase |
||
11 | { |
||
12 | /** @test */ |
||
13 | public function testFileCsvIteratorWithHeaderCombinedHeader() |
||
29 | |||
30 | /** @test */ |
||
31 | public function testFileCsvIteratorWithHeader() |
||
32 | { |
||
33 | $fp = $this->getMemoryFileHandle(<<<EOF |
||
34 | "col1", "col2" |
||
35 | "a11", "a12" |
||
36 | "a21", "a22" |
||
37 | EOF |
||
38 | ); |
||
39 | |||
40 | $data = iterator_to_array(new FileCsvIterator($fp)); |
||
41 | $this->assertEquals(2, count($data)); |
||
42 | $this->assertEquals('a22', $data[1]['col2']); |
||
43 | } |
||
44 | |||
45 | /** @test */ |
||
46 | public function testFileCsvIteratorWithHeaderAndNoContent() |
||
47 | { |
||
48 | $fp = $this->getMemoryFileHandle(<<<EOF |
||
49 | "col1", "col2" |
||
50 | EOF |
||
51 | ); |
||
52 | $data = iterator_to_array(new FileCsvIterator($fp)); |
||
53 | $this->assertEquals(0, count($data)); |
||
54 | } |
||
55 | |||
56 | /** @test */ |
||
57 | public function testFileCsvIteratorWithHeaderAndNoData() |
||
58 | { |
||
59 | $fp = $this->getMemoryFileHandle(<<<EOF |
||
60 | EOF |
||
61 | ); |
||
62 | |||
63 | $data = iterator_to_array(new FileCsvIterator($fp)); |
||
64 | $this->assertEquals(0, count($data)); |
||
65 | } |
||
66 | |||
67 | /** @test */ |
||
68 | public function testFileCsvIteratorFromSplFileInfo() |
||
73 | |||
74 | /** @test */ |
||
75 | public function testFileCsvIteratorWithoutHeader() |
||
76 | { |
||
77 | $fp = $this->getMemoryFileHandle(<<<EOF |
||
78 | "col1", "col2" |
||
79 | "a11", "a12" |
||
80 | EOF |
||
81 | ); |
||
82 | |||
83 | $data = iterator_to_array(new FileCsvIterator($fp, array('hasHeader' => false))); |
||
84 | $this->assertEquals(2, count($data)); |
||
85 | $this->assertEquals('col1', $data[0][0]); |
||
86 | } |
||
87 | |||
88 | /** @test */ |
||
89 | public function testFileCsvIteratorWithReplacedHeader() |
||
102 | |||
103 | /** @test */ |
||
104 | public function testFileCsvIteratorWithEnclosureSameAsDelimiter() |
||
105 | { |
||
106 | $fp = $this->getMemoryFileHandle(<<<EOF |
||
107 | col1,col2 |
||
108 | a11,a12 |
||
109 | "a2""1",a22 |
||
110 | EOF |
||
111 | ); |
||
112 | |||
113 | $data = iterator_to_array(new FileCsvIterator($fp, array('escape' => '"', 'enclosure' => '"'))); |
||
114 | $this->assertEquals(2, count($data)); |
||
115 | $this->assertEquals('a11', $data[0]['col1']); |
||
116 | $this->assertEquals('a2"1', $data[1]['col1']); |
||
117 | $this->assertEquals('a22', $data[1]['col2']); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @test |
||
122 | * @expectedException InvalidArgumentException |
||
123 | */ |
||
124 | public function testFileCsvIteratorWithInvalidArguments() |
||
128 | |||
129 | /** @test */ |
||
130 | public function testClosingHandle() |
||
135 | |||
136 | /** |
||
137 | * @test |
||
138 | * @expectedException InvalidArgumentException |
||
139 | */ |
||
140 | public function testFileCsvIteratorWithNonExistingFilePath() |
||
144 | |||
145 | /** |
||
146 | * @test |
||
147 | * @expectedException InvalidArgumentException |
||
148 | */ |
||
149 | public function testFileCsvIteratorWithInvalidOptions() |
||
153 | |||
154 | /** @test */ |
||
155 | public function testFileCsvWithCustomCharacterEncoding() |
||
161 | |||
162 | public function testFileCsvIteratorWithStreamInterface() |
||
180 | |||
181 | protected function getMemoryFileHandle($content) |
||
182 | { |
||
183 | $fp = fopen('php://memory', 'rw'); |
||
184 | fwrite($fp, $content); |
||
185 | rewind($fp); |
||
188 | } |
||
189 | |||
190 |