summaryrefslogtreecommitdiff
path: root/tests/UnitTests/TemplateSource/ValueTests/Objects/ObjectVariableTest.php
blob: 9be491059a849ef563ad8a7fb2b2e9cc937bd68b (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
<?php
/**
 * Smarty PHPunit tests object variables
 *

 * @author  Uwe Tews
 */

/**
 * class for object variable tests
 *
 * 
 * 
 *
 */
class ObjectVariableTest extends PHPUnit_Smarty
{
    public function setUp(): void
    {
        $this->setUpSmarty(__DIR__);
        $this->smarty->setForceCompile(true);
    }


    public function testInit()
    {
        $this->cleanDirs();
    }
    /**
     * test simple object variable
     */
    public function testObjectVariableOutput()
    {
        $object = new VariableObject;
        $tpl = $this->smarty->createTemplate('string:{$object->hello}');
        $tpl->assign('object', $object);
        $this->assertEquals('hello_world', $this->smarty->fetch($tpl));
    }

    /**
     * test simple object variable with variable property
     */
    public function testObjectVariableOutputVariableProperty()
    {
        $object = new VariableObject;
        $this->smarty->disableSecurity();
        $tpl = $this->smarty->createTemplate('string:{$p=\'hello\'}{$object->$p}');
        $tpl->assign('object', $object);
        $this->assertEquals('hello_world', $this->smarty->fetch($tpl));
    }

    /**
     * test simple object variable with method
     */
    public function testObjectVariableOutputMethod()
    {
        $object = new VariableObject;
        $tpl = $this->smarty->createTemplate('string:{$object->myhello()}');
        $tpl->assign('object', $object);
        $this->assertEquals('hello world', $this->smarty->fetch($tpl));
    }

    /**
     * test simple object variable with method
     */
    public function testObjectVariableOutputVariableMethod()
    {
        $object = new VariableObject;
        $this->smarty->disableSecurity();
        $tpl = $this->smarty->createTemplate('string:{$p=\'myhello\'}{$object->$p()}');
        $tpl->assign('object', $object);
        $this->assertEquals('hello world', $this->smarty->fetch($tpl));
    }

    /**
     * test  object variable in double quoted string
     */
    public function testObjectVariableOutputDoubleQuotes()
    {
        $object = new VariableObject;
        $tpl = $this->smarty->createTemplate('string:{"double quoted `$object->hello` okay"}');
        $tpl->assign('object', $object);
        $this->assertEquals('double quoted hello_world okay', $this->smarty->fetch($tpl));
    }

    /**
     * test  object variable in double quoted string as include name
     */
    public function testObjectVariableOutputDoubleQuotesInclude()
    {
        $object = new VariableObject;
        $tpl = $this->smarty->createTemplate('string:{include file="`$object->hello`_test.tpl"}');
        $tpl->assign('object', $object);
        $this->assertEquals('hello world', $this->smarty->fetch($tpl));
    }
}

Class VariableObject
{
    public $hello = 'hello_world';

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