summaryrefslogtreecommitdiff
path: root/tests/UnitTests/TemplateSource/ValueTests/Variables/Stream/StreamVariableTest.php
blob: 500dcc62de8c4e393fd0db1aa8fd95bf1c41068c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
 * Smarty PHPunit tests stream variables
 *

 * @author  Uwe Tews
 */

/**
 * class for stream variables tests
 *
 * 
 * 
 * 
 */
class StreamVariableTest extends PHPUnit_Smarty
{
    public function setUp(): void
    {
        $this->setUpSmarty(__DIR__);

        stream_wrapper_register("var", "VariableStream")
        or die("Failed to register protocol");
        $fp = fopen("var://foo", "r+");
        fwrite($fp, 'hello world');
        fclose($fp);
    }

    public function testInit()
    {
        $this->cleanDirs();
    }

    protected function tearDown(): void
    {
        parent::tearDown();
        stream_wrapper_unregister("var");
    }

    /**
     * test stream variable
     */
    public function testStreamVariable1()
    {
        $tpl = $this->smarty->createTemplate('eval:{$var:foo}', null, null, $this->smarty);
        $this->assertEquals('hello world', $this->smarty->fetch($tpl));
    }
    /*
        public function testStreamVariable2()
        {
            $tpl = $this->smarty->createTemplate('eval:{var:\'foo\'}', null, null, $this->smarty);
            $this->assertEquals('hello world', $this->smarty->fetch($tpl));
        }

        public function testStreamVariable3()
        {
            $tpl = $this->smarty->createTemplate('eval:{var:"foo"}', null, null, $this->smarty);
            $this->assertEquals('hello world', $this->smarty->fetch($tpl));
        }
    */
    /**
     * test no existent stream variable
     */
    //    public function testStreamVariable2()
    //    {
    //        $tpl = $this->smarty->createTemplate('eval:{$var:bar}', null, null, $this->smarty);
    //        $this->assertEquals('', $this->smarty->fetch($tpl));
    //    }
}

#[AllowDynamicProperties]
class VariableStream
{
    private $position;
    private $varname;

    public function stream_open($path, $mode, $options, &$opened_path)
    {
        $url = parse_url($path);
        $this->varname = $url["host"];
        $this->position = 0;

        return true;
    }

    public function stream_read($count)
    {
        $p = &$this->position;
        $ret = substr($GLOBALS[$this->varname], $p, $count);
        $p += strlen($ret);

        return $ret;
    }

    public function stream_write($data)
    {
        $v = &$GLOBALS[$this->varname];
        $l = strlen($data);
        $p = &$this->position;
        $v = substr($v ?? '', 0, $p) . $data . substr($v ?? '', $p += $l);

        return $l;
    }

    public function stream_tell()
    {
        return $this->position;
    }

    public function stream_eof()
    {
        return $this->position >= strlen($GLOBALS[$this->varname]);
    }

    public function stream_seek($offset, $whence)
    {
        $l = strlen($GLOBALS[$this->varname]);
        $p = &$this->position;
        switch ($whence) {
            case SEEK_SET:
                $newPos = $offset;
                break;
            case SEEK_CUR:
                $newPos = $p + $offset;
                break;
            case SEEK_END:
                $newPos = $l + $offset;
                break;
            default:
                return false;
        }
        $ret = ($newPos >= 0 && $newPos <= $l);
        if ($ret) {
            $p = $newPos;
        }
        return $ret;
    }
}