summaryrefslogtreecommitdiff
path: root/tests/UnitTests/ResourceTests/DefaultHandler/DefaultTemplateHandlerTest.php
blob: f20735075003dda6db0ee2e3c7b4593556b5774c (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
<?php
/**
 * Smarty PHPunit tests deault template handler
 *

 * @author  Uwe Tews
 */

/**
 * class for block plugin tests
 *
 * 
 * 
 * 
 */
class DefaultTemplateHandlerTest extends PHPUnit_Smarty
{
    public function setUp(): void
    {
        $this->setUpSmarty(__DIR__);
        $this->smarty->setForceCompile(true);
        $this->smarty->disableSecurity();
    }


    public function testInit()
    {
        $this->cleanDirs();
    }
    /**
     * test error on unknow template
     */
    public function testUnknownTemplate()
    {
        try {
            $this->smarty->fetch('foo.tpl');
        }
        catch (Exception $e) {
            $this->assertStringContainsString('Unable to load', $e->getMessage());

            return;
        }
        $this->fail('Exception for none existing template has not been raised.');
    }

    /**
     * test error on registration on none existent handler function.
     */
    public function testRegisterNoneExistentHandlerFunction()
    {
        try {
            $this->smarty->registerDefaultTemplateHandler('foo');
        }
        catch (Exception $e) {
            $this->assertStringContainsString("Default template handler", $e->getMessage());
            $this->assertStringContainsString("not callable", $e->getMessage());

            return;
        }
        $this->fail('Exception for none callable function has not been raised.');
    }
    /**
     * test replacement by default template handler
     */
    /**
     * public function testDefaultTemplateHandlerReplacement()
     * {
     * $this->smarty->register->defaultTemplateHandler('my_template_handler');
     * $this->assertEquals("Recsource foo.tpl of type file not found", $this->smarty->fetch('foo.tpl'));
     * }
     */
    public function testDefaultTemplateHandlerReplacementByTemplateFile()
    {
        $this->smarty->registerDefaultTemplateHandler('my_template_handler_file');
        $this->assertEquals("hello world", $this->smarty->fetch('foo.tpl'));
    }

    /**
     * test default template handler returning fals
     */
    public function testDefaultTemplateHandlerReturningFalse()
    {
        $this->smarty->registerDefaultTemplateHandler('my_false');
        try {
            $this->smarty->fetch('foo.tpl');
        }
        catch (Exception $e) {
            $this->assertStringContainsString('Default handler: No template default content for \'file:foo.tpl\'', $e->getMessage());

            return;
        }
        $this->fail('Exception for none existing template has not been raised.');
    }
}

function my_template_handler($resource_type, $resource_name, &$template_source, &$template_timestamp, \Smarty\Smarty $smarty)
{
    $output = "Recsource $resource_name of type $resource_type not found";
    $template_source = $output;
    $template_timestamp = time();

    return true;
}

function my_template_handler_file($resource_type, $resource_name, &$template_source, &$template_timestamp, \Smarty\Smarty $smarty)
{
    return $smarty->getTemplateDir(0) . 'helloworld.tpl';
}

function my_false($resource_type, $resource_name, &$template_source, &$template_timestamp, \Smarty\Smarty $smarty)
{
    return false;
}