@@ -14,380 +14,380 @@ |
||
14 | 14 | class Display |
15 | 15 | { |
16 | 16 | |
17 | - /** @var array */ |
|
18 | - protected $defaults = array( |
|
19 | - 'script_path' => 'asset/script.js', |
|
20 | - 'style_path' => 'asset/style.css' |
|
21 | - ); |
|
22 | - |
|
23 | - /** @var array */ |
|
24 | - protected $options; |
|
25 | - |
|
26 | - /** @var double */ |
|
27 | - protected $startTime; |
|
28 | - |
|
29 | - /** @var Console */ |
|
30 | - protected $console; |
|
31 | - |
|
32 | - /** @var array */ |
|
33 | - protected $speedData; |
|
34 | - |
|
35 | - /** @var array */ |
|
36 | - protected $queryData; |
|
37 | - |
|
38 | - /** @var array */ |
|
39 | - protected $memoryData; |
|
40 | - |
|
41 | - /** @var array */ |
|
42 | - protected $fileData; |
|
43 | - |
|
44 | - /** |
|
45 | - * @param array $options |
|
46 | - */ |
|
47 | - public function __construct(array $options = array()) |
|
48 | - { |
|
49 | - $options = array_intersect_key($options, $this->defaults); |
|
50 | - $this->options = array_replace($this->defaults, $options); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @param double $startTime |
|
55 | - */ |
|
56 | - public function setStartTime($startTime) |
|
57 | - { |
|
58 | - $this->startTime = $startTime; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @param Console $console |
|
63 | - */ |
|
64 | - public function setConsole(Console $console) |
|
65 | - { |
|
66 | - $this->console = $console; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Sets memory data |
|
71 | - * |
|
72 | - * @param array $data |
|
73 | - */ |
|
74 | - public function setMemoryData(array $data) |
|
75 | - { |
|
76 | - $this->memoryData = $data; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Sets query data |
|
81 | - * |
|
82 | - * @param array $data |
|
83 | - */ |
|
84 | - public function setQueryData(array $data) |
|
85 | - { |
|
86 | - $this->queryData = $data; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Sets speed data |
|
91 | - * |
|
92 | - * @param array $data |
|
93 | - */ |
|
94 | - public function setSpeedData(array $data) |
|
95 | - { |
|
96 | - $this->speedData = $data; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Sets file data |
|
101 | - * |
|
102 | - * @param array $data |
|
103 | - */ |
|
104 | - public function setFileData(array $data) |
|
105 | - { |
|
106 | - $this->fileData = $data; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @return array |
|
111 | - */ |
|
112 | - protected function getConsoleMeta() |
|
113 | - { |
|
114 | - $consoleMeta = array( |
|
115 | - 'log' => 0, |
|
116 | - 'memory' => 0, |
|
117 | - 'error' => 0, |
|
118 | - 'speed' => 0 |
|
119 | - ); |
|
120 | - foreach ($this->console->getLogs() as $log) { |
|
121 | - if (array_key_exists($log['type'], $consoleMeta)) { |
|
122 | - $consoleMeta[$log['type']]++; |
|
123 | - continue; |
|
124 | - } |
|
125 | - $consoleMeta['error']++; |
|
126 | - } |
|
127 | - |
|
128 | - return $consoleMeta; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * @return array |
|
133 | - */ |
|
134 | - protected function getConsoleMessages() |
|
135 | - { |
|
136 | - $messages = array(); |
|
137 | - foreach ($this->console->getLogs() as $log) { |
|
138 | - switch ($log['type']) { |
|
139 | - case 'log': |
|
140 | - $message = array( |
|
141 | - 'message' => print_r($log['data'], true), |
|
142 | - 'type' => 'log' |
|
143 | - ); |
|
144 | - break; |
|
145 | - case 'memory': |
|
146 | - $message = array( |
|
147 | - 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
148 | - 'data' => $this->getReadableMemory($log['data']), |
|
149 | - 'type' => 'memory' |
|
150 | - ); |
|
151 | - break; |
|
152 | - case 'error': |
|
153 | - $message = array( |
|
154 | - 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
155 | - 'type' => 'error' |
|
156 | - ); |
|
157 | - break; |
|
158 | - case 'speed': |
|
159 | - $elapsedTime = $log['data'] - $this->startTime; |
|
160 | - $message = array( |
|
161 | - 'message' => $log['name'], |
|
162 | - 'data' => $this->getReadableTime($elapsedTime), |
|
163 | - 'type' => 'speed' |
|
164 | - ); |
|
165 | - break; |
|
166 | - default: |
|
167 | - $message = array( |
|
168 | - 'message' => "Unrecognized console log type: {$log['type']}", |
|
169 | - 'type' => 'error' |
|
170 | - ); |
|
171 | - break; |
|
172 | - } |
|
173 | - array_push($messages, $message); |
|
174 | - } |
|
175 | - return $messages; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @return array |
|
180 | - */ |
|
181 | - protected function getSpeedMeta() |
|
182 | - { |
|
183 | - $elapsedTime = $this->getReadableTime($this->speedData['elapsed']); |
|
184 | - $allowedTime = $this->getReadableTime($this->speedData['allowed'], 0); |
|
185 | - |
|
186 | - return array( |
|
187 | - 'elapsed' => $elapsedTime, |
|
188 | - 'allowed' => $allowedTime, |
|
189 | - ); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * @return array |
|
194 | - */ |
|
195 | - public function getQueryMeta() |
|
196 | - { |
|
197 | - $queryCount = count($this->queryData); |
|
198 | - $queryTotalTime = array_reduce($this->queryData, function ($sum, $row) { |
|
199 | - return $sum + $row['time']; |
|
200 | - }, 0); |
|
201 | - $queryTotalTime = $this->getReadableTime($queryTotalTime); |
|
202 | - $querySlowestTime = array_reduce($this->queryData, function ($slowest, $row) { |
|
203 | - return ($slowest < $row['time']) ? $row['time'] : $slowest; |
|
204 | - }, 0); |
|
205 | - $querySlowestTime = $this->getReadableTime($querySlowestTime); |
|
206 | - |
|
207 | - return array( |
|
208 | - 'count' => $queryCount, |
|
209 | - 'time' => $queryTotalTime, |
|
210 | - 'slowest' => $querySlowestTime |
|
211 | - ); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * @return array |
|
216 | - */ |
|
217 | - public function getQueryList() |
|
218 | - { |
|
219 | - $queryList = array(); |
|
220 | - foreach ($this->queryData as $query) { |
|
221 | - array_push($queryList, array( |
|
222 | - 'message' => $query['sql'], |
|
223 | - 'sub_data' => array_filter($query['explain']), |
|
224 | - 'data' => $this->getReadableTime($query['time']) |
|
225 | - )); |
|
226 | - } |
|
227 | - return $queryList; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * @return array |
|
232 | - */ |
|
233 | - public function getMemoryMeta() |
|
234 | - { |
|
235 | - $usedMemory = $this->getReadableMemory($this->memoryData['used']); |
|
236 | - $allowedMemory = $this->memoryData['allowed']; // todo parse this, maybe? |
|
237 | - |
|
238 | - return array( |
|
239 | - 'used' => $usedMemory, |
|
240 | - 'allowed' => $allowedMemory |
|
241 | - ); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @return array |
|
246 | - */ |
|
247 | - protected function getFileMeta() |
|
248 | - { |
|
249 | - $fileCount = count($this->fileData); |
|
250 | - $fileTotalSize = array_reduce($this->fileData, function ($sum, $row) { |
|
251 | - return $sum + $row['size']; |
|
252 | - }, 0); |
|
253 | - $fileTotalSize = $this->getReadableMemory($fileTotalSize); |
|
254 | - $fileLargestSize = array_reduce($this->fileData, function ($largest, $row) { |
|
255 | - return ($largest < $row['size']) ? $row['size'] : $largest; |
|
256 | - }, 0); |
|
257 | - $fileLargestSize = $this->getReadableMemory($fileLargestSize); |
|
258 | - |
|
259 | - return array( |
|
260 | - 'count' => $fileCount, |
|
261 | - 'size' => $fileTotalSize, |
|
262 | - 'largest' => $fileLargestSize |
|
263 | - ); |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * @return array |
|
268 | - */ |
|
269 | - protected function getFileList() |
|
270 | - { |
|
271 | - $fileList = array(); |
|
272 | - foreach ($this->fileData as $file) { |
|
273 | - array_push($fileList, array( |
|
274 | - 'message' => $file['name'], |
|
275 | - 'data' => $this->getReadableMemory($file['size']) |
|
276 | - )); |
|
277 | - } |
|
278 | - return $fileList; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * Formatter for human-readable time |
|
283 | - * Only handles time up to 60 minutes gracefully |
|
284 | - * |
|
285 | - * @param double $time |
|
286 | - * @param integer $percision |
|
287 | - * @return string |
|
288 | - */ |
|
289 | - protected function getReadableTime($time, $percision = 3) |
|
290 | - { |
|
291 | - $unit = 's'; |
|
292 | - if ($time < 1) { |
|
293 | - $time *= 1000; |
|
294 | - $percision = 0; |
|
295 | - $unit = 'ms'; |
|
296 | - } elseif ($time > 60) { |
|
297 | - $time /= 60; |
|
298 | - $unit = 'm'; |
|
299 | - } |
|
300 | - $time = number_format($time, $percision); |
|
301 | - return "{$time} {$unit}"; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Formatter for human-readable memory |
|
306 | - * Only handles time up to a few gigs gracefully |
|
307 | - * |
|
308 | - * @param double $size |
|
309 | - * @param integer $percision |
|
310 | - */ |
|
311 | - protected function getReadableMemory($size, $percision = 2) |
|
312 | - { |
|
313 | - $unitOptions = array('b', 'k', 'M', 'G'); |
|
314 | - |
|
315 | - $base = log($size, 1024); |
|
316 | - |
|
317 | - $memory = round(pow(1024, $base - floor($base)), $percision); |
|
318 | - $unit = $unitOptions[floor($base)]; |
|
319 | - return "{$memory} {$unit}"; |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * @param array $messages |
|
324 | - * @param string $type |
|
325 | - * @return array |
|
326 | - */ |
|
327 | - protected function filterMessages($messages, $type) |
|
328 | - { |
|
329 | - return array_filter($messages, function ($message) use ($type) { |
|
330 | - return $message['type'] == $type; |
|
331 | - }); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @returns array |
|
336 | - */ |
|
337 | - protected function gatherTemplateData() |
|
338 | - { |
|
339 | - $consoleMeta = $this->getConsoleMeta(); |
|
340 | - $speedMeta = $this->getSpeedMeta(); |
|
341 | - $queryMeta = $this->getQueryMeta(); |
|
342 | - $memoryMeta = $this->getMemoryMeta(); |
|
343 | - $fileMeta = $this->getFileMeta(); |
|
344 | - |
|
345 | - $consoleMessages = $this->getConsoleMessages(); |
|
346 | - $queryList = $this->getQueryList(); |
|
347 | - $fileList = $this->getFileList(); |
|
348 | - |
|
349 | - return array( |
|
350 | - 'header' => array( |
|
351 | - 'console' => array_sum($consoleMeta), |
|
352 | - 'speed' => $speedMeta['elapsed'], |
|
353 | - 'query' => $queryMeta['count'], |
|
354 | - 'memory' => $memoryMeta['used'], |
|
355 | - 'files' => $fileMeta['count'] |
|
356 | - ), |
|
357 | - 'console' => array( |
|
358 | - 'meta' => $consoleMeta, |
|
359 | - 'messages' => $consoleMessages |
|
360 | - ), |
|
361 | - 'speed' => array( |
|
362 | - 'meta' => $speedMeta, |
|
363 | - 'messages' => $this->filterMessages($consoleMessages, 'speed') |
|
364 | - ), |
|
365 | - 'query' => array( |
|
366 | - 'meta' => $queryMeta, |
|
367 | - 'messages' => $queryList |
|
368 | - ), |
|
369 | - 'memory' => array( |
|
370 | - 'meta' => $memoryMeta, |
|
371 | - 'messages' => $this->filterMessages($consoleMessages, 'memory') |
|
372 | - ), |
|
373 | - 'files' => array( |
|
374 | - 'meta' => $fileMeta, |
|
375 | - 'messages' => $fileList |
|
376 | - ) |
|
377 | - ); |
|
378 | - } |
|
379 | - |
|
380 | - public function __invoke() |
|
381 | - { |
|
382 | - $templateData = $this->gatherTemplateData(); |
|
383 | - |
|
384 | - // todo is this really the best way to load these? |
|
385 | - $styles = file_get_contents(__DIR__ . "/../{$this->options['style_path']}"); |
|
386 | - $script = file_get_contents(__DIR__ . "/../{$this->options['script_path']}"); |
|
387 | - |
|
388 | - call_user_func(function () use ($templateData, $styles, $script) { |
|
389 | - extract($templateData); |
|
390 | - require_once __DIR__ . '/../asset/display.html'; |
|
391 | - }); |
|
392 | - } |
|
17 | + /** @var array */ |
|
18 | + protected $defaults = array( |
|
19 | + 'script_path' => 'asset/script.js', |
|
20 | + 'style_path' => 'asset/style.css' |
|
21 | + ); |
|
22 | + |
|
23 | + /** @var array */ |
|
24 | + protected $options; |
|
25 | + |
|
26 | + /** @var double */ |
|
27 | + protected $startTime; |
|
28 | + |
|
29 | + /** @var Console */ |
|
30 | + protected $console; |
|
31 | + |
|
32 | + /** @var array */ |
|
33 | + protected $speedData; |
|
34 | + |
|
35 | + /** @var array */ |
|
36 | + protected $queryData; |
|
37 | + |
|
38 | + /** @var array */ |
|
39 | + protected $memoryData; |
|
40 | + |
|
41 | + /** @var array */ |
|
42 | + protected $fileData; |
|
43 | + |
|
44 | + /** |
|
45 | + * @param array $options |
|
46 | + */ |
|
47 | + public function __construct(array $options = array()) |
|
48 | + { |
|
49 | + $options = array_intersect_key($options, $this->defaults); |
|
50 | + $this->options = array_replace($this->defaults, $options); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @param double $startTime |
|
55 | + */ |
|
56 | + public function setStartTime($startTime) |
|
57 | + { |
|
58 | + $this->startTime = $startTime; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @param Console $console |
|
63 | + */ |
|
64 | + public function setConsole(Console $console) |
|
65 | + { |
|
66 | + $this->console = $console; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Sets memory data |
|
71 | + * |
|
72 | + * @param array $data |
|
73 | + */ |
|
74 | + public function setMemoryData(array $data) |
|
75 | + { |
|
76 | + $this->memoryData = $data; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Sets query data |
|
81 | + * |
|
82 | + * @param array $data |
|
83 | + */ |
|
84 | + public function setQueryData(array $data) |
|
85 | + { |
|
86 | + $this->queryData = $data; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Sets speed data |
|
91 | + * |
|
92 | + * @param array $data |
|
93 | + */ |
|
94 | + public function setSpeedData(array $data) |
|
95 | + { |
|
96 | + $this->speedData = $data; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Sets file data |
|
101 | + * |
|
102 | + * @param array $data |
|
103 | + */ |
|
104 | + public function setFileData(array $data) |
|
105 | + { |
|
106 | + $this->fileData = $data; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @return array |
|
111 | + */ |
|
112 | + protected function getConsoleMeta() |
|
113 | + { |
|
114 | + $consoleMeta = array( |
|
115 | + 'log' => 0, |
|
116 | + 'memory' => 0, |
|
117 | + 'error' => 0, |
|
118 | + 'speed' => 0 |
|
119 | + ); |
|
120 | + foreach ($this->console->getLogs() as $log) { |
|
121 | + if (array_key_exists($log['type'], $consoleMeta)) { |
|
122 | + $consoleMeta[$log['type']]++; |
|
123 | + continue; |
|
124 | + } |
|
125 | + $consoleMeta['error']++; |
|
126 | + } |
|
127 | + |
|
128 | + return $consoleMeta; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * @return array |
|
133 | + */ |
|
134 | + protected function getConsoleMessages() |
|
135 | + { |
|
136 | + $messages = array(); |
|
137 | + foreach ($this->console->getLogs() as $log) { |
|
138 | + switch ($log['type']) { |
|
139 | + case 'log': |
|
140 | + $message = array( |
|
141 | + 'message' => print_r($log['data'], true), |
|
142 | + 'type' => 'log' |
|
143 | + ); |
|
144 | + break; |
|
145 | + case 'memory': |
|
146 | + $message = array( |
|
147 | + 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
148 | + 'data' => $this->getReadableMemory($log['data']), |
|
149 | + 'type' => 'memory' |
|
150 | + ); |
|
151 | + break; |
|
152 | + case 'error': |
|
153 | + $message = array( |
|
154 | + 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
155 | + 'type' => 'error' |
|
156 | + ); |
|
157 | + break; |
|
158 | + case 'speed': |
|
159 | + $elapsedTime = $log['data'] - $this->startTime; |
|
160 | + $message = array( |
|
161 | + 'message' => $log['name'], |
|
162 | + 'data' => $this->getReadableTime($elapsedTime), |
|
163 | + 'type' => 'speed' |
|
164 | + ); |
|
165 | + break; |
|
166 | + default: |
|
167 | + $message = array( |
|
168 | + 'message' => "Unrecognized console log type: {$log['type']}", |
|
169 | + 'type' => 'error' |
|
170 | + ); |
|
171 | + break; |
|
172 | + } |
|
173 | + array_push($messages, $message); |
|
174 | + } |
|
175 | + return $messages; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @return array |
|
180 | + */ |
|
181 | + protected function getSpeedMeta() |
|
182 | + { |
|
183 | + $elapsedTime = $this->getReadableTime($this->speedData['elapsed']); |
|
184 | + $allowedTime = $this->getReadableTime($this->speedData['allowed'], 0); |
|
185 | + |
|
186 | + return array( |
|
187 | + 'elapsed' => $elapsedTime, |
|
188 | + 'allowed' => $allowedTime, |
|
189 | + ); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * @return array |
|
194 | + */ |
|
195 | + public function getQueryMeta() |
|
196 | + { |
|
197 | + $queryCount = count($this->queryData); |
|
198 | + $queryTotalTime = array_reduce($this->queryData, function ($sum, $row) { |
|
199 | + return $sum + $row['time']; |
|
200 | + }, 0); |
|
201 | + $queryTotalTime = $this->getReadableTime($queryTotalTime); |
|
202 | + $querySlowestTime = array_reduce($this->queryData, function ($slowest, $row) { |
|
203 | + return ($slowest < $row['time']) ? $row['time'] : $slowest; |
|
204 | + }, 0); |
|
205 | + $querySlowestTime = $this->getReadableTime($querySlowestTime); |
|
206 | + |
|
207 | + return array( |
|
208 | + 'count' => $queryCount, |
|
209 | + 'time' => $queryTotalTime, |
|
210 | + 'slowest' => $querySlowestTime |
|
211 | + ); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * @return array |
|
216 | + */ |
|
217 | + public function getQueryList() |
|
218 | + { |
|
219 | + $queryList = array(); |
|
220 | + foreach ($this->queryData as $query) { |
|
221 | + array_push($queryList, array( |
|
222 | + 'message' => $query['sql'], |
|
223 | + 'sub_data' => array_filter($query['explain']), |
|
224 | + 'data' => $this->getReadableTime($query['time']) |
|
225 | + )); |
|
226 | + } |
|
227 | + return $queryList; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * @return array |
|
232 | + */ |
|
233 | + public function getMemoryMeta() |
|
234 | + { |
|
235 | + $usedMemory = $this->getReadableMemory($this->memoryData['used']); |
|
236 | + $allowedMemory = $this->memoryData['allowed']; // todo parse this, maybe? |
|
237 | + |
|
238 | + return array( |
|
239 | + 'used' => $usedMemory, |
|
240 | + 'allowed' => $allowedMemory |
|
241 | + ); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @return array |
|
246 | + */ |
|
247 | + protected function getFileMeta() |
|
248 | + { |
|
249 | + $fileCount = count($this->fileData); |
|
250 | + $fileTotalSize = array_reduce($this->fileData, function ($sum, $row) { |
|
251 | + return $sum + $row['size']; |
|
252 | + }, 0); |
|
253 | + $fileTotalSize = $this->getReadableMemory($fileTotalSize); |
|
254 | + $fileLargestSize = array_reduce($this->fileData, function ($largest, $row) { |
|
255 | + return ($largest < $row['size']) ? $row['size'] : $largest; |
|
256 | + }, 0); |
|
257 | + $fileLargestSize = $this->getReadableMemory($fileLargestSize); |
|
258 | + |
|
259 | + return array( |
|
260 | + 'count' => $fileCount, |
|
261 | + 'size' => $fileTotalSize, |
|
262 | + 'largest' => $fileLargestSize |
|
263 | + ); |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * @return array |
|
268 | + */ |
|
269 | + protected function getFileList() |
|
270 | + { |
|
271 | + $fileList = array(); |
|
272 | + foreach ($this->fileData as $file) { |
|
273 | + array_push($fileList, array( |
|
274 | + 'message' => $file['name'], |
|
275 | + 'data' => $this->getReadableMemory($file['size']) |
|
276 | + )); |
|
277 | + } |
|
278 | + return $fileList; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * Formatter for human-readable time |
|
283 | + * Only handles time up to 60 minutes gracefully |
|
284 | + * |
|
285 | + * @param double $time |
|
286 | + * @param integer $percision |
|
287 | + * @return string |
|
288 | + */ |
|
289 | + protected function getReadableTime($time, $percision = 3) |
|
290 | + { |
|
291 | + $unit = 's'; |
|
292 | + if ($time < 1) { |
|
293 | + $time *= 1000; |
|
294 | + $percision = 0; |
|
295 | + $unit = 'ms'; |
|
296 | + } elseif ($time > 60) { |
|
297 | + $time /= 60; |
|
298 | + $unit = 'm'; |
|
299 | + } |
|
300 | + $time = number_format($time, $percision); |
|
301 | + return "{$time} {$unit}"; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Formatter for human-readable memory |
|
306 | + * Only handles time up to a few gigs gracefully |
|
307 | + * |
|
308 | + * @param double $size |
|
309 | + * @param integer $percision |
|
310 | + */ |
|
311 | + protected function getReadableMemory($size, $percision = 2) |
|
312 | + { |
|
313 | + $unitOptions = array('b', 'k', 'M', 'G'); |
|
314 | + |
|
315 | + $base = log($size, 1024); |
|
316 | + |
|
317 | + $memory = round(pow(1024, $base - floor($base)), $percision); |
|
318 | + $unit = $unitOptions[floor($base)]; |
|
319 | + return "{$memory} {$unit}"; |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * @param array $messages |
|
324 | + * @param string $type |
|
325 | + * @return array |
|
326 | + */ |
|
327 | + protected function filterMessages($messages, $type) |
|
328 | + { |
|
329 | + return array_filter($messages, function ($message) use ($type) { |
|
330 | + return $message['type'] == $type; |
|
331 | + }); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @returns array |
|
336 | + */ |
|
337 | + protected function gatherTemplateData() |
|
338 | + { |
|
339 | + $consoleMeta = $this->getConsoleMeta(); |
|
340 | + $speedMeta = $this->getSpeedMeta(); |
|
341 | + $queryMeta = $this->getQueryMeta(); |
|
342 | + $memoryMeta = $this->getMemoryMeta(); |
|
343 | + $fileMeta = $this->getFileMeta(); |
|
344 | + |
|
345 | + $consoleMessages = $this->getConsoleMessages(); |
|
346 | + $queryList = $this->getQueryList(); |
|
347 | + $fileList = $this->getFileList(); |
|
348 | + |
|
349 | + return array( |
|
350 | + 'header' => array( |
|
351 | + 'console' => array_sum($consoleMeta), |
|
352 | + 'speed' => $speedMeta['elapsed'], |
|
353 | + 'query' => $queryMeta['count'], |
|
354 | + 'memory' => $memoryMeta['used'], |
|
355 | + 'files' => $fileMeta['count'] |
|
356 | + ), |
|
357 | + 'console' => array( |
|
358 | + 'meta' => $consoleMeta, |
|
359 | + 'messages' => $consoleMessages |
|
360 | + ), |
|
361 | + 'speed' => array( |
|
362 | + 'meta' => $speedMeta, |
|
363 | + 'messages' => $this->filterMessages($consoleMessages, 'speed') |
|
364 | + ), |
|
365 | + 'query' => array( |
|
366 | + 'meta' => $queryMeta, |
|
367 | + 'messages' => $queryList |
|
368 | + ), |
|
369 | + 'memory' => array( |
|
370 | + 'meta' => $memoryMeta, |
|
371 | + 'messages' => $this->filterMessages($consoleMessages, 'memory') |
|
372 | + ), |
|
373 | + 'files' => array( |
|
374 | + 'meta' => $fileMeta, |
|
375 | + 'messages' => $fileList |
|
376 | + ) |
|
377 | + ); |
|
378 | + } |
|
379 | + |
|
380 | + public function __invoke() |
|
381 | + { |
|
382 | + $templateData = $this->gatherTemplateData(); |
|
383 | + |
|
384 | + // todo is this really the best way to load these? |
|
385 | + $styles = file_get_contents(__DIR__ . "/../{$this->options['style_path']}"); |
|
386 | + $script = file_get_contents(__DIR__ . "/../{$this->options['script_path']}"); |
|
387 | + |
|
388 | + call_user_func(function () use ($templateData, $styles, $script) { |
|
389 | + extract($templateData); |
|
390 | + require_once __DIR__ . '/../asset/display.html'; |
|
391 | + }); |
|
392 | + } |
|
393 | 393 | } |
@@ -9,464 +9,464 @@ |
||
9 | 9 | class DisplayTest extends PHPUnit_Framework_TestCase |
10 | 10 | { |
11 | 11 | |
12 | - public function testConstruct() |
|
13 | - { |
|
14 | - $display = new Display(); |
|
15 | - $reflectedDisplay = new ReflectionClass(get_class($display)); |
|
16 | - $reflectedProperty = $reflectedDisplay->getProperty('defaults'); |
|
17 | - $reflectedProperty->setAccessible(true); |
|
18 | - $defaults = $reflectedProperty->getValue($display); |
|
19 | - |
|
20 | - $display = new Display(); |
|
21 | - $this->assertAttributeEquals($defaults, 'options', $display); |
|
22 | - |
|
23 | - $options = array( |
|
24 | - 'script_path' => 'testing/testing.js', |
|
25 | - 'fake_key' => 'foo bar' |
|
26 | - ); |
|
27 | - $expectedOptions = array_intersect_key($options, $defaults); |
|
28 | - $expectedOptions = array_replace($defaults, $expectedOptions); |
|
29 | - $display = new Display($options); |
|
30 | - $this->assertAttributeEquals($expectedOptions, 'options', $display); |
|
31 | - } |
|
32 | - |
|
33 | - public function testSetStartTime() |
|
34 | - { |
|
35 | - $startTime = microtime(true); |
|
36 | - $display = new Display(); |
|
37 | - $display->setStartTime($startTime); |
|
38 | - |
|
39 | - $this->assertAttributeEquals($startTime, 'startTime', $display); |
|
40 | - } |
|
41 | - |
|
42 | - public function testSetConsole() |
|
43 | - { |
|
44 | - $console = new Console(); |
|
45 | - $display = new Display(); |
|
46 | - $display->setConsole($console); |
|
47 | - |
|
48 | - $this->assertAttributeSame($console, 'console', $display); |
|
49 | - } |
|
50 | - |
|
51 | - public function testSetMemoryData() |
|
52 | - { |
|
53 | - $memoryData = array( |
|
54 | - 'used' => memory_get_peak_usage(), |
|
55 | - 'allowed' => ini_get('memory_limit') |
|
56 | - ); |
|
57 | - $display = new Display(); |
|
58 | - $display->setMemoryData($memoryData); |
|
59 | - |
|
60 | - $this->assertAttributeEquals($memoryData, 'memoryData', $display); |
|
61 | - } |
|
62 | - |
|
63 | - public function testSetQueryData() |
|
64 | - { |
|
65 | - $queryData = array( |
|
66 | - 'sql' => 'SELECT * FROM testing', |
|
67 | - 'explain' => array( |
|
68 | - 'key' => 'value' |
|
69 | - ), |
|
70 | - 'time' => 300 |
|
71 | - ); |
|
72 | - $display = new Display(); |
|
73 | - $display->setQueryData($queryData); |
|
74 | - |
|
75 | - $this->assertAttributeEquals($queryData, 'queryData', $display); |
|
76 | - } |
|
77 | - |
|
78 | - public function testSetSpeedData() |
|
79 | - { |
|
80 | - $speedData = array( |
|
81 | - 'elapsed' => 1.234, |
|
82 | - 'allowed' => 30 |
|
83 | - ); |
|
84 | - $display = new Display(); |
|
85 | - $display->setSpeedData($speedData); |
|
86 | - |
|
87 | - $this->assertAttributeEquals($speedData, 'speedData', $display); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @dataProvider dataConsoleStore |
|
92 | - */ |
|
93 | - public function testGetConsoleMeta($consoleStore, $expectedMeta, $expectedMessages) |
|
94 | - { |
|
95 | - $console = new Console(); |
|
96 | - $this->setInternalProperty($console, 'store', $consoleStore); |
|
97 | - $display = new Display(); |
|
98 | - $display->setConsole($console); |
|
99 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMeta'); |
|
100 | - |
|
101 | - $consoleMeta = $reflectedMethod->invoke($display); |
|
102 | - $this->assertEquals($expectedMeta, $consoleMeta); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @dataProvider dataConsoleStore |
|
107 | - */ |
|
108 | - public function testGetConsoleMessages($consoleStore, $expectedMeta, $expectedMessages) |
|
109 | - { |
|
110 | - $console = new Console(); |
|
111 | - $this->setInternalProperty($console, 'store', $consoleStore); |
|
112 | - $display = new Display(); |
|
113 | - $display->setConsole($console); |
|
114 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMessages'); |
|
115 | - |
|
116 | - $consoleMessages = $reflectedMethod->invoke($display); |
|
117 | - $this->assertEquals($expectedMessages, $consoleMessages); |
|
118 | - } |
|
119 | - |
|
120 | - public function testGetSpeedMeta() |
|
121 | - { |
|
122 | - $elapsedTime = 1234.678; |
|
123 | - $allowedTime = 30; |
|
124 | - $display = new Display(); |
|
125 | - $display->setSpeedData(array( |
|
126 | - 'elapsed' => $elapsedTime, |
|
127 | - 'allowed' => $allowedTime |
|
128 | - )); |
|
129 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
130 | - $elapsedTime = $reflectedMethod->invokeArgs($display, array($elapsedTime)); |
|
131 | - $allowedTime = $reflectedMethod->invokeArgs($display, array($allowedTime, 0)); |
|
132 | - $expectedMeta = array( |
|
133 | - 'elapsed' => $elapsedTime, |
|
134 | - 'allowed' => $allowedTime |
|
135 | - ); |
|
136 | - |
|
137 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getSpeedMeta'); |
|
138 | - $speedMeta = $reflectedMethod->invoke($display); |
|
139 | - $this->assertEquals($expectedMeta, $speedMeta); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @dataProvider dataQueryData |
|
144 | - */ |
|
145 | - public function testGetQueryMeta($queryData, $expectedMeta, $expectedList) |
|
146 | - { |
|
147 | - $display = new Display(); |
|
148 | - $display->setQueryData($queryData); |
|
149 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getQueryMeta'); |
|
150 | - |
|
151 | - $queryMeta = $reflectedMethod->invoke($display); |
|
152 | - $this->assertEquals($expectedMeta, $queryMeta); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * @dataProvider dataQueryData |
|
157 | - */ |
|
158 | - public function testGetQueryList($queryData, $expectedMeta, $expectedList) |
|
159 | - { |
|
160 | - $display = new Display(); |
|
161 | - $display->setQueryData($queryData); |
|
162 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getQueryList'); |
|
163 | - |
|
164 | - $queryList = $reflectedMethod->invoke($display); |
|
165 | - $this->assertEquals($expectedList, $queryList); |
|
166 | - } |
|
167 | - |
|
168 | - public function testGetMemoryMeta() |
|
169 | - { |
|
170 | - $usedMemory = 123456; |
|
171 | - $allowedMemory = '128M'; |
|
172 | - $display = new Display(); |
|
173 | - $display->setMemoryData(array( |
|
174 | - 'used' => $usedMemory, |
|
175 | - 'allowed' => $allowedMemory |
|
176 | - )); |
|
177 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
178 | - $usedMemory = $reflectedMethod->invokeArgs($display, array($usedMemory)); |
|
179 | - $expectedMeta = array( |
|
180 | - 'used' => $usedMemory, |
|
181 | - 'allowed' => $allowedMemory |
|
182 | - ); |
|
183 | - |
|
184 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getMemoryMeta'); |
|
185 | - $memoryMeta = $reflectedMethod->invoke($display); |
|
186 | - $this->assertEquals($expectedMeta, $memoryMeta); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * @dataProvider dataFileData |
|
191 | - */ |
|
192 | - public function testGetFileMeta($fileData, $expectedMeta, $expectedList) |
|
193 | - { |
|
194 | - $display = new Display(); |
|
195 | - $display->setFileData($fileData); |
|
196 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getFileMeta'); |
|
197 | - |
|
198 | - $fileMeta = $reflectedMethod->invoke($display); |
|
199 | - $this->assertEquals($expectedMeta, $fileMeta); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @dataProvider dataFileData |
|
204 | - */ |
|
205 | - public function testGetFileList($fileData, $expectedMeta, $expectedList) |
|
206 | - { |
|
207 | - $display = new Display(); |
|
208 | - $display->setFileData($fileData); |
|
209 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getFileList'); |
|
210 | - |
|
211 | - $fileList = $reflectedMethod->invoke($display); |
|
212 | - $this->assertEquals($expectedList, $fileList); |
|
213 | - } |
|
214 | - |
|
215 | - public function testFilterMessages() |
|
216 | - { |
|
217 | - $display = new Display(); |
|
218 | - $reflectedMethod = $this->getAccessibleMethod($display, 'filterMessages'); |
|
219 | - |
|
220 | - $filteredMessages = $reflectedMethod->invokeArgs($display, array(array(array( |
|
221 | - 'type' => 'remove' |
|
222 | - )), 'keep')); |
|
223 | - $this->assertEmpty($filteredMessages); |
|
224 | - $filteredMessages = $reflectedMethod->invokeArgs($display, array(array(array( |
|
225 | - 'type' => 'keep' |
|
226 | - )), 'keep')); |
|
227 | - $this->assertCount(1, $filteredMessages); |
|
228 | - } |
|
229 | - |
|
230 | - public function testGetReadableTime() |
|
231 | - { |
|
232 | - $timeTest = array( |
|
233 | - '.032432' => '32 ms', |
|
234 | - '24.3781' => '24.378 s', |
|
235 | - '145.123' => '2.419 m' |
|
236 | - ); |
|
237 | - $display = new Display(); |
|
238 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
239 | - |
|
240 | - foreach ($timeTest as $rawTime => $expectedTime) { |
|
241 | - $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime)); |
|
242 | - $this->assertEquals($expectedTime, $readableTime); |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - public function testGetReadableMemory() |
|
247 | - { |
|
248 | - $memoryTest = array( |
|
249 | - '314' => '314 b', |
|
250 | - '7403' => '7.23 k', |
|
251 | - '2589983' => '2.47 M' |
|
252 | - ); |
|
253 | - $display = new Display(); |
|
254 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
255 | - |
|
256 | - foreach ($memoryTest as $rawMemory => $expectedMemory) { |
|
257 | - $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory)); |
|
258 | - $this->assertEquals($expectedMemory, $readableMemory); |
|
259 | - } |
|
260 | - } |
|
261 | - |
|
262 | - public function dataConsoleStore() |
|
263 | - { |
|
264 | - $testException = new Exception('testing'); |
|
265 | - $display = new Display(); |
|
266 | - $reflectedTime = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
267 | - $reflectedMemory = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
268 | - |
|
269 | - return array( |
|
270 | - array( |
|
271 | - 'store' => array( |
|
272 | - array( |
|
273 | - 'data' => 'testing message', |
|
274 | - 'type' => 'log' |
|
275 | - ), |
|
276 | - array( |
|
277 | - 'name' => 'now', |
|
278 | - 'data' => microtime(true), |
|
279 | - 'type' => 'speed' |
|
280 | - ), |
|
281 | - array( |
|
282 | - 'name' => 'little later', |
|
283 | - 'data' => microtime(true) + 1, |
|
284 | - 'type' => 'speed' |
|
285 | - ), |
|
286 | - array( |
|
287 | - 'name' => 'invalid key', |
|
288 | - 'type' => 'foo' |
|
289 | - ) |
|
290 | - ), |
|
291 | - 'meta' => array( |
|
292 | - 'log' => 1, |
|
293 | - 'memory' => 0, |
|
294 | - 'error' => 1, |
|
295 | - 'speed' => 2 |
|
296 | - ), |
|
297 | - 'messages' => array( |
|
298 | - array( |
|
299 | - 'message' => 'testing message', |
|
300 | - 'type' => 'log' |
|
301 | - ), |
|
302 | - array( |
|
303 | - 'message' => 'now', |
|
304 | - 'data' => $reflectedTime->invokeArgs($display, array(microtime(true))), |
|
305 | - 'type' => 'speed' |
|
306 | - ), |
|
307 | - array( |
|
308 | - 'message' => 'little later', |
|
309 | - 'data' => $reflectedTime->invokeArgs($display, array(microtime(true) + 1)), |
|
310 | - 'type' => 'speed' |
|
311 | - ), |
|
312 | - array( |
|
313 | - 'message' => 'Unrecognized console log type: foo', |
|
314 | - 'type' => 'error' |
|
315 | - ) |
|
316 | - ) |
|
317 | - ), |
|
318 | - array( |
|
319 | - 'store' => array( |
|
320 | - array( |
|
321 | - 'data' => 'another testing message', |
|
322 | - 'type' => 'log' |
|
323 | - ), |
|
324 | - array( |
|
325 | - 'name' => 'test array', |
|
326 | - 'data' => strlen(serialize(array('key' => 'value'))), |
|
327 | - 'data_type' => 'array', |
|
328 | - 'type' => 'memory' |
|
329 | - ), |
|
330 | - array( |
|
331 | - 'name' => 'memory usage test', |
|
332 | - 'data' => memory_get_usage(), |
|
333 | - 'data_type' => '', |
|
334 | - 'type' => 'memory' |
|
335 | - ), |
|
336 | - array( |
|
337 | - 'data' => $testException->getMessage(), |
|
338 | - 'file' => $testException->getFile(), |
|
339 | - 'line' => $testException->getLine(), |
|
340 | - 'type' => 'error' |
|
341 | - ) |
|
342 | - ), |
|
343 | - 'meta' => array( |
|
344 | - 'log' => 1, |
|
345 | - 'memory' => 2, |
|
346 | - 'error' => 1, |
|
347 | - 'speed' => 0 |
|
348 | - ), |
|
349 | - 'messages' => array( |
|
350 | - array( |
|
351 | - 'message' => 'another testing message', |
|
352 | - 'type' => 'log' |
|
353 | - ), |
|
354 | - array( |
|
355 | - 'message' => 'array: test array', |
|
356 | - 'data' => $reflectedMemory->invokeArgs( |
|
357 | - $display, |
|
358 | - array(strlen(serialize(array('key' => 'value')))) |
|
359 | - ), |
|
360 | - 'type' => 'memory' |
|
361 | - ), |
|
362 | - array( |
|
363 | - 'message' => 'memory usage test', |
|
364 | - 'data' => $reflectedMemory->invokeArgs($display, array(memory_get_usage())), |
|
365 | - 'type' => 'memory' |
|
366 | - ), |
|
367 | - array( |
|
368 | - 'message' => sprintf( |
|
369 | - 'Line %s: %s in %s', |
|
370 | - $testException->getLine(), |
|
371 | - $testException->getMessage(), |
|
372 | - $testException->getFile() |
|
373 | - ), |
|
374 | - 'type' => 'error' |
|
375 | - ) |
|
376 | - ) |
|
377 | - ) |
|
378 | - ); |
|
379 | - } |
|
380 | - |
|
381 | - public function dataQueryData() |
|
382 | - { |
|
383 | - $display = new Display(); |
|
384 | - $reflectedTime = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
385 | - |
|
386 | - return array( |
|
387 | - array( |
|
388 | - 'data' => array( |
|
389 | - array( |
|
390 | - 'sql' => "SELECT * FROM testing", |
|
391 | - 'explain' => array('empty_key' => ''), |
|
392 | - 'time' => 25 |
|
393 | - ), |
|
394 | - array( |
|
395 | - 'sql' => "SELECT id FROM testing WHERE title = :title", |
|
396 | - 'explain' => array('key' => 'value'), |
|
397 | - 'time' => 5 |
|
398 | - ) |
|
399 | - ), |
|
400 | - 'meta' => array( |
|
401 | - 'count' => 2, |
|
402 | - 'time' => $reflectedTime->invokeArgs($display, array(30)), |
|
403 | - 'slowest' => $reflectedTime->invokeArgs($display, array(25)), |
|
404 | - ), |
|
405 | - 'list' => array( |
|
406 | - array( |
|
407 | - 'message' => 'SELECT * FROM testing', |
|
408 | - 'sub_data' => array(), |
|
409 | - 'data' => $reflectedTime->invokeArgs($display, array(25)) |
|
410 | - ), |
|
411 | - array( |
|
412 | - 'message' => 'SELECT id FROM testing WHERE title = :title', |
|
413 | - 'sub_data' => array('key' => 'value'), |
|
414 | - 'data' => $reflectedTime->invokeArgs($display, array(5)) |
|
415 | - ) |
|
416 | - ) |
|
417 | - ) |
|
418 | - ); |
|
419 | - } |
|
420 | - |
|
421 | - public function dataFileData() |
|
422 | - { |
|
423 | - $display = new Display(); |
|
424 | - $reflectedMemory = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
425 | - |
|
426 | - return array( |
|
427 | - array( |
|
428 | - 'data' => array( |
|
429 | - array( |
|
430 | - 'name' => 'test-file', |
|
431 | - 'size' => 1234 |
|
432 | - ), |
|
433 | - array( |
|
434 | - 'name' => 'test-file-2', |
|
435 | - 'size' => 66 |
|
436 | - ) |
|
437 | - ), |
|
438 | - 'meta' => array( |
|
439 | - 'count' => 2, |
|
440 | - 'size' => $reflectedMemory->invokeArgs($display, array(1300)), |
|
441 | - 'largest' => $reflectedMemory->invokeArgs($display, array(1234)), |
|
442 | - ), |
|
443 | - 'list' => array( |
|
444 | - array( |
|
445 | - 'message' => 'test-file', |
|
446 | - 'data' => $reflectedMemory->invokeArgs($display, array(1234)) |
|
447 | - ), |
|
448 | - array( |
|
449 | - 'message' => 'test-file-2', |
|
450 | - 'data' => $reflectedMemory->invokeArgs($display, array(66)) |
|
451 | - ) |
|
452 | - ) |
|
453 | - ) |
|
454 | - ); |
|
455 | - } |
|
456 | - |
|
457 | - protected function setInternalProperty($class, $property, $value) |
|
458 | - { |
|
459 | - $reflectedClass = new ReflectionClass(get_class($class)); |
|
460 | - $reflectedProperty = $reflectedClass->getProperty($property); |
|
461 | - $reflectedProperty->setAccessible(true); |
|
462 | - $reflectedProperty->setValue($class, $value); |
|
463 | - } |
|
464 | - |
|
465 | - protected function getAccessibleMethod($class, $method) |
|
466 | - { |
|
467 | - $reflectedClass = new ReflectionClass(get_class($class)); |
|
468 | - $reflectedMethod = $reflectedClass->getMethod($method); |
|
469 | - $reflectedMethod->setAccessible(true); |
|
470 | - return $reflectedMethod; |
|
471 | - } |
|
12 | + public function testConstruct() |
|
13 | + { |
|
14 | + $display = new Display(); |
|
15 | + $reflectedDisplay = new ReflectionClass(get_class($display)); |
|
16 | + $reflectedProperty = $reflectedDisplay->getProperty('defaults'); |
|
17 | + $reflectedProperty->setAccessible(true); |
|
18 | + $defaults = $reflectedProperty->getValue($display); |
|
19 | + |
|
20 | + $display = new Display(); |
|
21 | + $this->assertAttributeEquals($defaults, 'options', $display); |
|
22 | + |
|
23 | + $options = array( |
|
24 | + 'script_path' => 'testing/testing.js', |
|
25 | + 'fake_key' => 'foo bar' |
|
26 | + ); |
|
27 | + $expectedOptions = array_intersect_key($options, $defaults); |
|
28 | + $expectedOptions = array_replace($defaults, $expectedOptions); |
|
29 | + $display = new Display($options); |
|
30 | + $this->assertAttributeEquals($expectedOptions, 'options', $display); |
|
31 | + } |
|
32 | + |
|
33 | + public function testSetStartTime() |
|
34 | + { |
|
35 | + $startTime = microtime(true); |
|
36 | + $display = new Display(); |
|
37 | + $display->setStartTime($startTime); |
|
38 | + |
|
39 | + $this->assertAttributeEquals($startTime, 'startTime', $display); |
|
40 | + } |
|
41 | + |
|
42 | + public function testSetConsole() |
|
43 | + { |
|
44 | + $console = new Console(); |
|
45 | + $display = new Display(); |
|
46 | + $display->setConsole($console); |
|
47 | + |
|
48 | + $this->assertAttributeSame($console, 'console', $display); |
|
49 | + } |
|
50 | + |
|
51 | + public function testSetMemoryData() |
|
52 | + { |
|
53 | + $memoryData = array( |
|
54 | + 'used' => memory_get_peak_usage(), |
|
55 | + 'allowed' => ini_get('memory_limit') |
|
56 | + ); |
|
57 | + $display = new Display(); |
|
58 | + $display->setMemoryData($memoryData); |
|
59 | + |
|
60 | + $this->assertAttributeEquals($memoryData, 'memoryData', $display); |
|
61 | + } |
|
62 | + |
|
63 | + public function testSetQueryData() |
|
64 | + { |
|
65 | + $queryData = array( |
|
66 | + 'sql' => 'SELECT * FROM testing', |
|
67 | + 'explain' => array( |
|
68 | + 'key' => 'value' |
|
69 | + ), |
|
70 | + 'time' => 300 |
|
71 | + ); |
|
72 | + $display = new Display(); |
|
73 | + $display->setQueryData($queryData); |
|
74 | + |
|
75 | + $this->assertAttributeEquals($queryData, 'queryData', $display); |
|
76 | + } |
|
77 | + |
|
78 | + public function testSetSpeedData() |
|
79 | + { |
|
80 | + $speedData = array( |
|
81 | + 'elapsed' => 1.234, |
|
82 | + 'allowed' => 30 |
|
83 | + ); |
|
84 | + $display = new Display(); |
|
85 | + $display->setSpeedData($speedData); |
|
86 | + |
|
87 | + $this->assertAttributeEquals($speedData, 'speedData', $display); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @dataProvider dataConsoleStore |
|
92 | + */ |
|
93 | + public function testGetConsoleMeta($consoleStore, $expectedMeta, $expectedMessages) |
|
94 | + { |
|
95 | + $console = new Console(); |
|
96 | + $this->setInternalProperty($console, 'store', $consoleStore); |
|
97 | + $display = new Display(); |
|
98 | + $display->setConsole($console); |
|
99 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMeta'); |
|
100 | + |
|
101 | + $consoleMeta = $reflectedMethod->invoke($display); |
|
102 | + $this->assertEquals($expectedMeta, $consoleMeta); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @dataProvider dataConsoleStore |
|
107 | + */ |
|
108 | + public function testGetConsoleMessages($consoleStore, $expectedMeta, $expectedMessages) |
|
109 | + { |
|
110 | + $console = new Console(); |
|
111 | + $this->setInternalProperty($console, 'store', $consoleStore); |
|
112 | + $display = new Display(); |
|
113 | + $display->setConsole($console); |
|
114 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMessages'); |
|
115 | + |
|
116 | + $consoleMessages = $reflectedMethod->invoke($display); |
|
117 | + $this->assertEquals($expectedMessages, $consoleMessages); |
|
118 | + } |
|
119 | + |
|
120 | + public function testGetSpeedMeta() |
|
121 | + { |
|
122 | + $elapsedTime = 1234.678; |
|
123 | + $allowedTime = 30; |
|
124 | + $display = new Display(); |
|
125 | + $display->setSpeedData(array( |
|
126 | + 'elapsed' => $elapsedTime, |
|
127 | + 'allowed' => $allowedTime |
|
128 | + )); |
|
129 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
130 | + $elapsedTime = $reflectedMethod->invokeArgs($display, array($elapsedTime)); |
|
131 | + $allowedTime = $reflectedMethod->invokeArgs($display, array($allowedTime, 0)); |
|
132 | + $expectedMeta = array( |
|
133 | + 'elapsed' => $elapsedTime, |
|
134 | + 'allowed' => $allowedTime |
|
135 | + ); |
|
136 | + |
|
137 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getSpeedMeta'); |
|
138 | + $speedMeta = $reflectedMethod->invoke($display); |
|
139 | + $this->assertEquals($expectedMeta, $speedMeta); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @dataProvider dataQueryData |
|
144 | + */ |
|
145 | + public function testGetQueryMeta($queryData, $expectedMeta, $expectedList) |
|
146 | + { |
|
147 | + $display = new Display(); |
|
148 | + $display->setQueryData($queryData); |
|
149 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getQueryMeta'); |
|
150 | + |
|
151 | + $queryMeta = $reflectedMethod->invoke($display); |
|
152 | + $this->assertEquals($expectedMeta, $queryMeta); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * @dataProvider dataQueryData |
|
157 | + */ |
|
158 | + public function testGetQueryList($queryData, $expectedMeta, $expectedList) |
|
159 | + { |
|
160 | + $display = new Display(); |
|
161 | + $display->setQueryData($queryData); |
|
162 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getQueryList'); |
|
163 | + |
|
164 | + $queryList = $reflectedMethod->invoke($display); |
|
165 | + $this->assertEquals($expectedList, $queryList); |
|
166 | + } |
|
167 | + |
|
168 | + public function testGetMemoryMeta() |
|
169 | + { |
|
170 | + $usedMemory = 123456; |
|
171 | + $allowedMemory = '128M'; |
|
172 | + $display = new Display(); |
|
173 | + $display->setMemoryData(array( |
|
174 | + 'used' => $usedMemory, |
|
175 | + 'allowed' => $allowedMemory |
|
176 | + )); |
|
177 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
178 | + $usedMemory = $reflectedMethod->invokeArgs($display, array($usedMemory)); |
|
179 | + $expectedMeta = array( |
|
180 | + 'used' => $usedMemory, |
|
181 | + 'allowed' => $allowedMemory |
|
182 | + ); |
|
183 | + |
|
184 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getMemoryMeta'); |
|
185 | + $memoryMeta = $reflectedMethod->invoke($display); |
|
186 | + $this->assertEquals($expectedMeta, $memoryMeta); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * @dataProvider dataFileData |
|
191 | + */ |
|
192 | + public function testGetFileMeta($fileData, $expectedMeta, $expectedList) |
|
193 | + { |
|
194 | + $display = new Display(); |
|
195 | + $display->setFileData($fileData); |
|
196 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getFileMeta'); |
|
197 | + |
|
198 | + $fileMeta = $reflectedMethod->invoke($display); |
|
199 | + $this->assertEquals($expectedMeta, $fileMeta); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @dataProvider dataFileData |
|
204 | + */ |
|
205 | + public function testGetFileList($fileData, $expectedMeta, $expectedList) |
|
206 | + { |
|
207 | + $display = new Display(); |
|
208 | + $display->setFileData($fileData); |
|
209 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getFileList'); |
|
210 | + |
|
211 | + $fileList = $reflectedMethod->invoke($display); |
|
212 | + $this->assertEquals($expectedList, $fileList); |
|
213 | + } |
|
214 | + |
|
215 | + public function testFilterMessages() |
|
216 | + { |
|
217 | + $display = new Display(); |
|
218 | + $reflectedMethod = $this->getAccessibleMethod($display, 'filterMessages'); |
|
219 | + |
|
220 | + $filteredMessages = $reflectedMethod->invokeArgs($display, array(array(array( |
|
221 | + 'type' => 'remove' |
|
222 | + )), 'keep')); |
|
223 | + $this->assertEmpty($filteredMessages); |
|
224 | + $filteredMessages = $reflectedMethod->invokeArgs($display, array(array(array( |
|
225 | + 'type' => 'keep' |
|
226 | + )), 'keep')); |
|
227 | + $this->assertCount(1, $filteredMessages); |
|
228 | + } |
|
229 | + |
|
230 | + public function testGetReadableTime() |
|
231 | + { |
|
232 | + $timeTest = array( |
|
233 | + '.032432' => '32 ms', |
|
234 | + '24.3781' => '24.378 s', |
|
235 | + '145.123' => '2.419 m' |
|
236 | + ); |
|
237 | + $display = new Display(); |
|
238 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
239 | + |
|
240 | + foreach ($timeTest as $rawTime => $expectedTime) { |
|
241 | + $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime)); |
|
242 | + $this->assertEquals($expectedTime, $readableTime); |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + public function testGetReadableMemory() |
|
247 | + { |
|
248 | + $memoryTest = array( |
|
249 | + '314' => '314 b', |
|
250 | + '7403' => '7.23 k', |
|
251 | + '2589983' => '2.47 M' |
|
252 | + ); |
|
253 | + $display = new Display(); |
|
254 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
255 | + |
|
256 | + foreach ($memoryTest as $rawMemory => $expectedMemory) { |
|
257 | + $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory)); |
|
258 | + $this->assertEquals($expectedMemory, $readableMemory); |
|
259 | + } |
|
260 | + } |
|
261 | + |
|
262 | + public function dataConsoleStore() |
|
263 | + { |
|
264 | + $testException = new Exception('testing'); |
|
265 | + $display = new Display(); |
|
266 | + $reflectedTime = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
267 | + $reflectedMemory = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
268 | + |
|
269 | + return array( |
|
270 | + array( |
|
271 | + 'store' => array( |
|
272 | + array( |
|
273 | + 'data' => 'testing message', |
|
274 | + 'type' => 'log' |
|
275 | + ), |
|
276 | + array( |
|
277 | + 'name' => 'now', |
|
278 | + 'data' => microtime(true), |
|
279 | + 'type' => 'speed' |
|
280 | + ), |
|
281 | + array( |
|
282 | + 'name' => 'little later', |
|
283 | + 'data' => microtime(true) + 1, |
|
284 | + 'type' => 'speed' |
|
285 | + ), |
|
286 | + array( |
|
287 | + 'name' => 'invalid key', |
|
288 | + 'type' => 'foo' |
|
289 | + ) |
|
290 | + ), |
|
291 | + 'meta' => array( |
|
292 | + 'log' => 1, |
|
293 | + 'memory' => 0, |
|
294 | + 'error' => 1, |
|
295 | + 'speed' => 2 |
|
296 | + ), |
|
297 | + 'messages' => array( |
|
298 | + array( |
|
299 | + 'message' => 'testing message', |
|
300 | + 'type' => 'log' |
|
301 | + ), |
|
302 | + array( |
|
303 | + 'message' => 'now', |
|
304 | + 'data' => $reflectedTime->invokeArgs($display, array(microtime(true))), |
|
305 | + 'type' => 'speed' |
|
306 | + ), |
|
307 | + array( |
|
308 | + 'message' => 'little later', |
|
309 | + 'data' => $reflectedTime->invokeArgs($display, array(microtime(true) + 1)), |
|
310 | + 'type' => 'speed' |
|
311 | + ), |
|
312 | + array( |
|
313 | + 'message' => 'Unrecognized console log type: foo', |
|
314 | + 'type' => 'error' |
|
315 | + ) |
|
316 | + ) |
|
317 | + ), |
|
318 | + array( |
|
319 | + 'store' => array( |
|
320 | + array( |
|
321 | + 'data' => 'another testing message', |
|
322 | + 'type' => 'log' |
|
323 | + ), |
|
324 | + array( |
|
325 | + 'name' => 'test array', |
|
326 | + 'data' => strlen(serialize(array('key' => 'value'))), |
|
327 | + 'data_type' => 'array', |
|
328 | + 'type' => 'memory' |
|
329 | + ), |
|
330 | + array( |
|
331 | + 'name' => 'memory usage test', |
|
332 | + 'data' => memory_get_usage(), |
|
333 | + 'data_type' => '', |
|
334 | + 'type' => 'memory' |
|
335 | + ), |
|
336 | + array( |
|
337 | + 'data' => $testException->getMessage(), |
|
338 | + 'file' => $testException->getFile(), |
|
339 | + 'line' => $testException->getLine(), |
|
340 | + 'type' => 'error' |
|
341 | + ) |
|
342 | + ), |
|
343 | + 'meta' => array( |
|
344 | + 'log' => 1, |
|
345 | + 'memory' => 2, |
|
346 | + 'error' => 1, |
|
347 | + 'speed' => 0 |
|
348 | + ), |
|
349 | + 'messages' => array( |
|
350 | + array( |
|
351 | + 'message' => 'another testing message', |
|
352 | + 'type' => 'log' |
|
353 | + ), |
|
354 | + array( |
|
355 | + 'message' => 'array: test array', |
|
356 | + 'data' => $reflectedMemory->invokeArgs( |
|
357 | + $display, |
|
358 | + array(strlen(serialize(array('key' => 'value')))) |
|
359 | + ), |
|
360 | + 'type' => 'memory' |
|
361 | + ), |
|
362 | + array( |
|
363 | + 'message' => 'memory usage test', |
|
364 | + 'data' => $reflectedMemory->invokeArgs($display, array(memory_get_usage())), |
|
365 | + 'type' => 'memory' |
|
366 | + ), |
|
367 | + array( |
|
368 | + 'message' => sprintf( |
|
369 | + 'Line %s: %s in %s', |
|
370 | + $testException->getLine(), |
|
371 | + $testException->getMessage(), |
|
372 | + $testException->getFile() |
|
373 | + ), |
|
374 | + 'type' => 'error' |
|
375 | + ) |
|
376 | + ) |
|
377 | + ) |
|
378 | + ); |
|
379 | + } |
|
380 | + |
|
381 | + public function dataQueryData() |
|
382 | + { |
|
383 | + $display = new Display(); |
|
384 | + $reflectedTime = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
385 | + |
|
386 | + return array( |
|
387 | + array( |
|
388 | + 'data' => array( |
|
389 | + array( |
|
390 | + 'sql' => "SELECT * FROM testing", |
|
391 | + 'explain' => array('empty_key' => ''), |
|
392 | + 'time' => 25 |
|
393 | + ), |
|
394 | + array( |
|
395 | + 'sql' => "SELECT id FROM testing WHERE title = :title", |
|
396 | + 'explain' => array('key' => 'value'), |
|
397 | + 'time' => 5 |
|
398 | + ) |
|
399 | + ), |
|
400 | + 'meta' => array( |
|
401 | + 'count' => 2, |
|
402 | + 'time' => $reflectedTime->invokeArgs($display, array(30)), |
|
403 | + 'slowest' => $reflectedTime->invokeArgs($display, array(25)), |
|
404 | + ), |
|
405 | + 'list' => array( |
|
406 | + array( |
|
407 | + 'message' => 'SELECT * FROM testing', |
|
408 | + 'sub_data' => array(), |
|
409 | + 'data' => $reflectedTime->invokeArgs($display, array(25)) |
|
410 | + ), |
|
411 | + array( |
|
412 | + 'message' => 'SELECT id FROM testing WHERE title = :title', |
|
413 | + 'sub_data' => array('key' => 'value'), |
|
414 | + 'data' => $reflectedTime->invokeArgs($display, array(5)) |
|
415 | + ) |
|
416 | + ) |
|
417 | + ) |
|
418 | + ); |
|
419 | + } |
|
420 | + |
|
421 | + public function dataFileData() |
|
422 | + { |
|
423 | + $display = new Display(); |
|
424 | + $reflectedMemory = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
425 | + |
|
426 | + return array( |
|
427 | + array( |
|
428 | + 'data' => array( |
|
429 | + array( |
|
430 | + 'name' => 'test-file', |
|
431 | + 'size' => 1234 |
|
432 | + ), |
|
433 | + array( |
|
434 | + 'name' => 'test-file-2', |
|
435 | + 'size' => 66 |
|
436 | + ) |
|
437 | + ), |
|
438 | + 'meta' => array( |
|
439 | + 'count' => 2, |
|
440 | + 'size' => $reflectedMemory->invokeArgs($display, array(1300)), |
|
441 | + 'largest' => $reflectedMemory->invokeArgs($display, array(1234)), |
|
442 | + ), |
|
443 | + 'list' => array( |
|
444 | + array( |
|
445 | + 'message' => 'test-file', |
|
446 | + 'data' => $reflectedMemory->invokeArgs($display, array(1234)) |
|
447 | + ), |
|
448 | + array( |
|
449 | + 'message' => 'test-file-2', |
|
450 | + 'data' => $reflectedMemory->invokeArgs($display, array(66)) |
|
451 | + ) |
|
452 | + ) |
|
453 | + ) |
|
454 | + ); |
|
455 | + } |
|
456 | + |
|
457 | + protected function setInternalProperty($class, $property, $value) |
|
458 | + { |
|
459 | + $reflectedClass = new ReflectionClass(get_class($class)); |
|
460 | + $reflectedProperty = $reflectedClass->getProperty($property); |
|
461 | + $reflectedProperty->setAccessible(true); |
|
462 | + $reflectedProperty->setValue($class, $value); |
|
463 | + } |
|
464 | + |
|
465 | + protected function getAccessibleMethod($class, $method) |
|
466 | + { |
|
467 | + $reflectedClass = new ReflectionClass(get_class($class)); |
|
468 | + $reflectedMethod = $reflectedClass->getMethod($method); |
|
469 | + $reflectedMethod->setAccessible(true); |
|
470 | + return $reflectedMethod; |
|
471 | + } |
|
472 | 472 | } |