summaryrefslogtreecommitdiff
path: root/tests/UnitTests/SmartyMethodsTests/RegisterObject/CompileRegisteredObjectFunctionTest.php
blob: 9aca31fd5909fc7afda5c107402f897f7bec5378 (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
<?php
/**
 * Smarty PHPunit tests compilation of registered object functions
 *

 * @author  Uwe Tews
 */

/**
 * class for registered object function tests
 *
 * 
 * 
 * 
 */
class CompileRegisteredObjectFunctionTest extends PHPUnit_Smarty
{
    /**
     * @var RegObject
     */
    private $object;

    public function setUp(): void
    {
        $this->setUpSmarty(__DIR__);

        $this->smarty->setForceCompile(true);
        $this->smarty->disableSecurity();
        $this->object = new RegObject;
        $this->smarty->registerObject('objecttest', $this->object, 'myhello', true, 'myblock');
        $this->smarty->registerObject('objectprop', $this->object);
    }


    public function testInit()
    {
        $this->cleanDirs();
    }
    /**
     * test resgistered object as function
     */
    public function testRegisteredObjectFunction()
    {
        $tpl = $this->smarty->createTemplate('eval:{objecttest->myhello}');
        $this->assertEquals('hello world', $this->smarty->fetch($tpl));
    }

    /**
     * test resgistered object as function with modifier
     */
    public function testRegisteredObjectFunctionModifier()
    {
        $tpl = $this->smarty->createTemplate('eval:{objecttest->myhello|truncate:6}');
        $this->assertEquals('hel...', $this->smarty->fetch($tpl));
    }

    /**
     * test resgistered object as block function
     */
    public function testRegisteredObjectBlockFunction()
    {
        $tpl = $this->smarty->createTemplate('eval:{objecttest->myblock}hello world{/objecttest->myblock}');
        $this->assertEquals('block test', $this->smarty->fetch($tpl));
    }

    public function testRegisteredObjectBlockFunctionModifier1()
    {
        $tpl = $this->smarty->createTemplate('eval:{objecttest->myblock}hello world{/objecttest->myblock|upper}');
        $this->assertEquals(strtoupper('block test'), $this->smarty->fetch($tpl));
    }

    public function testRegisteredObjectBlockFunctionModifier2()
    {
        $tpl = $this->smarty->createTemplate('eval:{objecttest->myblock}hello world{/objecttest->myblock|default:""|upper}');
        $this->assertEquals(strtoupper('block test'), $this->smarty->fetch($tpl));
    }
    // TODO

    /**
    public function testRegisteredObjectProperty()
    {
        $tpl = $this->smarty->createTemplate('eval:{objectprop->prop}');
        $this->assertEquals('hello world', $this->smarty->fetch($tpl));
    }

    public function testRegisteredObjectPropertyAssign()
    {
        $tpl = $this->smarty->createTemplate('eval:{objectprop->prop assign="foo"}{$foo}');
        $this->assertEquals('hello world', $this->smarty->fetch($tpl));
    }
     */
}

Class RegObject
{
    public $prop = 'hello world';

    public function myhello($params)
    {
        return 'hello world';
    }

    public function myblock($params, $content, &$smarty_tpl, &$repeat)
    {
        if (isset($content)) {
            $output = str_replace('hello world', 'block test', $content);

            return $output;
        }
    }
}