|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
|
|
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
|
|
|
* Debug测试 |
|
14
|
|
|
* @author 大漠 <[email protected]> |
|
|
|
|
|
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace tests\thinkphp\library\think; |
|
18
|
|
|
|
|
19
|
|
|
use tests\thinkphp\library\think\config\ConfigInitTrait; |
|
20
|
|
|
use think\Config; |
|
|
|
|
|
|
21
|
|
|
use think\Debug; |
|
|
|
|
|
|
22
|
|
|
use think\Response; |
|
|
|
|
|
|
23
|
|
|
use think\response\Redirect; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class debugTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
use ConfigInitTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
|
|
|
|
|
30
|
|
|
* |
|
31
|
|
|
* @var Debug |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $object; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
37
|
|
|
* This method is called before a test is executed. |
|
38
|
|
|
*/ |
|
|
|
|
|
|
39
|
|
|
protected function setUp() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->object = new Debug(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Tears down the fixture, for example, closes a network connection. |
|
46
|
|
|
* This method is called after a test is executed. |
|
47
|
|
|
*/ |
|
|
|
|
|
|
48
|
|
|
protected function tearDown() |
|
49
|
|
|
{} |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
|
|
|
|
|
52
|
|
|
* @covers \think\Debug::remark |
|
53
|
|
|
* @todo Implement testRemark(). |
|
54
|
|
|
*/ |
|
|
|
|
|
|
55
|
|
|
public function testRemark() |
|
56
|
|
|
{ |
|
57
|
|
|
$name = "testremarkkey"; |
|
58
|
|
|
Debug::remark($name); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
|
|
|
|
|
62
|
|
|
* @covers \think\Debug::getRangeTime |
|
63
|
|
|
* @todo Implement testGetRangeTime(). |
|
64
|
|
|
*/ |
|
|
|
|
|
|
65
|
|
|
public function testGetRangeTime() |
|
66
|
|
|
{ |
|
67
|
|
|
$start = "testGetRangeTimeStart"; |
|
68
|
|
|
$end = "testGetRangeTimeEnd"; |
|
69
|
|
|
Debug::remark($start); |
|
70
|
|
|
usleep(20000); |
|
71
|
|
|
// \think\Debug::remark($end); |
|
72
|
|
|
|
|
73
|
|
|
$time = Debug::getRangeTime($start, $end); |
|
74
|
|
|
$this->assertLessThan(0.03, $time); |
|
75
|
|
|
//$this->assertEquals(0.03, ceil($time)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
|
|
|
|
|
79
|
|
|
* @covers \think\Debug::getUseTime |
|
80
|
|
|
* @todo Implement testGetUseTime(). |
|
81
|
|
|
*/ |
|
|
|
|
|
|
82
|
|
|
public function testGetUseTime() |
|
83
|
|
|
{ |
|
84
|
|
|
$time = Debug::getUseTime(); |
|
85
|
|
|
$this->assertLessThan(30, $time); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
|
|
|
|
|
89
|
|
|
* @covers \think\Debug::getThroughputRate |
|
90
|
|
|
* @todo Implement testGetThroughputRate(). |
|
91
|
|
|
*/ |
|
|
|
|
|
|
92
|
|
|
public function testGetThroughputRate() |
|
93
|
|
|
{ |
|
94
|
|
|
usleep(100000); |
|
95
|
|
|
$throughputRate = Debug::getThroughputRate(); |
|
96
|
|
|
$this->assertLessThan(10, $throughputRate); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
|
|
|
|
|
100
|
|
|
* @covers \think\Debug::getRangeMem |
|
101
|
|
|
* @todo Implement testGetRangeMem(). |
|
102
|
|
|
*/ |
|
|
|
|
|
|
103
|
|
|
public function testGetRangeMem() |
|
104
|
|
|
{ |
|
105
|
|
|
$start = "testGetRangeMemStart"; |
|
106
|
|
|
$end = "testGetRangeMemEnd"; |
|
107
|
|
|
Debug::remark($start); |
|
108
|
|
|
$str = ""; |
|
109
|
|
|
for ($i = 0; $i < 10000; $i++) { |
|
110
|
|
|
$str .= "mem"; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$rangeMem = Debug::getRangeMem($start, $end); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertLessThan(33, explode(" ", $rangeMem)[0]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
|
|
|
|
|
119
|
|
|
* @covers \think\Debug::getUseMem |
|
120
|
|
|
* @todo Implement testGetUseMem(). |
|
121
|
|
|
*/ |
|
|
|
|
|
|
122
|
|
|
public function testGetUseMem() |
|
123
|
|
|
{ |
|
124
|
|
|
$useMem = Debug::getUseMem(); |
|
125
|
|
|
|
|
126
|
|
|
$this->assertLessThan(35, explode(" ", $useMem)[0]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
|
|
|
|
|
130
|
|
|
* @covers \think\Debug::getMemPeak |
|
131
|
|
|
* @todo Implement testGetMemPeak(). |
|
132
|
|
|
*/ |
|
|
|
|
|
|
133
|
|
|
public function testGetMemPeak() |
|
134
|
|
|
{ |
|
135
|
|
|
$start = "testGetMemPeakStart"; |
|
136
|
|
|
$end = "testGetMemPeakEnd"; |
|
137
|
|
|
Debug::remark($start); |
|
138
|
|
|
$str = ""; |
|
139
|
|
|
for ($i = 0; $i < 100000; $i++) { |
|
140
|
|
|
$str .= "mem"; |
|
141
|
|
|
} |
|
142
|
|
|
$memPeak = Debug::getMemPeak($start, $end); |
|
143
|
|
|
$this->assertLessThan(500, explode(" ", $memPeak)[0]); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
|
|
|
|
|
147
|
|
|
* @covers \think\Debug::getFile |
|
148
|
|
|
* @todo Implement testGetFile(). |
|
149
|
|
|
*/ |
|
|
|
|
|
|
150
|
|
|
public function testGetFile() |
|
151
|
|
|
{ |
|
152
|
|
|
$count = Debug::getFile(); |
|
153
|
|
|
|
|
154
|
|
|
$this->assertEquals(count(get_included_files()), $count); |
|
155
|
|
|
|
|
156
|
|
|
$info = Debug::getFile(true); |
|
157
|
|
|
$this->assertEquals(count(get_included_files()), count($info)); |
|
158
|
|
|
|
|
159
|
|
|
$this->assertContains("KB", $info[0]); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
|
|
|
|
|
163
|
|
|
* @covers \think\Debug::dump |
|
164
|
|
|
* @todo Implement testDump(). |
|
165
|
|
|
*/ |
|
|
|
|
|
|
166
|
|
|
public function testDump() |
|
167
|
|
|
{ |
|
168
|
|
|
if (strstr(PHP_VERSION, 'hhvm')) { |
|
169
|
|
|
return; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$var = []; |
|
173
|
|
|
$var["key"] = "val"; |
|
174
|
|
|
$output = Debug::dump($var, false, $label = "label"); |
|
175
|
|
|
$array = explode("array", json_encode($output)); |
|
176
|
|
|
if (IS_WIN) { |
|
|
|
|
|
|
177
|
|
|
$this->assertEquals("(1) {\\n [\\\"key\\\"] => string(3) \\\"val\\\"\\n}\\n\\r\\n\"", end($array)); |
|
178
|
|
|
} elseif (strstr(PHP_OS, 'Darwin')) { |
|
179
|
|
|
$this->assertEquals("(1) {\\n [\\\"key\\\"] => string(3) \\\"val\\\"\\n}\\n\\n\"", end($array)); |
|
180
|
|
|
} else { |
|
181
|
|
|
$this->assertEquals("(1) {\\n 'key' =>\\n string(3) \\\"val\\\"\\n}\\n\\n\"", end($array)); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
|
|
|
|
|
186
|
|
|
* @expectedException \think\exception\ClassNotFoundException |
|
187
|
|
|
*/ |
|
|
|
|
|
|
188
|
|
|
public function testInjectWithErrorType() |
|
189
|
|
|
{ |
|
190
|
|
|
Config::set('trace', ['type' => 'NullDebug']); |
|
191
|
|
|
|
|
192
|
|
|
$response = new Response(); |
|
193
|
|
|
$context = 'TestWithErrorType'; |
|
194
|
|
|
|
|
195
|
|
|
Debug::inject($response, $context); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function testInject() |
|
|
|
|
|
|
199
|
|
|
{ |
|
200
|
|
|
Config::set('trace', ['type' => 'Console']); |
|
201
|
|
|
|
|
202
|
|
|
$response = new Response(); |
|
203
|
|
|
$context = 'TestWithoutBodyTag'; |
|
204
|
|
|
Debug::inject($response, $context); |
|
205
|
|
|
$this->assertNotEquals('TestWithoutBodyTag', $context); |
|
206
|
|
|
$this->assertStringStartsWith('TestWithoutBodyTag', $context); |
|
207
|
|
|
|
|
208
|
|
|
$response = new Response(); |
|
209
|
|
|
$context = '<body></body>'; |
|
210
|
|
|
Debug::inject($response, $context); |
|
211
|
|
|
$this->assertNotEquals('<body></body>', $context); |
|
212
|
|
|
$this->assertStringStartsWith('<body>', $context); |
|
213
|
|
|
$this->assertStringEndsWith('</body>', $context); |
|
214
|
|
|
|
|
215
|
|
|
$response = new Redirect(); |
|
216
|
|
|
$context = '<body></body>'; |
|
217
|
|
|
Debug::inject($response, $context); |
|
218
|
|
|
$this->assertEquals('<body></body>', $context); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|