1--TEST--
2Test ftruncate() function : error conditions
3--FILE--
4<?php
5/*
6 Prototype: bool ftruncate ( resource $handle, int $size );
7 Description: truncates a file to a given length
8*/
9
10echo "*** Testing ftruncate() : error conditions ***\n";
11
12$filename = dirname(__FILE__)."/ftruncate_error.tmp";
13$file_handle = fopen($filename, "w" );
14fwrite($file_handle, (binary)"Testing ftruncate error conditions \n");
15fflush($file_handle);
16echo "\n Initial file size = ".filesize($filename)."\n";
17
18echo "-- Testing ftruncate() with less than expected number of arguments --\n";
19
20// zero arguments
21var_dump( ftruncate() );
22
23// arguments less than expected numbers
24var_dump( ftruncate( $file_handle ) );
25// check the first size
26var_dump( filesize($filename) );
27
28echo "-- Testing ftruncate() with more than expected number of arguments --\n";
29// more than expected number of arguments
30var_dump( ftruncate($file_handle, 10, 20) );
31// check the first size
32var_dump( filesize($filename) );
33
34// test invalid arguments : non-resources
35echo "-- Testing ftruncate() with invalid file pointer --\n";
36$invalid_args = array (
37  "string",
38  10,
39  10.5,
40  true,
41  array(1,2,3),
42  new stdclass,
43);
44/* loop to test ftruncate() with different invalid type of args */
45for($loop_counter = 1; $loop_counter <= count($invalid_args); $loop_counter++) {
46  echo "-- Iteration $loop_counter --\n";
47  var_dump( ftruncate($invalid_args[$loop_counter - 1], 10) );
48}
49
50// ftruncate() on a file handle which is already closed/unset
51echo "-- Testing ftruncate() with closed/unset file handle --\n";
52
53// ftruncate on close file handle
54fclose($file_handle);
55var_dump( ftruncate($file_handle,10) );
56// check the first size
57var_dump( filesize($filename) );
58
59// ftruncate on a file handle which is unset
60$fp = fopen($filename, "w");
61unset($fp); //unset file handle
62var_dump( ftruncate(@$fp,10));
63// check the first size
64var_dump( filesize($filename) );
65
66echo "Done\n";
67?>
68--CLEAN--
69<?php
70$filename = dirname(__FILE__)."/ftruncate_error.tmp";
71unlink( $filename );
72?>
73--EXPECTF--
74*** Testing ftruncate() : error conditions ***
75
76 Initial file size = 36
77-- Testing ftruncate() with less than expected number of arguments --
78
79Warning: ftruncate() expects exactly 2 parameters, 0 given in %s on line %d
80bool(false)
81
82Warning: ftruncate() expects exactly 2 parameters, 1 given in %s on line %d
83bool(false)
84int(36)
85-- Testing ftruncate() with more than expected number of arguments --
86
87Warning: ftruncate() expects exactly 2 parameters, 3 given in %s on line %d
88bool(false)
89int(36)
90-- Testing ftruncate() with invalid file pointer --
91-- Iteration 1 --
92
93Warning: ftruncate() expects parameter 1 to be resource, string given in %s on line %d
94bool(false)
95-- Iteration 2 --
96
97Warning: ftruncate() expects parameter 1 to be resource, integer given in %s on line %d
98bool(false)
99-- Iteration 3 --
100
101Warning: ftruncate() expects parameter 1 to be resource, float given in %s on line %d
102bool(false)
103-- Iteration 4 --
104
105Warning: ftruncate() expects parameter 1 to be resource, boolean given in %s on line %d
106bool(false)
107-- Iteration 5 --
108
109Warning: ftruncate() expects parameter 1 to be resource, array given in %s on line %d
110bool(false)
111-- Iteration 6 --
112
113Warning: ftruncate() expects parameter 1 to be resource, object given in %s on line %d
114bool(false)
115-- Testing ftruncate() with closed/unset file handle --
116
117Warning: ftruncate(): supplied resource is not a valid stream resource in %s on line %d
118bool(false)
119int(36)
120
121Warning: ftruncate() expects parameter 1 to be resource, null given in %s on line %d
122bool(false)
123int(36)
124Done
125