-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVParserTest.php
More file actions
224 lines (186 loc) · 5.1 KB
/
Copy pathCSVParserTest.php
File metadata and controls
224 lines (186 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* CSV Parser functionality tests
*
* @package FE_CSV_Import_Export\Tests\Unit
*/
use PHPUnit\Framework\TestCase;
/**
* CSV Parser tests
*
* @since 0.9.7
*/
class CSVParserTest extends TestCase {
/**
* Test basic CSV row parsing
*
* @since 0.9.7
* @return void
*/
public function test_parse_csv_row_basic() {
$line = 'John,Doe,30';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ 'John', 'Doe', '30' ], $result );
$this->assertCount( 3, $result );
}
/**
* Test CSV row parsing with quotes
*
* @since 0.9.7
* @return void
*/
public function test_parse_csv_row_with_quotes() {
$line = '"John, Jr.",Doe,30';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ 'John, Jr.', 'Doe', '30' ], $result );
$this->assertCount( 3, $result );
}
/**
* Test CSV row parsing with escaped quotes
*
* @since 0.9.7
* @return void
*/
public function test_parse_csv_row_with_escaped_quotes() {
$line = '"John ""The Rock"" Doe",Developer,40';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ 'John "The Rock" Doe', 'Developer', '40' ], $result );
}
/**
* Test CSV row parsing with semicolon delimiter
*
* @since 0.9.7
* @return void
*/
public function test_parse_csv_row_semicolon_delimiter() {
$line = 'John;Doe;30';
$delimiter = ';';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ 'John', 'Doe', '30' ], $result );
}
/**
* Test CSV row parsing with tab delimiter
*
* @since 0.9.7
* @return void
*/
public function test_parse_csv_row_tab_delimiter() {
$line = "John\tDoe\t30";
$delimiter = "\t";
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ 'John', 'Doe', '30' ], $result );
}
/**
* Test empty CSV line detection
*
* @since 0.9.7
* @return void
*/
public function test_is_empty_csv_line() {
$this->assertTrue( $this->isEmptyCsvLine( '' ) );
$this->assertTrue( $this->isEmptyCsvLine( ' ' ) );
$this->assertTrue( $this->isEmptyCsvLine( "\t\n" ) );
$this->assertFalse( $this->isEmptyCsvLine( 'John,Doe' ) );
$this->assertFalse( $this->isEmptyCsvLine( ' , ' ) );
}
/**
* Test CSV content parsing into lines
*
* @since 0.9.7
* @return void
*/
public function test_parse_csv_content_to_lines() {
$csv_content = "name,email,age\nJohn,john@test.com,30\nJane,jane@test.com,25";
$lines = explode( "\n", $csv_content );
$this->assertCount( 3, $lines );
$this->assertEquals( 'name,email,age', $lines[0] );
$this->assertEquals( 'John,john@test.com,30', $lines[1] );
$this->assertEquals( 'Jane,jane@test.com,25', $lines[2] );
}
/**
* Test CSV header extraction
*
* @since 0.9.7
* @return void
*/
public function test_extract_csv_headers() {
$csv_content = "name,email,age\nJohn,john@test.com,30";
$lines = explode( "\n", $csv_content );
$headers = str_getcsv( $lines[0], ',' );
$this->assertEquals( [ 'name', 'email', 'age' ], $headers );
$this->assertCount( 3, $headers );
}
/**
* Test CSV data row extraction
*
* @since 0.9.7
* @return void
*/
public function test_extract_csv_data_rows() {
$csv_content = "name,email,age\nJohn,john@test.com,30\nJane,jane@test.com,25";
$lines = explode( "\n", $csv_content );
// Skip header and get data rows
$data_rows = array_slice( $lines, 1 );
$this->assertCount( 2, $data_rows );
$row1 = str_getcsv( $data_rows[0], ',' );
$this->assertEquals( [ 'John', 'john@test.com', '30' ], $row1 );
$row2 = str_getcsv( $data_rows[1], ',' );
$this->assertEquals( [ 'Jane', 'jane@test.com', '25' ], $row2 );
}
/**
* Test CSV with special characters
*
* @since 0.9.7
* @return void
*/
public function test_csv_with_special_characters() {
$line = '"John & Jane","Doe-Smith","$30,000"';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ 'John & Jane', 'Doe-Smith', '$30,000' ], $result );
}
/**
* Test CSV with multiline content in quotes
*
* @since 0.9.7
* @return void
*/
public function test_csv_with_multiline_content() {
$line = '"Line 1\nLine 2","Doe",30';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
// Check actual values to test
$this->assertCount( 3, $result );
$this->assertEquals( 'Doe', $result[1] );
$this->assertEquals( '30', $result[2] );
// First element contains newline characters
$this->assertStringContainsString( 'Line 1', $result[0] );
$this->assertStringContainsString( 'Line 2', $result[0] );
}
/**
* Test CSV with empty fields
*
* @since 0.9.7
* @return void
*/
public function test_csv_with_empty_fields() {
$line = 'John,,30';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ 'John', '', '30' ], $result );
$this->assertCount( 3, $result );
}
/**
* Helper method to check if CSV line is empty
*
* @since 0.9.7
* @param string $line CSV line.
* @return bool True if line is empty.
*/
private function isEmptyCsvLine( $line ) {
return empty( trim( $line ) );
}
}