Completed
Pull Request — 5.1 (#1327)
by
unknown
05:23
created

templateTest::testDisplay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 52
nc 1
nop 0
dl 0
loc 58
rs 9.0472
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
12
/**
13
 * 模板测试
14
 * @author    oldrind
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 4
Loading history...
15
 */
16
17
namespace tests\thinkphp\library\think;
18
19
use think\Cache;
0 ignored issues
show
Bug introduced by
The type think\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use think\Template;
0 ignored issues
show
Bug introduced by
The type think\Template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
class templateTest extends \PHPUnit_Framework_TestCase
1 ignored issue
show
Bug introduced by
The type PHPUnit_Framework_TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Missing class doc comment
Loading history...
Coding Style introduced by
Class name must begin with a capital letter
Loading history...
23
{
24
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
     * @var Template
26
     */
27
    protected $template;
28
29
    public function setUp()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
30
    {
31
        $this->template = new Template();
32
    }
33
34
    public function testAssign()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
35
    {
36
        $reflectProperty = new \ReflectionProperty(get_class($this->template), 'data');
37
        $reflectProperty->setAccessible(true);
38
39
        $this->template->assign('version', 'ThinkPHP3.2');
40
        $data = $reflectProperty->getValue($this->template);
41
        $this->assertEquals('ThinkPHP3.2', $data['version']);
42
43
        $this->template->assign(['name' => 'Gao', 'version' => 'ThinkPHP5']);
44
        $data = $reflectProperty->getValue($this->template);
45
        $this->assertEquals('Gao', $data['name']);
46
        $this->assertEquals('ThinkPHP5', $data['version']);
47
    }
48
49
    public function testGet()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
50
    {
51
        $this->template = new Template();
52
        $data = [
53
            'project' => 'ThinkPHP',
54
            'version' => [
55
                'ThinkPHP5' => ['Think5.0', 'Think5.1']
56
            ]
57
        ];
58
        $this->template->assign($data);
59
60
        $this->assertSame($data, $this->template->get());
61
        $this->assertSame('ThinkPHP', $this->template->get('project'));
62
        $this->assertSame(['Think5.0', 'Think5.1'], $this->template->get('version.ThinkPHP5'));
63
        $this->assertNull($this->template->get('version.ThinkPHP3.2'));
64
    }
65
66
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $content should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expected should have a doc-comment as per coding-style.
Loading history...
67
     * @dataProvider provideTestParseWithVar
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
    public function testParseWithVar($content, $expected)
70
    {
71
        $this->template = new Template();
72
73
        $this->template->parse($content);
74
        $this->assertEquals($expected, $content);
75
    }
76
77
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $content should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expected should have a doc-comment as per coding-style.
Loading history...
78
     * @dataProvider provideTestParseWithVarFunction
79
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
80
    public function testParseWithVarFunction($content, $expected)
81
    {
82
        $this->template = new Template();
83
84
        $this->template->parse($content);
85
        $this->assertEquals($expected, $content);
86
    }
87
88
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $content should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expected should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
89
     * @dataProvider provideTestParseWithVarIdentify
90
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
91
    public function testParseWithVarIdentify($content, $expected, $config)
92
    {
93
        $this->template = new Template($config);
94
95
        $this->template->parse($content);
96
        $this->assertEquals($expected, $content);
97
    }
98
99
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $content should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expected should have a doc-comment as per coding-style.
Loading history...
100
     * @dataProvider provideTestParseWithThinkVar
101
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
102
    public function testParseWithThinkVar($content, $expected)
103
    {
104
        $config['tpl_begin'] = '{';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $config = array(); before regardless.
Loading history...
105
        $config['tpl_end']   = '}';
106
        $this->template            = new Template($config);
107
108
        $_SERVER['SERVER_NAME'] = 'server_name';
109
        $_GET['action']         = 'action';
110
        $_POST['action']        = 'action';
111
        $_COOKIE['name']        = 'name';
112
        $_SESSION['action']     = ['name' => 'name'];
113
114
        $this->template->parse($content);
115
        $this->assertEquals($expected, $content);
116
    }
117
118
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
119
     * @expectedException \think\exception\TemplateNotFoundException
120
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
121
    public function testFetchWithEmptyTemplate()
122
    {
123
        $this->template = new Template();
124
125
        $this->template->fetch('Foo');
126
    }
127
128
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $data should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expected should have a doc-comment as per coding-style.
Loading history...
129
     * @dataProvider provideTestFetchWithNoCache
130
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
131
    public function testFetchWithNoCache($data, $expected)
132
    {
133
        $this->template = new Template();
134
135
        $this->template->fetch($data['template'], $data['vars'], $data['config']);
136
137
        $this->expectOutputString($expected);
138
    }
139
140
    public function testFetchWithCache()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
141
    {
142
        $this->template = new Template();
143
144
        $data = [
145
            'name' => 'value'
146
        ];
147
        $config = [
148
            'cache_id'      => 'TEST_FETCH_WITH_CACHE',
149
            'display_cache' => true,
150
        ];
151
152
        $this->template->fetch(APP_PATH . 'views' . DS .'display.html', $data, $config);
0 ignored issues
show
Bug introduced by
The constant tests\thinkphp\library\think\APP_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant tests\thinkphp\library\think\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
153
154
        $this->expectOutputString('value');
155
        $this->assertEquals('value', Cache::get($config['cache_id']));
156
    }
157
158
    public function testDisplay()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
159
    {
160
        $config = [
161
            'view_path'   => APP_PATH . DS . 'views' . DS,
0 ignored issues
show
Bug introduced by
The constant tests\thinkphp\library\think\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant tests\thinkphp\library\think\APP_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
162
            'view_suffix' => '.html',
163
            'layout_on'   => true,
164
            'layout_name' => 'layout'
165
        ];
166
167
        $this->template = new Template($config);
168
169
        $this->template->assign('files', ['extend' => 'extend', 'include' => 'include']);
170
        $this->template->assign('user', ['name' => 'name', 'account' => 100]);
171
        $this->template->assign('message', 'message');
172
        $this->template->assign('info', ['value' => 'value']);
173
174
        $content = <<<EOF
175
{extend name="\$files.extend" /}
176
{block name="main"}
177
main
178
{block name="side"}
179
{__BLOCK__}
180
    {include file="\$files.include" name="\$user.name" value="\$user.account" /}
181
    {\$message}{literal}{\$message}{/literal}
182
{/block}
183
{block name="mainbody"}
184
    mainbody
185
{/block}
186
{/block}
187
EOF;
188
        $expected = <<<EOF
189
<nav>
190
header
191
<div id="wrap">
192
    <input name="info" value="value">
193
value:
194
195
main
196
197
198
    side
199
200
    <input name="name" value="100">
201
value:
202
    message{\$message}
203
204
205
    mainbody
206
207
208
209
    {\$name}
210
211
    php code</div>
212
</nav>
213
EOF;
214
        $this->template->display($content);
215
        $this->expectOutputString($expected);
216
    }
217
218
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $data should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expected should have a doc-comment as per coding-style.
Loading history...
219
     * @dataProvider provideTestLayout
220
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
221
    public function testLayout($data, $expected)
222
    {
223
        $this->template = new Template();
224
225
        $this->template->layout($data['name'], $data['replace']);
226
227
        $this->assertSame($expected['layout_on'], $this->template->config('layout_on'));
228
        $this->assertSame($expected['layout_name'], $this->template->config('layout_name'));
229
        $this->assertSame($expected['layout_item'], $this->template->config('layout_item'));
230
    }
231
232
    public function testParseAttr()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
233
    {
234
        $attributes = $this->template->parseAttr("<name version='ThinkPHP' name=\"Gao\"></name>");
235
        $this->assertSame(['version' => 'ThinkPHP', 'name' => 'Gao'], $attributes);
236
237
        $attributes = $this->template->parseAttr("<name version='ThinkPHP' name=\"Gao\">TestCase</name>", 'version');
238
        $this->assertSame('ThinkPHP', $attributes);
239
    }
240
241
    public function testIsCache()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
242
    {
243
        $this->template = new Template();
244
        $config = [
245
            'cache_id'      => rand(0, 10000) . rand(0, 10000) . time(),
246
            'display_cache' => true
247
        ];
248
249
        $this->assertFalse($this->template->isCache($config['cache_id']));
250
251
        $this->template->fetch(APP_PATH . 'views' . DS .'display.html', [], $config);
0 ignored issues
show
Bug introduced by
The constant tests\thinkphp\library\think\APP_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant tests\thinkphp\library\think\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
252
        $this->assertTrue($this->template->isCache($config['cache_id']));
253
    }
254
255
    public function provideTestParseWithVar()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
256
    {
257
        return [
258
            ["{\$name.a.b}", "<?php echo \$name['a']['b']; ?>"],
259
            ["{\$name.a??'test'}", "<?php echo isset(\$name['a'])?\$name['a']:'test'; ?>"],
260
            ["{\$name.a?='test'}", "<?php if(!empty(\$name['a'])) echo 'test'; ?>"],
261
            ["{\$name.a?:'test'}", "<?php echo !empty(\$name['a'])?\$name['a']:'test'; ?>"],
262
            ["{\$name.a?\$name.b:'no'}", "<?php echo !empty(\$name['a'])?\$name['b']:'no'; ?>"],
263
            ["{\$name.a==\$name.b?='test'}", "<?php if(\$name['a']==\$name['b']) echo 'test'; ?>"],
264
            ["{\$name.a==\$name.b?'a':'b'}", "<?php echo \$name['a']==\$name['b']?'a':'b'; ?>"],
265
            ["{\$name.a|default='test'==\$name.b?'a':'b'}", "<?php echo (isset(\$name['a']) && (\$name['a'] !== '')?\$name['a']:'test')==\$name['b']?'a':'b'; ?>"],
266
            ["{\$name.a|trim==\$name.b?='eq'}", "<?php if(trim(\$name['a'])==\$name['b']) echo 'eq'; ?>"],
267
            ["{:ltrim(rtrim(\$name.a))}", "<?php echo ltrim(rtrim(\$name['a'])); ?>"],
268
            ["{~echo(trim(\$name.a))}", "<?php echo(trim(\$name['a'])); ?>"],
269
            ["{++\$name.a}", "<?php echo ++\$name['a']; ?>"],
270
            ["{/*\$name*/}", ""],
271
            ["{\$0a}", "{\$0a}"]
272
        ];
273
    }
274
275
    public function provideTestParseWithVarFunction()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
276
    {
277
        return [
278
            ["{\$name.a.b|default='test'}", "<?php echo (isset(\$name['a']['b']) && (\$name['a']['b'] !== '')?\$name['a']['b']:'test'); ?>"],
279
            ["{\$create_time|date=\"y-m-d\",###}", "<?php echo date(\"y-m-d\",\$create_time); ?>"],
280
            ["{\$name}\n{\$name|trim|substr=0,3}", "<?php echo \$name; ?>\n<?php echo substr(trim(\$name),0,3); ?>"]
281
        ];
282
    }
283
284
    public function provideTestParseWithVarIdentify()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
285
    {
286
        $config['tpl_begin']        = '<#';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $config = array(); before regardless.
Loading history...
287
        $config['tpl_end']          = '#>';
288
        $config['tpl_var_identify'] = '';
289
290
        return [
291
            [
292
                "<#\$info.a??'test'#>",
293
                "<?php echo ((is_array(\$info)?\$info['a']:\$info->a)) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?>",
294
                $config
295
            ],
296
            [
297
                "<#\$info.a?='test'#>",
298
                "<?php if((is_array(\$info)?\$info['a']:\$info->a)) echo 'test'; ?>",
299
                $config
300
            ],
301
            [
302
                "<#\$info.a==\$info.b?='test'#>",
303
                "<?php if((is_array(\$info)?\$info['a']:\$info->a)==(is_array(\$info)?\$info['b']:\$info->b)) echo 'test'; ?>",
304
                $config
305
            ],
306
            [
307
                "<#\$info.a|default='test'?'yes':'no'#>",
308
                "<?php echo ((is_array(\$info)?\$info['a']:\$info->a) ?: 'test')?'yes':'no'; ?>",
309
                $config
310
            ],
311
            [
312
                "{\$info2.b|trim?'yes':'no'}",
313
                "<?php echo trim(\$info2->b)?'yes':'no'; ?>",
314
                array_merge(['tpl_var_identify' => 'obj'])
315
            ]
316
        ];
317
    }
318
319
    public function provideTestParseWithThinkVar()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
320
    {
321
        return [
322
            ["{\$Think.SERVER.SERVER_NAME}<br/>", "<?php echo \\think\\Request::instance()->server('SERVER_NAME'); ?><br/>"],
323
            ["{\$Think.GET.action}<br/>", "<?php echo \\think\\Request::instance()->get('action'); ?><br/>"],
324
            ["{\$Think.POST.action}<br/>", "<?php echo \\think\\Request::instance()->post('action'); ?><br/>"],
325
            ["{\$Think.COOKIE.action}<br/>", "<?php echo \\think\\Cookie::get('action'); ?><br/>"],
326
            ["{\$Think.COOKIE.action.name}<br/>", "<?php echo \\think\\Cookie::get('action.name'); ?><br/>"],
327
            ["{\$Think.SESSION.action}<br/>", "<?php echo \\think\\Session::get('action'); ?><br/>"],
328
            ["{\$Think.SESSION.action.name}<br/>", "<?php echo \\think\\Session::get('action.name'); ?><br/>"],
329
            ["{\$Think.ENV.OS}<br/>", "<?php echo \\think\\Request::instance()->env('OS'); ?><br/>"],
330
            ["{\$Think.REQUEST.action}<br/>", "<?php echo \\think\\Request::instance()->request('action'); ?><br/>"],
331
            ["{\$Think.CONST.THINK_VERSION}<br/>", "<?php echo THINK_VERSION; ?><br/>"],
332
            ["{\$Think.LANG.action}<br/>", "<?php echo \\think\\Lang::get('action'); ?><br/>"],
333
            ["{\$Think.CONFIG.action.name}<br/>", "<?php echo \\think\\Config::get('action.name'); ?><br/>"],
334
            ["{\$Think.NOW}<br/>", "<?php echo date('Y-m-d g:i a',time()); ?><br/>"],
335
            ["{\$Think.VERSION}<br/>", "<?php echo THINK_VERSION; ?><br/>"],
336
            ["{\$Think.LDELIM}<br/>", "<?php echo '{'; ?><br/>"],
337
            ["{\$Think.RDELIM}<br/>", "<?php echo '}'; ?><br/>"],
338
            ["{\$Think.THINK_VERSION}<br/>", "<?php echo THINK_VERSION; ?><br/>"],
339
            ["{\$Think.SITE.URL}", "<?php echo ''; ?>"]
340
        ];
341
    }
342
343
    public function provideTestFetchWithNoCache()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
344
    {
345
        $provideData = [];
346
347
        $this->template = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('template' => test...), 'config' => array()) of type array<string,array|string> is incompatible with the declared type think\Template of property $template.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
348
            'template' => APP_PATH . 'views' . DS .'display.html',
0 ignored issues
show
Bug introduced by
The constant tests\thinkphp\library\think\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant tests\thinkphp\library\think\APP_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
349
            'vars'     => [],
350
            'config'   => []
351
        ];
352
        $expected = 'default';
353
        $provideData[] = [$this->template, $expected];
354
355
        $this->template = [
356
            'template' => APP_PATH . 'views' . DS .'display.html',
357
            'vars'     => ['name' => 'ThinkPHP5'],
358
            'config'   => []
359
        ];
360
        $expected = 'ThinkPHP5';
361
        $provideData[] = [$this->template, $expected];
362
363
        $this->template = [
364
            'template' => 'views@display',
365
            'vars'     => [],
366
            'config'   => [
367
                'view_suffix' => 'html'
368
            ]
369
        ];
370
        $expected = 'default';
371
        $provideData[] = [$this->template, $expected];
372
373
        $this->template = [
374
            'template' => 'views@/display',
375
            'vars'     => ['name' => 'ThinkPHP5'],
376
            'config'   => [
377
                'view_suffix' => 'phtml'
378
            ]
379
        ];
380
        $expected = 'ThinkPHP5';
381
        $provideData[] = [$this->template, $expected];
382
383
        $this->template = [
384
            'template' => 'display',
385
            'vars'     => ['name' => 'ThinkPHP5'],
386
            'config'   => [
387
                'view_suffix' => 'html',
388
                'view_base'   => APP_PATH . 'views' . DS
389
            ]
390
        ];
391
        $expected = 'ThinkPHP5';
392
        $provideData[] = [$this->template, $expected];
393
394
        return $provideData;
395
    }
396
397
    public function provideTestLayout()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
398
    {
399
        $provideData = [];
400
401
        $data = ['name' => false, 'replace' => ''];
402
        $expected = ['layout_on' => false, 'layout_name' => 'layout', 'layout_item' => '{__CONTENT__}'];
403
        $provideData[] = [$data, $expected];
404
405
        $data = ['name' => null, 'replace' => ''];
406
        $expected = ['layout_on' => true, 'layout_name' => 'layout', 'layout_item' => '{__CONTENT__}'];
407
        $provideData[] = [$data, $expected];
408
409
        $data = ['name' => 'ThinkName', 'replace' => 'ThinkReplace'];
410
        $expected = ['layout_on' => true, 'layout_name' => 'ThinkName', 'layout_item' => 'ThinkReplace'];
411
        $provideData[] = [$data, $expected];
412
413
        return $provideData;
414
    }
415
}
416