|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace itertools; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
|
|
7
|
|
|
class StringCsvIteratorTest extends PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** @test */ |
|
10
|
|
|
public function testStringCsvIteratorWithHeaderInFirstLine() |
|
11
|
|
|
{ |
|
12
|
|
|
$it = new StringCsvIterator(<<<EOF |
|
13
|
|
|
"col1", "col2" |
|
14
|
|
|
"a11", "a12" |
|
15
|
|
|
"a21", "a22" |
|
16
|
|
|
EOF |
|
17
|
|
|
); |
|
18
|
|
|
|
|
19
|
|
|
$data = iterator_to_array($it); |
|
20
|
|
|
$this->assertEquals(2, count($data)); |
|
21
|
|
|
$this->assertEquals('a22', $data[1]['col2']); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** @test */ |
|
25
|
|
|
public function testStringCsvIteratorWithHeaderSpecified() |
|
26
|
|
|
{ |
|
27
|
|
|
$it = new StringCsvIterator(<<<EOF |
|
28
|
|
|
"a11", "a12" |
|
29
|
|
|
"a21", "a22" |
|
30
|
|
|
EOF |
|
31
|
|
|
, array('header' => array('col1', 'col2'))); |
|
32
|
|
|
|
|
33
|
|
|
$data = iterator_to_array($it); |
|
34
|
|
|
$this->assertEquals(2, count($data)); |
|
35
|
|
|
$this->assertEquals('a22', $data[1]['col2']); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @test |
|
40
|
|
|
* @expectedException itertools\InvalidCsvException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testStringCsvWithMissingRowsException() |
|
43
|
|
|
{ |
|
44
|
|
|
$it = new StringCsvIterator(<<<EOF |
|
45
|
|
|
"col1", "col2" |
|
46
|
|
|
"a21" |
|
47
|
|
|
EOF |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$data = iterator_to_array($it); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @test */ |
|
54
|
|
|
public function testStringCsvWithMissingRowsIgnored() |
|
55
|
|
|
{ |
|
56
|
|
|
$it = new StringCsvIterator(<<<EOF |
|
57
|
|
|
"col1", "col2" |
|
58
|
|
|
"a21" |
|
59
|
|
|
EOF |
|
60
|
|
|
, array('ignoreMissingRows' => true)); |
|
61
|
|
|
|
|
62
|
|
|
$data = iterator_to_array($it); |
|
63
|
|
|
$this->assertEquals(array('col1' => 'a21', 'col2' => null), $data[0]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @test */ |
|
67
|
|
|
public function testStringCsvWithToMuchRowsIgnored() |
|
68
|
|
|
{ |
|
69
|
|
|
$it = new StringCsvIterator(<<<EOF |
|
70
|
|
|
"col1", "col2" |
|
71
|
|
|
"a21", "a22", "a23" |
|
72
|
|
|
EOF |
|
73
|
|
|
, array('ignoreMissingRows' => true)); |
|
74
|
|
|
|
|
75
|
|
|
$data = iterator_to_array($it); |
|
76
|
|
|
$this->assertEquals(array('col1' => 'a21', 'col2' => 'a22'), $data[0]); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.