Completed
Branch 6.0 (d30585)
by yun
14:52
created

TestTemplate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 57
rs 10
wmc 5
1
<?php
2
3
namespace think\tests;
4
5
use Mockery\MockInterface;
6
use PHPUnit\Framework\TestCase;
7
use think\App;
8
use think\Config;
9
use think\Container;
10
use think\contract\TemplateHandlerInterface;
11
use think\View;
12
use Mockery as m;
13
14
class ViewTest extends TestCase
15
{
16
    /** @var App|MockInterface */
17
    protected $app;
18
19
    /** @var View|MockInterface */
20
    protected $view;
21
22
    /** @var Config|MockInterface */
23
    protected $config;
24
25
    protected function tearDown(): void
26
    {
27
        m::close();
28
    }
29
30
    protected function setUp()
31
    {
32
        $this->app = m::mock(App::class)->makePartial();
33
        Container::setInstance($this->app);
34
35
        $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
36
        $this->config = m::mock(Config::class)->makePartial();
37
        $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
38
39
        $this->view = new View($this->app);
40
    }
41
42
    public function testAssignData()
43
    {
44
        $this->view->assign('foo', 'bar');
45
        $this->view->assign(['baz' => 'boom']);
46
        $this->view->qux = "corge";
47
48
        $this->assertEquals('bar', $this->view->foo);
49
        $this->assertEquals('boom', $this->view->baz);
50
        $this->assertEquals('corge', $this->view->qux);
51
        $this->assertTrue(isset($this->view->qux));
52
    }
53
54
    public function testRender()
55
    {
56
        $this->config->shouldReceive("get")->with("view.type", 'php')->andReturn(TestTemplate::class);
57
58
        $this->view->filter(function ($content) {
59
            return $content;
60
        });
61
62
        $this->assertEquals("fetch", $this->view->fetch('foo'));
63
        $this->assertEquals("display", $this->view->display('foo'));
64
    }
65
66
}
67
68
class TestTemplate implements TemplateHandlerInterface
69
{
70
71
    /**
72
     * 检测是否存在模板文件
73
     * @access public
74
     * @param string $template 模板文件或者模板规则
75
     * @return bool
76
     */
77
    public function exists(string $template): bool
78
    {
79
        return true;
80
    }
81
82
    /**
83
     * 渲染模板文件
84
     * @access public
85
     * @param string $template 模板文件
86
     * @param array  $data     模板变量
87
     * @return void
88
     */
89
    public function fetch(string $template, array $data = []): void
90
    {
91
        echo "fetch";
92
    }
93
94
    /**
95
     * 渲染模板内容
96
     * @access public
97
     * @param string $content 模板内容
98
     * @param array  $data    模板变量
99
     * @return void
100
     */
101
    public function display(string $content, array $data = []): void
102
    {
103
        echo "display";
104
    }
105
106
    /**
107
     * 配置模板引擎
108
     * @access private
109
     * @param array $config 参数
110
     * @return void
111
     */
112
    public function config(array $config): void
113
    {
114
        // TODO: Implement config() method.
115
    }
116
117
    /**
118
     * 获取模板引擎配置
119
     * @access public
120
     * @param string $name 参数名
121
     * @return void
122
     */
123
    public function getConfig(string $name)
124
    {
125
        // TODO: Implement getConfig() method.
126
    }
127
}
128