chengkun
2025-09-15 17e42d4e0fa95c7af0173be4ef4768eeb6090d5f
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
<?php
 
namespace think\tests;
 
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;
use think\App;
use think\Config;
use think\Container;
use think\contract\TemplateHandlerInterface;
use think\View;
use Mockery as m;
 
class ViewTest extends TestCase
{
    /** @var App|MockInterface */
    protected $app;
 
    /** @var View|MockInterface */
    protected $view;
 
    /** @var Config|MockInterface */
    protected $config;
 
    protected function tearDown(): void
    {
        m::close();
    }
 
    protected function setUp(): void
    {
        $this->app = m::mock(App::class)->makePartial();
        Container::setInstance($this->app);
 
        $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
        $this->config = m::mock(Config::class)->makePartial();
        $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
 
        $this->view = new View($this->app);
    }
 
    public function testAssignData()
    {
        $this->view->assign('foo', 'bar');
        $this->view->assign(['baz' => 'boom']);
        $this->view->qux = "corge";
 
        $this->assertEquals('bar', $this->view->foo);
        $this->assertEquals('boom', $this->view->baz);
        $this->assertEquals('corge', $this->view->qux);
        $this->assertTrue(isset($this->view->qux));
    }
 
    public function testRender()
    {
        $this->config->shouldReceive("get")->with("view.type", 'php')->andReturn(TestTemplate::class);
 
        $this->view->filter(function ($content) {
            return $content;
        });
 
        $this->assertEquals("fetch", $this->view->fetch('foo'));
        $this->assertEquals("display", $this->view->display('foo'));
    }
 
}
 
class TestTemplate implements TemplateHandlerInterface
{
 
    /**
     * 检测是否存在模板文件
     * @param string $template 模板文件或者模板规则
     * @return bool
     */
    public function exists(string $template): bool
    {
        return true;
    }
 
    /**
     * 渲染模板文件
     * @param string $template 模板文件
     * @param array  $data     模板变量
     * @return void
     */
    public function fetch(string $template, array $data = []): void
    {
        echo "fetch";
    }
 
    /**
     * 渲染模板内容
     * @param string $content 模板内容
     * @param array  $data    模板变量
     * @return void
     */
    public function display(string $content, array $data = []): void
    {
        echo "display";
    }
 
    /**
     * 配置模板引擎
     * @param array $config 参数
     * @return void
     */
    public function config(array $config): void
    {
        // TODO: Implement config() method.
    }
 
    /**
     * 获取模板引擎配置
     * @param string $name 参数名
     * @return mixed
     */
    public function getConfig(string $name)
    {
        // TODO: Implement getConfig() method.
    }
}