1--TEST--
2SplFileObject::fputcsv(): Usage variations -- with line without any CSV fields
3--FILE--
4<?php
5
6/* Testing fputcsv() to write to a file when the field has no CSV format */
7
8echo "*** Testing fputcsv() : with no CSV format in the field ***\n";
9
10/* the array is with three elements in it. Each element should be read as
11   1st element is delimiter, 2nd element is enclosure
12   and 3rd element is csv fields
13*/
14
15$fields = array( array('water_fruit\n'),
16                array("water_fruit\n"),
17                array("")
18         );
19
20$file_path = __DIR__;
21$file = "$file_path/fputcsv_variation10.tmp";
22
23$file_modes = array ("r+", "r+b", "r+t",
24                     "a+", "a+b", "a+t",
25                     "w+", "w+b", "w+t",
26                     "x+", "x+b", "x+t");
27
28$loop_counter = 1;
29foreach ($fields as $field) {
30  for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
31
32    echo "\n-- file opened in $file_modes[$mode_counter] --\n";
33    // create the file and add the content with has csv fields
34    if ( strstr($file_modes[$mode_counter], "r") ) {
35      $fo = new SplFileObject($file, 'w');
36    } else {
37      $fo = new SplFileObject($file, $file_modes[$mode_counter]);
38    }
39    $fo->setCsvControl(escape: '\\');
40
41    $csv_field = $field;
42
43    // write to a file in csv format
44    var_dump( $fo->fputcsv($csv_field) );
45
46    // check the file pointer position and eof
47    var_dump( $fo->ftell() );
48    var_dump( $fo->eof() );
49    //close the file
50    unset($fo);
51
52    // print the file contents
53    var_dump( file_get_contents($file) );
54
55    //delete file
56    unlink($file);
57  } //end of mode loop
58} // end of foreach
59
60echo "Done\n";
61?>
62--EXPECTF--
63*** Testing fputcsv() : with no CSV format in the field ***
64
65-- file opened in r+ --
66int(16)
67int(16)
68bool(false)
69string(16) ""water_fruit\n"
70"
71
72-- file opened in r+b --
73int(16)
74int(16)
75bool(false)
76string(16) ""water_fruit\n"
77"
78
79-- file opened in r+t --
80int(16)
81int(16)
82bool(false)
83string(%d) ""water_fruit\n"
84"
85
86-- file opened in a+ --
87int(16)
88int(16)
89bool(false)
90string(16) ""water_fruit\n"
91"
92
93-- file opened in a+b --
94int(16)
95int(16)
96bool(false)
97string(16) ""water_fruit\n"
98"
99
100-- file opened in a+t --
101int(16)
102int(16)
103bool(false)
104string(%d) ""water_fruit\n"
105"
106
107-- file opened in w+ --
108int(16)
109int(16)
110bool(false)
111string(16) ""water_fruit\n"
112"
113
114-- file opened in w+b --
115int(16)
116int(16)
117bool(false)
118string(16) ""water_fruit\n"
119"
120
121-- file opened in w+t --
122int(16)
123int(16)
124bool(false)
125string(%d) ""water_fruit\n"
126"
127
128-- file opened in x+ --
129int(16)
130int(16)
131bool(false)
132string(16) ""water_fruit\n"
133"
134
135-- file opened in x+b --
136int(16)
137int(16)
138bool(false)
139string(16) ""water_fruit\n"
140"
141
142-- file opened in x+t --
143int(16)
144int(16)
145bool(false)
146string(%d) ""water_fruit\n"
147"
148
149-- file opened in r+ --
150int(15)
151int(15)
152bool(false)
153string(15) ""water_fruit
154"
155"
156
157-- file opened in r+b --
158int(15)
159int(15)
160bool(false)
161string(15) ""water_fruit
162"
163"
164
165-- file opened in r+t --
166int(15)
167int(15)
168bool(false)
169string(%d) ""water_fruit
170"
171"
172
173-- file opened in a+ --
174int(15)
175int(15)
176bool(false)
177string(15) ""water_fruit
178"
179"
180
181-- file opened in a+b --
182int(15)
183int(15)
184bool(false)
185string(15) ""water_fruit
186"
187"
188
189-- file opened in a+t --
190int(15)
191int(15)
192bool(false)
193string(%d) ""water_fruit
194"
195"
196
197-- file opened in w+ --
198int(15)
199int(15)
200bool(false)
201string(15) ""water_fruit
202"
203"
204
205-- file opened in w+b --
206int(15)
207int(15)
208bool(false)
209string(15) ""water_fruit
210"
211"
212
213-- file opened in w+t --
214int(15)
215int(15)
216bool(false)
217string(%d) ""water_fruit
218"
219"
220
221-- file opened in x+ --
222int(15)
223int(15)
224bool(false)
225string(15) ""water_fruit
226"
227"
228
229-- file opened in x+b --
230int(15)
231int(15)
232bool(false)
233string(15) ""water_fruit
234"
235"
236
237-- file opened in x+t --
238int(15)
239int(15)
240bool(false)
241string(%d) ""water_fruit
242"
243"
244
245-- file opened in r+ --
246int(1)
247int(1)
248bool(false)
249string(1) "
250"
251
252-- file opened in r+b --
253int(1)
254int(1)
255bool(false)
256string(1) "
257"
258
259-- file opened in r+t --
260int(1)
261int(1)
262bool(false)
263string(%d) "
264"
265
266-- file opened in a+ --
267int(1)
268int(1)
269bool(false)
270string(1) "
271"
272
273-- file opened in a+b --
274int(1)
275int(1)
276bool(false)
277string(1) "
278"
279
280-- file opened in a+t --
281int(1)
282int(1)
283bool(false)
284string(%d) "
285"
286
287-- file opened in w+ --
288int(1)
289int(1)
290bool(false)
291string(1) "
292"
293
294-- file opened in w+b --
295int(1)
296int(1)
297bool(false)
298string(1) "
299"
300
301-- file opened in w+t --
302int(1)
303int(1)
304bool(false)
305string(%d) "
306"
307
308-- file opened in x+ --
309int(1)
310int(1)
311bool(false)
312string(1) "
313"
314
315-- file opened in x+b --
316int(1)
317int(1)
318bool(false)
319string(1) "
320"
321
322-- file opened in x+t --
323int(1)
324int(1)
325bool(false)
326string(%d) "
327"
328Done
329