1--TEST--
2User-space streams: stream_truncate()
3--FILE--
4<?php
5class test_wrapper_base {
6    public $context;
7    public $mode;
8    function stream_open($path, $mode, $openedpath) {
9        return true;
10    }
11    function stream_eof() {
12        return false;
13    }
14}
15class test_wrapper extends test_wrapper_base {
16    function stream_truncate($new_size) {
17        echo "truncation with new_size=$new_size\n";
18        return true;
19    }
20}
21class test_wrapper_bad extends test_wrapper_base {
22    function stream_truncate($new_size) {
23        echo "truncation with new_size=$new_size\n";
24        return "kkk";
25    }
26}
27function test($name, $fd, $dest_size) {
28    echo "------ $name: -------\n";
29    var_dump(ftruncate($fd, $dest_size));
30}
31var_dump(stream_wrapper_register('test', 'test_wrapper'));
32var_dump(stream_wrapper_register('test2', 'test_wrapper_base'));
33var_dump(stream_wrapper_register('test3', 'test_wrapper_bad'));
34
35$fd = fopen("test://foo","r");
36$fd2 = fopen("test2://foo","r");
37$fd3 = fopen("test3://foo","r");
38
39test("stream_truncate not implemented", $fd2, 0);
40test("stream_truncate size 0", $fd, 0);
41test("stream_truncate size 10", $fd, 10);
42try {
43    test("stream_truncate negative size", $fd, -1);
44} catch (\ValueError $e) {
45    echo $e->getMessage() . \PHP_EOL;
46}
47test("stream_truncate bad return", $fd3, 0);
48?>
49--EXPECTF--
50bool(true)
51bool(true)
52bool(true)
53------ stream_truncate not implemented: -------
54
55Warning: ftruncate(): Can't truncate this stream! in %s on line %d
56bool(false)
57------ stream_truncate size 0: -------
58truncation with new_size=0
59bool(true)
60------ stream_truncate size 10: -------
61truncation with new_size=10
62bool(true)
63------ stream_truncate negative size: -------
64ftruncate(): Argument #2 ($size) must be greater than or equal to 0
65------ stream_truncate bad return: -------
66truncation with new_size=0
67
68Warning: ftruncate(): test_wrapper_bad::stream_truncate did not return a boolean! in %s on line %d
69bool(false)
70