1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class CsvTest extends PHPUnit_Framework_TestCase |
4
|
|
|
{ |
5
|
|
|
protected function setUp() |
6
|
|
|
{ |
7
|
|
|
$this->string = "id,name\n1,Bob\n2,Bill"; |
8
|
|
|
$this->parser = new \CsvParser\Parser(); |
9
|
|
|
$this->csv = $this->parser->fromString($this->string); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public function testAppendRow() |
13
|
|
|
{ |
14
|
|
|
$actual = $this->parser->toArray($this->csv); |
15
|
|
|
$expected = array(array('id'=>1, 'name'=>'Bob'),array('id'=>2, 'name'=>'Bill')); |
16
|
|
|
$this->assertEquals($expected, $actual); |
17
|
|
|
|
18
|
|
|
$this->csv->appendRow(array('id'=>3, 'name'=>'Ben')); |
19
|
|
|
$actual = $this->parser->toArray($this->csv); |
20
|
|
|
$expected = array(array('id'=>3, 'name'=>'Ben'),array('id'=>1, 'name'=>'Bob'),array('id'=>2, 'name'=>'Bill')); |
21
|
|
|
$this->assertEquals($expected, $actual); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testPrependRow() |
25
|
|
|
{ |
26
|
|
|
$this->string = "id,name\n1,Bob\n2,Bill"; |
27
|
|
|
$this->parser = new \CsvParser\Parser(); |
28
|
|
|
$this->csv = $this->parser->fromString($this->string); |
29
|
|
|
|
30
|
|
|
$this->csv->prependRow(array('id'=>3, 'name'=>'Ben')); |
31
|
|
|
$actual = $this->parser->toArray($this->csv); |
32
|
|
|
$expected = array(array('id'=>1, 'name'=>'Bob'),array('id'=>2, 'name'=>'Bill'),array('id'=>3, 'name'=>'Ben')); |
33
|
|
|
$this->assertEquals($expected, $actual); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testGetRowCount() |
37
|
|
|
{ |
38
|
|
|
$this->string = "id,name\n1,Bob\n2,Bill"; |
39
|
|
|
$this->parser = new \CsvParser\Parser(); |
40
|
|
|
$this->csv = $this->parser->fromString($this->string); |
41
|
|
|
|
42
|
|
|
$this->assertEquals(2, $this->csv->getRowCount()); |
43
|
|
|
|
44
|
|
|
$this->csv->appendRow(array('id'=>3, 'name'=>'Ben')); |
45
|
|
|
$this->assertEquals(3, $this->csv->getRowCount()); |
46
|
|
|
|
47
|
|
|
$this->csv->prependRow(array('id'=>77, 'name'=>'Benji')); |
48
|
|
|
$this->assertEquals(4, $this->csv->getRowCount()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testColumnExists() |
52
|
|
|
{ |
53
|
|
|
$this->string = "id,name\n1,Bob\n2,Bill"; |
54
|
|
|
$this->parser = new \CsvParser\Parser(); |
55
|
|
|
$this->csv = $this->parser->fromString($this->string); |
56
|
|
|
|
57
|
|
|
$this->assertTrue($this->csv->columnExists('name')); |
58
|
|
|
$this->assertFalse($this->csv->columnExists('somethingelse')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testMapColumn() |
62
|
|
|
{ |
63
|
|
|
$this->csv->mapColumn('name', function ($val) { |
64
|
|
|
return 'Sir ' . $val; |
65
|
|
|
}); |
66
|
|
|
$actual = $this->parser->toArray($this->csv); |
67
|
|
|
$expected = array(array('id'=>1, 'name'=>'Sir Bob'),array('id'=>2, 'name'=>'Sir Bill')); |
68
|
|
|
$this->assertEquals($expected, $actual); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testMapRows() |
72
|
|
|
{ |
73
|
|
|
$this->csv->mapRows(function ($row) { |
74
|
|
|
$row['codename'] = base64_encode($row['id']); |
75
|
|
|
return $row; |
76
|
|
|
}); |
77
|
|
|
$actual = $this->parser->toArray($this->csv); |
78
|
|
|
$expected = array(array('id'=>1, 'name'=>'Bob','codename'=>'MQ=='),array('id'=>2, 'name'=>'Bill','codename'=>'Mg==')); |
79
|
|
|
$this->assertEquals($expected, $actual); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testFilterRows() |
83
|
|
|
{ |
84
|
|
|
$this->csv->filterRows(function ($row) { |
85
|
|
|
return $row['id'] != 2; |
86
|
|
|
}); |
87
|
|
|
$actual = $this->parser->toArray($this->csv); |
88
|
|
|
$expected = array(array('id'=>1, 'name'=>'Bob')); |
89
|
|
|
$this->assertEquals($expected, $actual); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testRemoveDuplicates() |
93
|
|
|
{ |
94
|
|
|
$string = "id,name\n1,Bob\n2,Bill\n3,Bob\n4,James"; |
95
|
|
|
$parser = new \CsvParser\Parser(); |
96
|
|
|
$csv = $parser->fromString($string); |
97
|
|
|
|
98
|
|
|
$csv->removeDuplicates('name'); |
99
|
|
|
|
100
|
|
|
$actual = $parser->toArray($csv); |
101
|
|
|
$expected = array(0=>array('id'=>1, 'name'=>'Bob'), 1=>array('id'=>2, 'name'=>'Bill'), 3=>array('id'=>4, 'name'=>'James')); |
102
|
|
|
$this->assertEquals($expected, $actual); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testRemoveBlanks() |
106
|
|
|
{ |
107
|
|
|
$string = "id,name\n1,Bob\n2,Bill\n3,\n4,James"; |
108
|
|
|
$parser = new \CsvParser\Parser(); |
109
|
|
|
$csv = $parser->fromString($string); |
110
|
|
|
|
111
|
|
|
$csv->removeBlanks('name'); |
112
|
|
|
|
113
|
|
|
$actual = $parser->toArray($csv); |
114
|
|
|
$expected = array(0=>array('id'=>1, 'name'=>'Bob'), 1=>array('id'=>2, 'name'=>'Bill'), 3=>array('id'=>4, 'name'=>'James')); |
115
|
|
|
$this->assertEquals($expected, $actual); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testFirst() |
119
|
|
|
{ |
120
|
|
|
$string = "id,name\n1,Bob\n2,Bill\n3,\n4,James"; |
121
|
|
|
$parser = new \CsvParser\Parser(); |
122
|
|
|
$csv = $parser->fromString($string); |
123
|
|
|
$expected = ['id' => 1, 'name' => 'Bob']; |
124
|
|
|
$this->assertEquals($expected, $csv->first()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testGetKeys() |
128
|
|
|
{ |
129
|
|
|
$string = "id,name\n1,Bob\n2,Bill\n3,\n4,James"; |
130
|
|
|
$parser = new \CsvParser\Parser(); |
131
|
|
|
$csv = $parser->fromString($string); |
132
|
|
|
$expected = ['id', 'name']; |
133
|
|
|
$this->assertEquals($expected, $csv->getKeys()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testReKey() |
137
|
|
|
{ |
138
|
|
|
$string = "id,name\n1,Bob\n2,Bill\n3,\n4,James"; |
139
|
|
|
$parser = new \CsvParser\Parser(); |
140
|
|
|
$csv = $parser->fromString($string); |
141
|
|
|
$csv->reKey(['name', 'id']); |
142
|
|
|
$this->assertEquals("\"name\",\"id\"\n\"Bob\",\"1\"\n\"Bill\",\"2\"\n\"\",\"3\"\n\"James\",\"4\"", $parser->toString($csv)); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|