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

Foo::fetchTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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    Haotong Lin <[email protected]>
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 ReflectionClass;
20
use think\Controller;
0 ignored issues
show
Bug introduced by
The type think\Controller 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
use think\Request;
0 ignored issues
show
Bug introduced by
The type think\Request 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...
22
use think\View;
0 ignored issues
show
Bug introduced by
The type think\View 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...
23
24
require_once CORE_PATH . '../../helper.php';
0 ignored issues
show
Bug introduced by
The constant tests\thinkphp\library\think\CORE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
25
26
class Foo extends Controller
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
27
{
28
    public $test = 'test';
29
30
    public function _initialize()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Public method name "Foo::_initialize" must not be prefixed with an underscore
Loading history...
31
    {
32
        $this->test = 'abcd';
33
    }
34
35
    public function assignTest()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
36
    {
37
        $this->assign('abcd', 'dcba');
38
        $this->assign(['key1' => 'value1', 'key2' => 'value2']);
39
    }
40
41
    public function fetchTest()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
42
    {
43
        $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...
44
        return $this->fetch($template, ['name' => 'ThinkPHP']);
45
    }
46
47
    public function displayTest()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
48
    {
49
        $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...
50
        return $this->display($template, ['name' => 'ThinkPHP']);
51
    }
52
    public function test()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
53
    {
54
        $data       = [
55
            'username'   => 'username',
56
            'nickname'   => 'nickname',
57
            'password'   => '123456',
58
            'repassword' => '123456',
59
            'email'      => '[email protected]',
60
            'sex'        => '0',
61
            'age'        => '20',
62
            'code'       => '1234',
63
        ];
64
65
        $validate = [
66
            ['username', 'length:5,15', '用户名长度为5到15个字符'],
67
            ['nickname', 'require', '请填昵称'],
68
            ['password', '[\w-]{6,15}', '密码长度为6到15个字符'],
69
            ['repassword', 'confirm:password', '两次密码不一到致'],
70
            ['email', 'filter:validate_email', '邮箱格式错误'],
71
            ['sex', 'in:0,1', '性别只能为为男或女'],
72
            ['age', 'between:1,80', '年龄只能在10-80之间'],
73
        ];
74
        return $this->validate($data, $validate);
75
    }
76
}
77
78
class Bar extends Controller
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
79
{
80
    public $test = 1;
81
82
    public $beforeActionList = ['action1', 'action2'];
83
84
    public function action1()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
85
    {
86
        $this->test += 2;
87
        return 'action1';
88
    }
89
90
    public function action2()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
91
    {
92
        $this->test += 4;
93
        return 'action2';
94
    }
95
}
96
97
class Baz extends Controller
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
98
{
99
    public $test = 1;
100
101
    public $beforeActionList = [
102
        'action1' => ['only' => 'index'],
103
        'action2' => ['except' => 'index'],
104
        'action3' => ['only' => 'abcd'],
105
        'action4' => ['except' => 'abcd'],
106
    ];
107
108
    public function action1()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
109
    {
110
        $this->test += 2;
111
        return 'action1';
112
    }
113
114
    public function action2()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
115
    {
116
        $this->test += 4;
117
        return 'action2';
118
    }
119
120
    public function action3()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
121
    {
122
        $this->test += 8;
123
        return 'action2';
124
    }
125
126
    public function action4()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
127
    {
128
        $this->test += 16;
129
        return 'action2';
130
    }
131
}
132
133
class controllerTest 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...
134
{
135
    public function testInitialize()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
136
    {
137
        $foo = new Foo(Request::instance());
138
        $this->assertEquals('abcd', $foo->test);
139
    }
140
141
    public function testBeforeAction()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
142
    {
143
        $obj = new Bar(Request::instance());
144
        $this->assertEquals(7, $obj->test);
145
146
        $obj = new Baz(Request::instance());
147
        $this->assertEquals(19, $obj->test);
148
    }
149
150
    private function getView($controller)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "controllerTest::getView" must be prefixed with an underscore
Loading history...
151
    {
152
        $view     = new View();
153
        $rc       = new ReflectionClass(get_class($controller));
154
        $property = $rc->getProperty('view');
155
        $property->setAccessible(true);
156
        $property->setValue($controller, $view);
157
        return $view;
158
    }
159
160
    public function testFetch()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
161
    {
162
        $controller      = new Foo(Request::instance());
163
        $view            = $this->getView($controller);
164
        $template        = APP_PATH . 'views' . DS .'display.html';
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...
165
        $viewFetch       = $view->fetch($template, ['name' => 'ThinkPHP']);
166
        $this->assertEquals($controller->fetchTest(), $viewFetch);
167
    }
168
169
    public function testDisplay()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
170
    {
171
        $controller      = new Foo;
172
        $view            = $this->getView($controller);
173
        $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...
174
        $viewFetch       = $view->display($template, ['name' => 'ThinkPHP']);
175
176
        $this->assertEquals($controller->displayTest(), $viewFetch);
177
    }
178
179
    public function testAssign()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
180
    {
181
        $controller = new Foo(Request::instance());
182
        $view       = $this->getView($controller);
183
        $controller->assignTest();
184
        $expect = ['abcd' => 'dcba', 'key1' => 'value1', 'key2' => 'value2'];
185
        $this->assertAttributeEquals($expect, 'data', $view);
186
    }
187
188
    public function testValidate()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
189
    {
190
        $controller = new Foo(Request::instance());
191
        $result = $controller->test();
192
        $this->assertTrue($result);
193
    }
194
}
195