Annotation of embedaddon/php/ext/standard/tests/file/fwrite_variation1.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test fwrite() function : usage variations - r, rb & rt modes
3: --SKIPIF--
4: <?php
5: if( substr(PHP_OS, 0, 3) == 'WIN' ) {
6: die('skip...Not valid for Windows');
7: }
8: ?>
9: --FILE--
10: <?php
11: /*
12: Prototype: int fwrite ( resource $handle,string string, [, int $length] );
13: Description: fwrite() writes the contents of string to the file stream pointed to by handle.
14: If the length arquement is given,writing will stop after length bytes have been
15: written or the end of string reached, whichever comes first.
16: fwrite() returns the number of bytes written or FALSE on error
17: */
18:
19:
20: echo "*** Testing fwrite() various operations ***\n";
21:
22: // include the file.inc for Function: function delete_file($filename)
23: include ("file.inc");
24:
25: /*
26: Test fwrite with file opened in mode : r,rb,rt
27: File having content of type numeric, text,text_with_new_line & alphanumeric
28: */
29:
30: $file_modes = array("r","rb","rt");
31: $file_content_types = array("numeric","text","text_with_new_line","alphanumeric");
32:
33: foreach($file_content_types as $file_content_type) {
34: echo "\n-- Testing fwrite() with file having content of type ". $file_content_type ." --\n";
35:
36: /* open the file using $files_modes and perform fwrite() on it */
37: foreach($file_modes as $file_mode) {
38: echo "-- Opening file in $file_mode --\n";
39:
40: // create the temp file with content of type $file_content_type
41: $filename = dirname(__FILE__)."/fwrite_variation1.tmp"; // this is name of the file
42: create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fwrite_variation");
43:
44: $file_handle = fopen($filename, $file_mode);
45: if(!$file_handle) {
46: echo "Error: failed to fopen() file: $filename!";
47: exit();
48: }
49:
50: $data_to_be_written="";
51: fill_buffer($data_to_be_written,$file_content_type,1024); //get the data of size 1024
52:
53: /* Write the data into the file, verify it by checking the file pointer position, eof position,
54: filesize & by displaying the content */
55:
56: var_dump( ftell($file_handle) ); // expected: 0
57: var_dump( fwrite($file_handle, $data_to_be_written ));
58: var_dump( ftell($file_handle) ); // expected: 0
59: var_dump( feof($file_handle) ); // expected: false
60:
61: // move the file pointer to end of the file and try fwrite()
62: fseek($file_handle, SEEK_END, 0);
63: var_dump( ftell($file_handle) ); // expecting 1024
64: var_dump( fwrite($file_handle, $data_to_be_written) ); // fwrite to fail
65: var_dump( ftell($file_handle) ); //check that file pointer points at eof, expected: 1024
66: var_dump( feof($file_handle) ); // ensure that feof() points to eof, expected: true
67:
68: // ensure that file content/size didn't change.
69: var_dump( fclose($file_handle) );
70: clearstatcache();//clears file status cache
71: var_dump( filesize($filename) ); // expected: 1024
72: var_dump(md5(file_get_contents($filename))); // hash the output
73: delete_file($filename); // delete file with name fwrite_variation1.tmp
74: } // end of inner foreach loop
75: } // end of outer foreach loop
76:
77: echo "Done\n";
78: ?>
79: --EXPECTF--
80: *** Testing fwrite() various operations ***
81:
82: -- Testing fwrite() with file having content of type numeric --
83: -- Opening file in r --
84: int(0)
85: int(0)
86: int(0)
87: bool(false)
88: int(2)
89: int(0)
90: int(2)
91: bool(false)
92: bool(true)
93: int(1024)
94: string(32) "950b7457d1deb6332f2fc5d42f3129d6"
95: -- Opening file in rb --
96: int(0)
97: int(0)
98: int(0)
99: bool(false)
100: int(2)
101: int(0)
102: int(2)
103: bool(false)
104: bool(true)
105: int(1024)
106: string(32) "950b7457d1deb6332f2fc5d42f3129d6"
107: -- Opening file in rt --
108: int(0)
109: int(0)
110: int(0)
111: bool(false)
112: int(2)
113: int(0)
114: int(2)
115: bool(false)
116: bool(true)
117: int(1024)
118: string(32) "950b7457d1deb6332f2fc5d42f3129d6"
119:
120: -- Testing fwrite() with file having content of type text --
121: -- Opening file in r --
122: int(0)
123: int(0)
124: int(0)
125: bool(false)
126: int(2)
127: int(0)
128: int(2)
129: bool(false)
130: bool(true)
131: int(1024)
132: string(32) "e486000c4c8452774f746a27658d87fa"
133: -- Opening file in rb --
134: int(0)
135: int(0)
136: int(0)
137: bool(false)
138: int(2)
139: int(0)
140: int(2)
141: bool(false)
142: bool(true)
143: int(1024)
144: string(32) "e486000c4c8452774f746a27658d87fa"
145: -- Opening file in rt --
146: int(0)
147: int(0)
148: int(0)
149: bool(false)
150: int(2)
151: int(0)
152: int(2)
153: bool(false)
154: bool(true)
155: int(1024)
156: string(32) "e486000c4c8452774f746a27658d87fa"
157:
158: -- Testing fwrite() with file having content of type text_with_new_line --
159: -- Opening file in r --
160: int(0)
161: int(0)
162: int(0)
163: bool(false)
164: int(2)
165: int(0)
166: int(2)
167: bool(false)
168: bool(true)
169: int(1024)
170: string(32) "b09c8026a64a88d36d4c2f17983964bb"
171: -- Opening file in rb --
172: int(0)
173: int(0)
174: int(0)
175: bool(false)
176: int(2)
177: int(0)
178: int(2)
179: bool(false)
180: bool(true)
181: int(1024)
182: string(32) "b09c8026a64a88d36d4c2f17983964bb"
183: -- Opening file in rt --
184: int(0)
185: int(0)
186: int(0)
187: bool(false)
188: int(2)
189: int(0)
190: int(2)
191: bool(false)
192: bool(true)
193: int(1024)
194: string(32) "b09c8026a64a88d36d4c2f17983964bb"
195:
196: -- Testing fwrite() with file having content of type alphanumeric --
197: -- Opening file in r --
198: int(0)
199: int(0)
200: int(0)
201: bool(false)
202: int(2)
203: int(0)
204: int(2)
205: bool(false)
206: bool(true)
207: int(1024)
208: string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
209: -- Opening file in rb --
210: int(0)
211: int(0)
212: int(0)
213: bool(false)
214: int(2)
215: int(0)
216: int(2)
217: bool(false)
218: bool(true)
219: int(1024)
220: string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
221: -- Opening file in rt --
222: int(0)
223: int(0)
224: int(0)
225: bool(false)
226: int(2)
227: int(0)
228: int(2)
229: bool(false)
230: bool(true)
231: int(1024)
232: string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
233: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>