1--TEST--
2Test fputcsv() function : error conditions
3--FILE--
4<?php
5/*
6 Prototype: int fputcsv ( resource $handle [, array $fields [, string $delimiter [, string $enclosure]]] );
7 Description:fputcsv() formats a line (passed as a fields array) as CSV and write it to the specified file
8   handle. Returns the length of the written string, or FALSE on failure.
9*/
10
11echo "*** Testing error conditions ***\n";
12// zero argument
13echo "-- Testing fputcsv() with zero argument --\n";
14var_dump( fputcsv() );
15
16// more than expected no. of args
17echo "-- Testing fputcsv() with more than expected number of arguments --\n";
18$fp = fopen(__FILE__, "r");
19$fields = array("fld1", "fld2");
20$delim = ";";
21$enclosure ="\"";
22var_dump( fputcsv($fp, $fields, $delim, $enclosure, $fp) );
23fclose($fp);
24
25// test invalid arguments : non-resources
26echo "-- Testing fputcsv() with invalid arguments --\n";
27$invalid_args = array (
28  "string",
29  10,
30  10.5,
31  true,
32  array(1,2,3),
33  new stdclass,
34);
35/* loop to test fputcsv() with different invalid type of args */
36for($loop_counter = 1; $loop_counter <= count($invalid_args); $loop_counter++) {
37  echo "-- Iteration $loop_counter --\n";
38  var_dump( fputcsv($invalid_args[$loop_counter - 1]) ); // with default args
39  var_dump( fputcsv($invalid_args[$loop_counter - 1], $fields, $delim, $enclosure) ); // all args specified
40}
41
42echo "Done\n";
43--EXPECTF--
44*** Testing error conditions ***
45-- Testing fputcsv() with zero argument --
46
47Warning: fputcsv() expects at least 2 parameters, 0 given in %s on line %d
48NULL
49-- Testing fputcsv() with more than expected number of arguments --
50
51Warning: fputcsv() expects parameter 5 to be string, resource given in %s on line %d
52NULL
53-- Testing fputcsv() with invalid arguments --
54-- Iteration 1 --
55
56Warning: fputcsv() expects at least 2 parameters, 1 given in %s on line %d
57NULL
58
59Warning: fputcsv() expects parameter 1 to be resource, string given in %s on line %d
60NULL
61-- Iteration 2 --
62
63Warning: fputcsv() expects at least 2 parameters, 1 given in %s on line %d
64NULL
65
66Warning: fputcsv() expects parameter 1 to be resource, integer given in %s on line %d
67NULL
68-- Iteration 3 --
69
70Warning: fputcsv() expects at least 2 parameters, 1 given in %s on line %d
71NULL
72
73Warning: fputcsv() expects parameter 1 to be resource, float given in %s on line %d
74NULL
75-- Iteration 4 --
76
77Warning: fputcsv() expects at least 2 parameters, 1 given in %s on line %d
78NULL
79
80Warning: fputcsv() expects parameter 1 to be resource, boolean given in %s on line %d
81NULL
82-- Iteration 5 --
83
84Warning: fputcsv() expects at least 2 parameters, 1 given in %s on line %d
85NULL
86
87Warning: fputcsv() expects parameter 1 to be resource, array given in %s on line %d
88NULL
89-- Iteration 6 --
90
91Warning: fputcsv() expects at least 2 parameters, 1 given in %s on line %d
92NULL
93
94Warning: fputcsv() expects parameter 1 to be resource, object given in %s on line %d
95NULL
96Done
97