1--TEST--
2stream_set_chunk_size basic tests
3--FILE--
4<?php
5class test_wrapper {
6    function stream_open($path, $mode, $openedpath) {
7        return true;
8    }
9    function stream_eof() {
10        return false;
11    }
12    function stream_read($count) {
13        echo "read with size: ", $count, "\n";
14        return str_repeat('a', $count);
15    }
16    function stream_write($data) {
17        echo "write with size: ", strlen($data), "\n";
18        return strlen($data);
19    }
20    function stream_set_option($option, $arg1, $arg2) {
21        echo "option: ", $option, ", ", $arg1, ", ", $arg2, "\n";
22        return false;
23    }
24}
25
26var_dump(stream_wrapper_register('test', 'test_wrapper'));
27
28$f = fopen("test://foo","r");
29
30/* when the chunk size is 1, the read buffer is skipped, but the
31 * the writes are made in chunks of size 1 (business as usual)
32 * This should probably be revisited */
33echo "should return previous chunk size (8192)\n";
34var_dump(stream_set_chunk_size($f, 1));
35echo "should be read without buffer (\$count == 10000)\n";
36var_dump(strlen(fread($f, 10000)));
37echo "should have no effect on writes\n";
38var_dump(fwrite($f, str_repeat('b', 3)));
39
40echo "should return previous chunk size (1)\n";
41var_dump(stream_set_chunk_size($f, 100));
42echo "should elicit one read of size 100 (chunk size)\n";
43var_dump(strlen(fread($f, 250)));
44echo "should elicit one read of size 100 (chunk size)\n";
45var_dump(strlen(fread($f, 50)));
46echo "should elicit no read because there is sufficient cached data\n";
47var_dump(strlen(fread($f, 50)));
48echo "should have no effect on writes\n";
49var_dump(strlen(fwrite($f, str_repeat('b', 250))));
50
51echo "\nerror conditions\n";
52try {
53    stream_set_chunk_size($f, 0);
54} catch (ValueError $exception) {
55    echo $exception->getMessage() . "\n";
56}
57try {
58    stream_set_chunk_size($f, -1);
59} catch (ValueError $exception) {
60    echo $exception->getMessage() . "\n";
61}
62?>
63--EXPECT--
64bool(true)
65should return previous chunk size (8192)
66int(8192)
67should be read without buffer ($count == 10000)
68read with size: 10000
69int(10000)
70should have no effect on writes
71write with size: 3
72int(3)
73should return previous chunk size (1)
74int(1)
75should elicit one read of size 100 (chunk size)
76read with size: 100
77int(100)
78should elicit one read of size 100 (chunk size)
79read with size: 100
80int(50)
81should elicit no read because there is sufficient cached data
82int(50)
83should have no effect on writes
84write with size: 250
85int(3)
86
87error conditions
88stream_set_chunk_size(): Argument #2 ($size) must be greater than 0
89stream_set_chunk_size(): Argument #2 ($size) must be greater than 0
90