@@ -15,88 +15,88 @@ |
||
15 | 15 | class Console |
16 | 16 | { |
17 | 17 | |
18 | - /** @var array */ |
|
19 | - protected $store = array(); |
|
18 | + /** @var array */ |
|
19 | + protected $store = array(); |
|
20 | 20 | |
21 | - /** |
|
22 | - * Logs data to the console |
|
23 | - * Accepts any data type |
|
24 | - * |
|
25 | - * @param mixed $data |
|
26 | - */ |
|
27 | - public function log($data) |
|
28 | - { |
|
29 | - array_push($this->store, array( |
|
30 | - 'data' => $data, |
|
31 | - 'type' => 'log' |
|
32 | - )); |
|
33 | - } |
|
21 | + /** |
|
22 | + * Logs data to the console |
|
23 | + * Accepts any data type |
|
24 | + * |
|
25 | + * @param mixed $data |
|
26 | + */ |
|
27 | + public function log($data) |
|
28 | + { |
|
29 | + array_push($this->store, array( |
|
30 | + 'data' => $data, |
|
31 | + 'type' => 'log' |
|
32 | + )); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Logs memory usage of a variable |
|
37 | - * If no parameter is passed in, logs current memory usage |
|
38 | - * |
|
39 | - * @param mixed $object |
|
40 | - * @param string $name |
|
41 | - */ |
|
42 | - public function logMemory($object = null, $name = 'PHP') |
|
43 | - { |
|
44 | - $memory = memory_get_usage(); |
|
45 | - $dataType = ''; |
|
46 | - if (!is_null($object)) { |
|
47 | - $memory = strlen(serialize($object)); |
|
48 | - $dataType = gettype($object); |
|
49 | - } |
|
35 | + /** |
|
36 | + * Logs memory usage of a variable |
|
37 | + * If no parameter is passed in, logs current memory usage |
|
38 | + * |
|
39 | + * @param mixed $object |
|
40 | + * @param string $name |
|
41 | + */ |
|
42 | + public function logMemory($object = null, $name = 'PHP') |
|
43 | + { |
|
44 | + $memory = memory_get_usage(); |
|
45 | + $dataType = ''; |
|
46 | + if (!is_null($object)) { |
|
47 | + $memory = strlen(serialize($object)); |
|
48 | + $dataType = gettype($object); |
|
49 | + } |
|
50 | 50 | |
51 | - array_push($this->store, array( |
|
52 | - 'name' => $name, |
|
53 | - 'data' => $memory, |
|
54 | - 'data_type' => $dataType, |
|
55 | - 'type' => 'memory' |
|
56 | - )); |
|
57 | - } |
|
51 | + array_push($this->store, array( |
|
52 | + 'name' => $name, |
|
53 | + 'data' => $memory, |
|
54 | + 'data_type' => $dataType, |
|
55 | + 'type' => 'memory' |
|
56 | + )); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Logs exception with optional message override |
|
61 | - * |
|
62 | - * @param Exception $exception |
|
63 | - * @param string $message |
|
64 | - */ |
|
65 | - public function logError(Exception $exception, $message = '') |
|
66 | - { |
|
67 | - if (empty($message)) { |
|
68 | - $message = $exception->getMessage(); |
|
69 | - } |
|
59 | + /** |
|
60 | + * Logs exception with optional message override |
|
61 | + * |
|
62 | + * @param Exception $exception |
|
63 | + * @param string $message |
|
64 | + */ |
|
65 | + public function logError(Exception $exception, $message = '') |
|
66 | + { |
|
67 | + if (empty($message)) { |
|
68 | + $message = $exception->getMessage(); |
|
69 | + } |
|
70 | 70 | |
71 | - array_push($this->store, array( |
|
72 | - 'data' => $message, |
|
73 | - 'file' => $exception->getFile(), |
|
74 | - 'line' => $exception->getLine(), |
|
75 | - 'type' => 'error' |
|
76 | - )); |
|
77 | - } |
|
71 | + array_push($this->store, array( |
|
72 | + 'data' => $message, |
|
73 | + 'file' => $exception->getFile(), |
|
74 | + 'line' => $exception->getLine(), |
|
75 | + 'type' => 'error' |
|
76 | + )); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Logs current time with optional message |
|
81 | - * |
|
82 | - * @param string $name |
|
83 | - */ |
|
84 | - public function logSpeed($name = 'Point in Time') |
|
85 | - { |
|
86 | - array_push($this->store, array( |
|
87 | - 'data' => microtime(true), |
|
88 | - 'name' => $name, |
|
89 | - 'type' => 'speed' |
|
90 | - )); |
|
91 | - } |
|
79 | + /** |
|
80 | + * Logs current time with optional message |
|
81 | + * |
|
82 | + * @param string $name |
|
83 | + */ |
|
84 | + public function logSpeed($name = 'Point in Time') |
|
85 | + { |
|
86 | + array_push($this->store, array( |
|
87 | + 'data' => microtime(true), |
|
88 | + 'name' => $name, |
|
89 | + 'type' => 'speed' |
|
90 | + )); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Returns the collected logs |
|
95 | - * |
|
96 | - * @returns array |
|
97 | - */ |
|
98 | - public function getLogs() |
|
99 | - { |
|
100 | - return $this->store; |
|
101 | - } |
|
93 | + /** |
|
94 | + * Returns the collected logs |
|
95 | + * |
|
96 | + * @returns array |
|
97 | + */ |
|
98 | + public function getLogs() |
|
99 | + { |
|
100 | + return $this->store; |
|
101 | + } |
|
102 | 102 | } |
@@ -9,108 +9,108 @@ |
||
9 | 9 | class ConsoleTest extends PHPUnit_Framework_TestCase |
10 | 10 | { |
11 | 11 | |
12 | - public function testLog() |
|
13 | - { |
|
14 | - $data = array( |
|
15 | - 'key' => 'value' |
|
16 | - ); |
|
17 | - |
|
18 | - $console = new Console(); |
|
19 | - $console->log($data); |
|
20 | - $store = $this->getProtectedStore($console); |
|
21 | - $log = array_pop($store); |
|
22 | - |
|
23 | - $this->assertSame($data, $log['data']); |
|
24 | - $this->assertEquals('log', $log['type']); |
|
25 | - } |
|
26 | - |
|
27 | - public function testLogMemory() |
|
28 | - { |
|
29 | - $data = array( |
|
30 | - 'key' => 'value' |
|
31 | - ); |
|
32 | - $memory = strlen(serialize($data)); |
|
33 | - $name = 'Test Array'; |
|
34 | - |
|
35 | - $console = new Console(); |
|
36 | - $console->logMemory($data, $name); |
|
37 | - $store = $this->getProtectedStore($console); |
|
38 | - $log = array_pop($store); |
|
39 | - |
|
40 | - $this->assertEquals($name, $log['name']); |
|
41 | - $this->assertEquals($memory, $log['data']); |
|
42 | - $this->assertEquals('array', $log['data_type']); |
|
43 | - $this->assertEquals('memory', $log['type']); |
|
44 | - } |
|
45 | - |
|
46 | - public function testLogError() |
|
47 | - { |
|
48 | - $error = new Exception('Test Exception'); |
|
49 | - |
|
50 | - $console = new Console(); |
|
51 | - $console->logError($error); |
|
52 | - $store = $this->getProtectedStore($console); |
|
53 | - $log = array_pop($store); |
|
54 | - |
|
55 | - $this->assertEquals($error->getMessage(), $log['data']); |
|
56 | - $this->assertEquals($error->getFile(), $log['file']); |
|
57 | - $this->assertEquals($error->getLine(), $log['line']); |
|
58 | - $this->assertEquals('error', $log['type']); |
|
59 | - |
|
60 | - $error = new Exception('Test Exception'); |
|
61 | - $message = 'override message'; |
|
62 | - |
|
63 | - $console = new Console(); |
|
64 | - $console->logError($error, $message); |
|
65 | - $store = $this->getProtectedStore($console); |
|
66 | - $log = array_pop($store); |
|
67 | - |
|
68 | - $this->assertEquals($message, $log['data']); |
|
69 | - } |
|
70 | - |
|
71 | - public function testLogSpeed() |
|
72 | - { |
|
73 | - $name = 'Test Speed'; |
|
74 | - |
|
75 | - $console = new Console(); |
|
76 | - $console->logSpeed($name); |
|
77 | - $store = $this->getProtectedStore($console); |
|
78 | - $log = array_pop($store); |
|
79 | - |
|
80 | - $this->assertEquals($name, $log['name']); |
|
81 | - $this->assertEquals('speed', $log['type']); |
|
82 | - } |
|
83 | - |
|
84 | - public function testGetLogs() |
|
85 | - { |
|
86 | - $store = array( |
|
87 | - array( |
|
88 | - 'data' => 'a string', |
|
89 | - 'type' => 'log' |
|
90 | - ), |
|
91 | - array( |
|
92 | - 'name' => '', |
|
93 | - 'data' => 123, |
|
94 | - 'data_type' => 'array', |
|
95 | - 'type' => 'memory' |
|
96 | - ) |
|
97 | - ); |
|
98 | - |
|
99 | - $console = new Console(); |
|
100 | - |
|
101 | - $reflectedConsole = new ReflectionClass(get_class($console)); |
|
102 | - $reflectedProperty = $reflectedConsole->getProperty('store'); |
|
103 | - $reflectedProperty->setAccessible(true); |
|
104 | - $reflectedProperty->setValue($console, $store); |
|
105 | - |
|
106 | - $this->assertSame($store, $console->getLogs()); |
|
107 | - } |
|
108 | - |
|
109 | - protected function getProtectedStore(Console $console) |
|
110 | - { |
|
111 | - $reflectedConsole = new ReflectionClass(get_class($console)); |
|
112 | - $reflectedProperty = $reflectedConsole->getProperty('store'); |
|
113 | - $reflectedProperty->setAccessible(true); |
|
114 | - return $reflectedProperty->getValue($console); |
|
115 | - } |
|
12 | + public function testLog() |
|
13 | + { |
|
14 | + $data = array( |
|
15 | + 'key' => 'value' |
|
16 | + ); |
|
17 | + |
|
18 | + $console = new Console(); |
|
19 | + $console->log($data); |
|
20 | + $store = $this->getProtectedStore($console); |
|
21 | + $log = array_pop($store); |
|
22 | + |
|
23 | + $this->assertSame($data, $log['data']); |
|
24 | + $this->assertEquals('log', $log['type']); |
|
25 | + } |
|
26 | + |
|
27 | + public function testLogMemory() |
|
28 | + { |
|
29 | + $data = array( |
|
30 | + 'key' => 'value' |
|
31 | + ); |
|
32 | + $memory = strlen(serialize($data)); |
|
33 | + $name = 'Test Array'; |
|
34 | + |
|
35 | + $console = new Console(); |
|
36 | + $console->logMemory($data, $name); |
|
37 | + $store = $this->getProtectedStore($console); |
|
38 | + $log = array_pop($store); |
|
39 | + |
|
40 | + $this->assertEquals($name, $log['name']); |
|
41 | + $this->assertEquals($memory, $log['data']); |
|
42 | + $this->assertEquals('array', $log['data_type']); |
|
43 | + $this->assertEquals('memory', $log['type']); |
|
44 | + } |
|
45 | + |
|
46 | + public function testLogError() |
|
47 | + { |
|
48 | + $error = new Exception('Test Exception'); |
|
49 | + |
|
50 | + $console = new Console(); |
|
51 | + $console->logError($error); |
|
52 | + $store = $this->getProtectedStore($console); |
|
53 | + $log = array_pop($store); |
|
54 | + |
|
55 | + $this->assertEquals($error->getMessage(), $log['data']); |
|
56 | + $this->assertEquals($error->getFile(), $log['file']); |
|
57 | + $this->assertEquals($error->getLine(), $log['line']); |
|
58 | + $this->assertEquals('error', $log['type']); |
|
59 | + |
|
60 | + $error = new Exception('Test Exception'); |
|
61 | + $message = 'override message'; |
|
62 | + |
|
63 | + $console = new Console(); |
|
64 | + $console->logError($error, $message); |
|
65 | + $store = $this->getProtectedStore($console); |
|
66 | + $log = array_pop($store); |
|
67 | + |
|
68 | + $this->assertEquals($message, $log['data']); |
|
69 | + } |
|
70 | + |
|
71 | + public function testLogSpeed() |
|
72 | + { |
|
73 | + $name = 'Test Speed'; |
|
74 | + |
|
75 | + $console = new Console(); |
|
76 | + $console->logSpeed($name); |
|
77 | + $store = $this->getProtectedStore($console); |
|
78 | + $log = array_pop($store); |
|
79 | + |
|
80 | + $this->assertEquals($name, $log['name']); |
|
81 | + $this->assertEquals('speed', $log['type']); |
|
82 | + } |
|
83 | + |
|
84 | + public function testGetLogs() |
|
85 | + { |
|
86 | + $store = array( |
|
87 | + array( |
|
88 | + 'data' => 'a string', |
|
89 | + 'type' => 'log' |
|
90 | + ), |
|
91 | + array( |
|
92 | + 'name' => '', |
|
93 | + 'data' => 123, |
|
94 | + 'data_type' => 'array', |
|
95 | + 'type' => 'memory' |
|
96 | + ) |
|
97 | + ); |
|
98 | + |
|
99 | + $console = new Console(); |
|
100 | + |
|
101 | + $reflectedConsole = new ReflectionClass(get_class($console)); |
|
102 | + $reflectedProperty = $reflectedConsole->getProperty('store'); |
|
103 | + $reflectedProperty->setAccessible(true); |
|
104 | + $reflectedProperty->setValue($console, $store); |
|
105 | + |
|
106 | + $this->assertSame($store, $console->getLogs()); |
|
107 | + } |
|
108 | + |
|
109 | + protected function getProtectedStore(Console $console) |
|
110 | + { |
|
111 | + $reflectedConsole = new ReflectionClass(get_class($console)); |
|
112 | + $reflectedProperty = $reflectedConsole->getProperty('store'); |
|
113 | + $reflectedProperty->setAccessible(true); |
|
114 | + return $reflectedProperty->getValue($console); |
|
115 | + } |
|
116 | 116 | } |
@@ -6,5 +6,5 @@ |
||
6 | 6 | |
7 | 7 | class LoggingPdo extends PDO |
8 | 8 | { |
9 | - public $queries = array(); |
|
9 | + public $queries = array(); |
|
10 | 10 | } |
@@ -17,194 +17,194 @@ |
||
17 | 17 | class PhpQuickProfiler |
18 | 18 | { |
19 | 19 | |
20 | - /** @var double */ |
|
21 | - protected $startTime; |
|
22 | - |
|
23 | - /** @var Console */ |
|
24 | - protected $console; |
|
25 | - |
|
26 | - /** @var Display */ |
|
27 | - protected $display; |
|
28 | - |
|
29 | - /** @var array */ |
|
30 | - protected $profiledQueries = array(); |
|
31 | - |
|
32 | - /** |
|
33 | - * @param double $startTime |
|
34 | - */ |
|
35 | - public function __construct($startTime = null) |
|
36 | - { |
|
37 | - if (is_null($startTime)) { |
|
38 | - $startTime = microtime(true); |
|
39 | - } |
|
40 | - $this->startTime = $startTime; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * @param Console $console |
|
45 | - */ |
|
46 | - public function setConsole(Console $console) |
|
47 | - { |
|
48 | - $this->console = $console; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @param Display $display |
|
53 | - */ |
|
54 | - public function setDisplay(Display $display) |
|
55 | - { |
|
56 | - $this->display = $display; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Get data about files loaded for the application to current point |
|
61 | - * |
|
62 | - * @returns array |
|
63 | - */ |
|
64 | - public function gatherFileData() |
|
65 | - { |
|
66 | - $files = get_included_files(); |
|
67 | - $data = array(); |
|
68 | - foreach ($files as $file) { |
|
69 | - array_push($data, array( |
|
70 | - 'name' => $file, |
|
71 | - 'size' => filesize($file) |
|
72 | - )); |
|
73 | - } |
|
74 | - return $data; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Get data about memory usage of the application |
|
79 | - * |
|
80 | - * @returns array |
|
81 | - */ |
|
82 | - public function gatherMemoryData() |
|
83 | - { |
|
84 | - $usedMemory = memory_get_peak_usage(); |
|
85 | - $allowedMemory = ini_get('memory_limit'); |
|
86 | - return array( |
|
87 | - 'used' => $usedMemory, |
|
88 | - 'allowed' => $allowedMemory |
|
89 | - ); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @param array $profiledQueries |
|
94 | - */ |
|
95 | - public function setProfiledQueries(array $profiledQueries) |
|
96 | - { |
|
97 | - $this->profiledQueries = $profiledQueries; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Get data about sql usage of the application |
|
102 | - * |
|
103 | - * @param object $dbConnection |
|
104 | - * @returns array |
|
105 | - */ |
|
106 | - public function gatherQueryData($dbConnection = null) |
|
107 | - { |
|
108 | - if (is_null($dbConnection)) { |
|
109 | - return array(); |
|
110 | - } |
|
111 | - |
|
112 | - if (empty($this->profiledQueries) && property_exists($dbConnection, 'queries')) { |
|
113 | - $this->setProfiledQueries($dbConnection->queries); |
|
114 | - } |
|
115 | - |
|
116 | - $data = array(); |
|
117 | - foreach ($this->profiledQueries as $query) { |
|
118 | - array_push($data, array( |
|
119 | - 'sql' => $query['sql'], |
|
120 | - 'explain' => $this->explainQuery($dbConnection, $query['sql'], $query['parameters']), |
|
121 | - 'time' => $query['time'] |
|
122 | - )); |
|
123 | - } |
|
124 | - return $data; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Attempts to explain a query |
|
129 | - * |
|
130 | - * @param object $dbConnection |
|
131 | - * @param string $query |
|
132 | - * @param array $parameters |
|
133 | - * @throws Exception |
|
134 | - * @return array |
|
135 | - */ |
|
136 | - protected function explainQuery($dbConnection, $query, $parameters = array()) |
|
137 | - { |
|
138 | - $driver = $dbConnection->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
139 | - $query = $this->getExplainQuery($query, $driver); |
|
140 | - $statement = $dbConnection->prepare($query); |
|
141 | - if ($statement === false) { |
|
142 | - throw new Exception('Invalid query passed to explainQuery method'); |
|
143 | - } |
|
144 | - $statement->execute($parameters); |
|
145 | - $result = $statement->fetch(\PDO::FETCH_ASSOC); |
|
146 | - if ($result === false) { |
|
147 | - throw new Exception('Query could not be explained with given parameters'); |
|
148 | - } |
|
149 | - return $result; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Attempts to figure out what kind of explain query format the db wants |
|
154 | - * |
|
155 | - * @param string $query |
|
156 | - * @param string $driver |
|
157 | - * @throws Exception |
|
158 | - * @return string |
|
159 | - */ |
|
160 | - protected function getExplainQuery($query, $driver) |
|
161 | - { |
|
162 | - if ($driver == 'mysql') { |
|
163 | - return "EXPLAIN {$query}"; |
|
164 | - } elseif ($driver == 'sqlite') { |
|
165 | - return "EXPLAIN QUERY PLAN {$query}"; |
|
166 | - } |
|
167 | - throw new Exception('Could not process db driver'); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Get data about speed of the application |
|
172 | - * |
|
173 | - * @returns array |
|
174 | - */ |
|
175 | - public function gatherSpeedData() |
|
176 | - { |
|
177 | - $elapsedTime = microtime(true) - $this->startTime; |
|
178 | - $elapsedTime = round($elapsedTime, 3); |
|
179 | - $allowedTime = ini_get('max_execution_time'); |
|
180 | - return array( |
|
181 | - 'elapsed' => $elapsedTime, |
|
182 | - 'allowed' => $allowedTime |
|
183 | - ); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Triggers end display of the profiling data |
|
188 | - * |
|
189 | - * @param object $dbConnection |
|
190 | - * @throws Exception |
|
191 | - */ |
|
192 | - public function display($dbConnection = null) |
|
193 | - { |
|
194 | - if (!isset($this->display)) { |
|
195 | - throw new Exception('Display object has not been injected into Profiler'); |
|
196 | - } |
|
197 | - if (!isset($this->console)) { |
|
198 | - throw new Exception('Console object has not been injected into Profiler'); |
|
199 | - } |
|
200 | - |
|
201 | - $this->display->setStartTime($this->startTime); |
|
202 | - $this->display->setConsole($this->console); |
|
203 | - $this->display->setFileData($this->gatherFileData()); |
|
204 | - $this->display->setMemoryData($this->gatherMemoryData()); |
|
205 | - $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
206 | - $this->display->setSpeedData($this->gatherSpeedData()); |
|
207 | - |
|
208 | - $this->display->__invoke(); |
|
209 | - } |
|
20 | + /** @var double */ |
|
21 | + protected $startTime; |
|
22 | + |
|
23 | + /** @var Console */ |
|
24 | + protected $console; |
|
25 | + |
|
26 | + /** @var Display */ |
|
27 | + protected $display; |
|
28 | + |
|
29 | + /** @var array */ |
|
30 | + protected $profiledQueries = array(); |
|
31 | + |
|
32 | + /** |
|
33 | + * @param double $startTime |
|
34 | + */ |
|
35 | + public function __construct($startTime = null) |
|
36 | + { |
|
37 | + if (is_null($startTime)) { |
|
38 | + $startTime = microtime(true); |
|
39 | + } |
|
40 | + $this->startTime = $startTime; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * @param Console $console |
|
45 | + */ |
|
46 | + public function setConsole(Console $console) |
|
47 | + { |
|
48 | + $this->console = $console; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @param Display $display |
|
53 | + */ |
|
54 | + public function setDisplay(Display $display) |
|
55 | + { |
|
56 | + $this->display = $display; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Get data about files loaded for the application to current point |
|
61 | + * |
|
62 | + * @returns array |
|
63 | + */ |
|
64 | + public function gatherFileData() |
|
65 | + { |
|
66 | + $files = get_included_files(); |
|
67 | + $data = array(); |
|
68 | + foreach ($files as $file) { |
|
69 | + array_push($data, array( |
|
70 | + 'name' => $file, |
|
71 | + 'size' => filesize($file) |
|
72 | + )); |
|
73 | + } |
|
74 | + return $data; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Get data about memory usage of the application |
|
79 | + * |
|
80 | + * @returns array |
|
81 | + */ |
|
82 | + public function gatherMemoryData() |
|
83 | + { |
|
84 | + $usedMemory = memory_get_peak_usage(); |
|
85 | + $allowedMemory = ini_get('memory_limit'); |
|
86 | + return array( |
|
87 | + 'used' => $usedMemory, |
|
88 | + 'allowed' => $allowedMemory |
|
89 | + ); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @param array $profiledQueries |
|
94 | + */ |
|
95 | + public function setProfiledQueries(array $profiledQueries) |
|
96 | + { |
|
97 | + $this->profiledQueries = $profiledQueries; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Get data about sql usage of the application |
|
102 | + * |
|
103 | + * @param object $dbConnection |
|
104 | + * @returns array |
|
105 | + */ |
|
106 | + public function gatherQueryData($dbConnection = null) |
|
107 | + { |
|
108 | + if (is_null($dbConnection)) { |
|
109 | + return array(); |
|
110 | + } |
|
111 | + |
|
112 | + if (empty($this->profiledQueries) && property_exists($dbConnection, 'queries')) { |
|
113 | + $this->setProfiledQueries($dbConnection->queries); |
|
114 | + } |
|
115 | + |
|
116 | + $data = array(); |
|
117 | + foreach ($this->profiledQueries as $query) { |
|
118 | + array_push($data, array( |
|
119 | + 'sql' => $query['sql'], |
|
120 | + 'explain' => $this->explainQuery($dbConnection, $query['sql'], $query['parameters']), |
|
121 | + 'time' => $query['time'] |
|
122 | + )); |
|
123 | + } |
|
124 | + return $data; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Attempts to explain a query |
|
129 | + * |
|
130 | + * @param object $dbConnection |
|
131 | + * @param string $query |
|
132 | + * @param array $parameters |
|
133 | + * @throws Exception |
|
134 | + * @return array |
|
135 | + */ |
|
136 | + protected function explainQuery($dbConnection, $query, $parameters = array()) |
|
137 | + { |
|
138 | + $driver = $dbConnection->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
139 | + $query = $this->getExplainQuery($query, $driver); |
|
140 | + $statement = $dbConnection->prepare($query); |
|
141 | + if ($statement === false) { |
|
142 | + throw new Exception('Invalid query passed to explainQuery method'); |
|
143 | + } |
|
144 | + $statement->execute($parameters); |
|
145 | + $result = $statement->fetch(\PDO::FETCH_ASSOC); |
|
146 | + if ($result === false) { |
|
147 | + throw new Exception('Query could not be explained with given parameters'); |
|
148 | + } |
|
149 | + return $result; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Attempts to figure out what kind of explain query format the db wants |
|
154 | + * |
|
155 | + * @param string $query |
|
156 | + * @param string $driver |
|
157 | + * @throws Exception |
|
158 | + * @return string |
|
159 | + */ |
|
160 | + protected function getExplainQuery($query, $driver) |
|
161 | + { |
|
162 | + if ($driver == 'mysql') { |
|
163 | + return "EXPLAIN {$query}"; |
|
164 | + } elseif ($driver == 'sqlite') { |
|
165 | + return "EXPLAIN QUERY PLAN {$query}"; |
|
166 | + } |
|
167 | + throw new Exception('Could not process db driver'); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Get data about speed of the application |
|
172 | + * |
|
173 | + * @returns array |
|
174 | + */ |
|
175 | + public function gatherSpeedData() |
|
176 | + { |
|
177 | + $elapsedTime = microtime(true) - $this->startTime; |
|
178 | + $elapsedTime = round($elapsedTime, 3); |
|
179 | + $allowedTime = ini_get('max_execution_time'); |
|
180 | + return array( |
|
181 | + 'elapsed' => $elapsedTime, |
|
182 | + 'allowed' => $allowedTime |
|
183 | + ); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Triggers end display of the profiling data |
|
188 | + * |
|
189 | + * @param object $dbConnection |
|
190 | + * @throws Exception |
|
191 | + */ |
|
192 | + public function display($dbConnection = null) |
|
193 | + { |
|
194 | + if (!isset($this->display)) { |
|
195 | + throw new Exception('Display object has not been injected into Profiler'); |
|
196 | + } |
|
197 | + if (!isset($this->console)) { |
|
198 | + throw new Exception('Console object has not been injected into Profiler'); |
|
199 | + } |
|
200 | + |
|
201 | + $this->display->setStartTime($this->startTime); |
|
202 | + $this->display->setConsole($this->console); |
|
203 | + $this->display->setFileData($this->gatherFileData()); |
|
204 | + $this->display->setMemoryData($this->gatherMemoryData()); |
|
205 | + $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
206 | + $this->display->setSpeedData($this->gatherSpeedData()); |
|
207 | + |
|
208 | + $this->display->__invoke(); |
|
209 | + } |
|
210 | 210 | } |
@@ -8,19 +8,19 @@ discard block |
||
8 | 8 | class PhpQuickProfilerTest extends PHPUnit_Framework_TestCase |
9 | 9 | { |
10 | 10 | |
11 | - protected static $dbConnection; |
|
11 | + protected static $dbConnection; |
|
12 | 12 | |
13 | - public static function setUpBeforeClass() |
|
14 | - { |
|
15 | - self::$dbConnection = new LoggingPdo('sqlite::memory:'); |
|
16 | - $createTable = " |
|
13 | + public static function setUpBeforeClass() |
|
14 | + { |
|
15 | + self::$dbConnection = new LoggingPdo('sqlite::memory:'); |
|
16 | + $createTable = " |
|
17 | 17 | CREATE TABLE IF NOT EXISTS `testing` ( |
18 | 18 | `id` integer PRIMARY KEY AUTOINCREMENT, |
19 | 19 | `title` varchar(60) NOT NULL |
20 | 20 | );"; |
21 | - self::$dbConnection->exec($createTable); |
|
21 | + self::$dbConnection->exec($createTable); |
|
22 | 22 | |
23 | - $hydrateTable = " |
|
23 | + $hydrateTable = " |
|
24 | 24 | INSERT INTO `testing` |
25 | 25 | (`title`) |
26 | 26 | VALUES |
@@ -28,318 +28,318 @@ discard block |
||
28 | 28 | ('beta'), |
29 | 29 | ('charlie'), |
30 | 30 | ('delta');"; |
31 | - self::$dbConnection->exec($hydrateTable); |
|
32 | - } |
|
33 | - |
|
34 | - public function testConstruct() |
|
35 | - { |
|
36 | - $startTime = microtime(true); |
|
37 | - |
|
38 | - $profiler = new PhpQuickProfiler(); |
|
39 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
40 | - |
|
41 | - $profiler = new PhpQuickProfiler($startTime); |
|
42 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
43 | - } |
|
44 | - |
|
45 | - public function testSetConsole() |
|
46 | - { |
|
47 | - $console = new Console(); |
|
48 | - $profiler = new PhpQuickProfiler(); |
|
49 | - $profiler->setConsole($console); |
|
50 | - |
|
51 | - $this->assertAttributeSame($console, 'console', $profiler); |
|
52 | - } |
|
53 | - |
|
54 | - public function testSetDisplay() |
|
55 | - { |
|
56 | - $display = new Display(); |
|
57 | - $profiler = new PhpQuickProfiler(); |
|
58 | - $profiler->setDisplay($display); |
|
59 | - |
|
60 | - $this->assertAttributeSame($display, 'display', $profiler); |
|
61 | - } |
|
62 | - |
|
63 | - public function testGatherFileData() |
|
64 | - { |
|
65 | - $files = get_included_files(); |
|
66 | - $profiler = new PhpQuickProfiler(); |
|
67 | - $gatheredFileData = $profiler->gatherFileData(); |
|
68 | - |
|
69 | - $this->assertInternalType('array', $gatheredFileData); |
|
70 | - $this->assertEquals(count($files), count($gatheredFileData)); |
|
71 | - foreach ($gatheredFileData as $fileData) { |
|
72 | - $this->assertInternalType('array', $fileData); |
|
73 | - $this->assertArrayHasKey('name', $fileData); |
|
74 | - $this->assertContains($fileData['name'], $files); |
|
75 | - $this->assertArrayHasKey('size', $fileData); |
|
76 | - $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - public function testGatherMemoryData() |
|
81 | - { |
|
82 | - $memoryUsage = memory_get_peak_usage(); |
|
83 | - $allowedLimit = ini_get('memory_limit'); |
|
84 | - $profiler = new PhpQuickProfiler(); |
|
85 | - $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
86 | - |
|
87 | - $this->assertInternalType('array', $gatheredMemoryData); |
|
88 | - $this->assertEquals(2, count($gatheredMemoryData)); |
|
89 | - $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
90 | - $this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
|
91 | - $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
92 | - $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
|
93 | - } |
|
94 | - |
|
95 | - public function testSetProfiledQueries() |
|
96 | - { |
|
97 | - $profiledQueries = $this->dataProfiledQueries(); |
|
98 | - $profiler = new PhpQuickProfiler(); |
|
99 | - $profiler->setProfiledQueries($profiledQueries); |
|
100 | - |
|
101 | - $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
102 | - } |
|
103 | - |
|
104 | - public function testGatherQueryData() |
|
105 | - { |
|
106 | - $profiledQueries = $this->dataProfiledQueries(); |
|
107 | - $profiledQueriesSql = array(); |
|
108 | - $profiledQueriesTime = array(); |
|
109 | - foreach ($profiledQueries as $queryData) { |
|
110 | - array_push($profiledQueriesSql, $queryData['sql']); |
|
111 | - array_push($profiledQueriesTime, $queryData['time']); |
|
112 | - } |
|
113 | - |
|
114 | - $profiler = new PhpQuickProfiler(); |
|
115 | - $profiler->setProfiledQueries($profiledQueries); |
|
116 | - $gatheredQueryData = $profiler->gatherQueryData(self::$dbConnection); |
|
117 | - |
|
118 | - $this->assertInternalType('array', $gatheredQueryData); |
|
119 | - $this->assertEquals(count($profiledQueries), count($gatheredQueryData)); |
|
120 | - foreach ($gatheredQueryData as $queryData) { |
|
121 | - $this->assertInternalType('array', $queryData); |
|
122 | - $this->assertArrayHasKey('sql', $queryData); |
|
123 | - $this->assertContains($queryData['sql'], $profiledQueriesSql); |
|
124 | - $this->assertArrayHasKey('explain', $queryData); |
|
125 | - $this->assertInternaltype('array', $queryData['explain']); |
|
126 | - $this->assertGreaterThan(0, count($queryData['explain'])); |
|
127 | - $this->assertArrayHasKey('time', $queryData); |
|
128 | - $this->assertContains($queryData['time'], $profiledQueriesTime); |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - public function testGatherQueryDataInternalProfiler() |
|
133 | - { |
|
134 | - $profiledQueries = $this->dataProfiledQueries(); |
|
135 | - $dbConnection = self::$dbConnection; |
|
136 | - $dbConnection->queries = $profiledQueries; |
|
137 | - $profiler = new PhpQuickProfiler(); |
|
138 | - $profiler->gatherQueryData($dbConnection); |
|
139 | - |
|
140 | - $this->assertAttributeSame($profiledQueries, 'profiledQueries', $profiler); |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @dataProvider dataProfiledQueries |
|
145 | - */ |
|
146 | - public function testExplainQuery($sql, $parameters) |
|
147 | - { |
|
148 | - $profiler = new PhpQuickProfiler(); |
|
149 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
150 | - |
|
151 | - $explainedQuery = $reflectedMethod->invokeArgs( |
|
152 | - $profiler, |
|
153 | - array(self::$dbConnection, $sql, $parameters) |
|
154 | - ); |
|
155 | - $this->assertInternalType('array', $explainedQuery); |
|
156 | - $this->assertGreaterThan(0, count($explainedQuery)); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @expectedException Exception |
|
161 | - */ |
|
162 | - public function testExplainQueryBadQueryException() |
|
163 | - { |
|
164 | - $invalidQuery = 'SELECT * FROM `fake_table`'; |
|
165 | - $profiler = new PhpQuickProfiler(); |
|
166 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
167 | - |
|
168 | - $reflectedMethod->invokeArgs( |
|
169 | - $profiler, |
|
170 | - array(self::$dbConnection, $invalidQuery) |
|
171 | - ); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @expectedException Exception |
|
176 | - */ |
|
177 | - public function testExplainQueryBadParametersException() |
|
178 | - { |
|
179 | - $query = 'SELECT * FROM `testing` WHERE `title` = :title'; |
|
180 | - $invalidParams = array('id' => 1); |
|
181 | - $profiler = new PhpQuickProfiler(); |
|
182 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
183 | - |
|
184 | - $reflectedMethod->invokeArgs( |
|
185 | - $profiler, |
|
186 | - array(self::$dbConnection, $query, $invalidParams) |
|
187 | - ); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * @dataProvider dataConnectionDrivers |
|
192 | - */ |
|
193 | - public function testGetExplainQuery($driver, $prefix) |
|
194 | - { |
|
195 | - $query = 'SELECT * FROM `testing`'; |
|
196 | - $profiler = new PhpQuickProfiler(); |
|
197 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
198 | - |
|
199 | - $explainQuery = $reflectedMethod->invokeArgs( |
|
200 | - $profiler, |
|
201 | - array($query, $driver) |
|
202 | - ); |
|
203 | - |
|
204 | - $explainPrefix = str_replace($query, '', $explainQuery); |
|
205 | - $explainPrefix = trim($explainPrefix); |
|
206 | - $this->assertEquals($prefix, $explainPrefix); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * @expectedException Exception |
|
211 | - */ |
|
212 | - public function testGetExplainQueryUnsupportedDriver() |
|
213 | - { |
|
214 | - $query = 'SELECT * FROM `testing`'; |
|
215 | - $unsupportedDriver = 'zz'; |
|
216 | - $profiler = new PhpQuickProfiler(); |
|
217 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
218 | - |
|
219 | - $reflectedMethod->invokeArgs( |
|
220 | - $profiler, |
|
221 | - array($query, $unsupportedDriver) |
|
222 | - ); |
|
223 | - } |
|
224 | - |
|
225 | - public function testGatherSpeedData() |
|
226 | - { |
|
227 | - $elapsedTime = 1.234; |
|
228 | - $startTime = microtime(true) - $elapsedTime; |
|
229 | - $allowedTime = ini_get('max_execution_time'); |
|
230 | - $profiler = new PhpQuickProfiler($startTime); |
|
231 | - $gatheredSpeedData = $profiler->gatherSpeedData(); |
|
232 | - |
|
233 | - $this->assertInternalType('array', $gatheredSpeedData); |
|
234 | - $this->assertEquals(2, count($gatheredSpeedData)); |
|
235 | - $this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
|
236 | - $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
|
237 | - $this->assertArrayHasKey('allowed', $gatheredSpeedData); |
|
238 | - $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
|
239 | - } |
|
240 | - |
|
241 | - public function testDisplay() |
|
242 | - { |
|
243 | - $console = new Console(); |
|
244 | - $profiler = new PhpQuickProfiler(); |
|
245 | - |
|
246 | - $reflectedProfiler = new ReflectionClass(get_class($profiler)); |
|
247 | - $reflectedProperty = $reflectedProfiler->getProperty('startTime'); |
|
248 | - $reflectedProperty->setAccessible(true); |
|
249 | - $startTime = $reflectedProperty->getValue($profiler); |
|
250 | - |
|
251 | - $expectedDisplay = new Display(); |
|
252 | - $expectedDisplay->setStartTime($startTime); |
|
253 | - $expectedDisplay->setConsole($console); |
|
254 | - $expectedDisplay->setFileData($profiler->gatherFileData()); |
|
255 | - $expectedDisplay->setMemoryData($profiler->gatherMemoryData()); |
|
256 | - $expectedDisplay->setQueryData($profiler->gatherQueryData()); |
|
257 | - $expectedDisplay->setSpeedData($profiler->gatherSpeedData()); |
|
258 | - ob_start(); |
|
259 | - $expectedDisplay->__invoke(); |
|
260 | - ob_end_clean(); |
|
261 | - |
|
262 | - $display = new Display(); |
|
263 | - $profiler->setConsole($console); |
|
264 | - $profiler->setDisplay($display); |
|
265 | - ob_start(); |
|
266 | - $profiler->display(); |
|
267 | - ob_end_clean(); |
|
268 | - |
|
269 | - $this->assertAttributeEquals($expectedDisplay, 'display', $profiler); |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * @expectedException Exception |
|
274 | - */ |
|
275 | - public function testDisplayNothingSetException() |
|
276 | - { |
|
277 | - $profiler = new PhpQuickProfiler(); |
|
278 | - $profiler->display(); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * @expectedException Exception |
|
283 | - */ |
|
284 | - public function testDisplayNoConsoleException() |
|
285 | - { |
|
286 | - $display = new Display(); |
|
287 | - $profiler = new PhpQuickProfiler(); |
|
288 | - $profiler->setDisplay($display); |
|
289 | - $profiler->display(); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * @expectedException Exception |
|
294 | - */ |
|
295 | - public function testDisplayNoDisplayException() |
|
296 | - { |
|
297 | - $console = new Console(); |
|
298 | - $profiler = new PhpQuickProfiler(); |
|
299 | - $profiler->setConsole($console); |
|
300 | - $profiler->display(); |
|
301 | - } |
|
302 | - |
|
303 | - public function dataProfiledQueries() |
|
304 | - { |
|
305 | - return array( |
|
306 | - array( |
|
307 | - 'sql' => "SELECT * FROM testing", |
|
308 | - 'parameters' => array(), |
|
309 | - 'time' => 25 |
|
310 | - ), |
|
311 | - array( |
|
312 | - 'sql' => "SELECT id FROM testing WHERE title = :title", |
|
313 | - 'parameters' => array('title' => 'beta'), |
|
314 | - 'time' => 5 |
|
315 | - ) |
|
316 | - ); |
|
317 | - } |
|
318 | - |
|
319 | - public function dataConnectionDrivers() |
|
320 | - { |
|
321 | - return array( |
|
322 | - array( |
|
323 | - 'driver' => 'mysql', |
|
324 | - 'prefix' => 'EXPLAIN' |
|
325 | - ), |
|
326 | - array( |
|
327 | - 'driver' => 'sqlite', |
|
328 | - 'prefix' => 'EXPLAIN QUERY PLAN' |
|
329 | - ) |
|
330 | - ); |
|
331 | - } |
|
332 | - |
|
333 | - protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName) |
|
334 | - { |
|
335 | - $reflectedConsole = new ReflectionClass(get_class($profiler)); |
|
336 | - $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
337 | - $reflectedMethod->setAccessible(true); |
|
338 | - return $reflectedMethod; |
|
339 | - } |
|
340 | - |
|
341 | - public static function tearDownAfterClass() |
|
342 | - { |
|
343 | - self::$dbConnection = null; |
|
344 | - } |
|
31 | + self::$dbConnection->exec($hydrateTable); |
|
32 | + } |
|
33 | + |
|
34 | + public function testConstruct() |
|
35 | + { |
|
36 | + $startTime = microtime(true); |
|
37 | + |
|
38 | + $profiler = new PhpQuickProfiler(); |
|
39 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
40 | + |
|
41 | + $profiler = new PhpQuickProfiler($startTime); |
|
42 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
43 | + } |
|
44 | + |
|
45 | + public function testSetConsole() |
|
46 | + { |
|
47 | + $console = new Console(); |
|
48 | + $profiler = new PhpQuickProfiler(); |
|
49 | + $profiler->setConsole($console); |
|
50 | + |
|
51 | + $this->assertAttributeSame($console, 'console', $profiler); |
|
52 | + } |
|
53 | + |
|
54 | + public function testSetDisplay() |
|
55 | + { |
|
56 | + $display = new Display(); |
|
57 | + $profiler = new PhpQuickProfiler(); |
|
58 | + $profiler->setDisplay($display); |
|
59 | + |
|
60 | + $this->assertAttributeSame($display, 'display', $profiler); |
|
61 | + } |
|
62 | + |
|
63 | + public function testGatherFileData() |
|
64 | + { |
|
65 | + $files = get_included_files(); |
|
66 | + $profiler = new PhpQuickProfiler(); |
|
67 | + $gatheredFileData = $profiler->gatherFileData(); |
|
68 | + |
|
69 | + $this->assertInternalType('array', $gatheredFileData); |
|
70 | + $this->assertEquals(count($files), count($gatheredFileData)); |
|
71 | + foreach ($gatheredFileData as $fileData) { |
|
72 | + $this->assertInternalType('array', $fileData); |
|
73 | + $this->assertArrayHasKey('name', $fileData); |
|
74 | + $this->assertContains($fileData['name'], $files); |
|
75 | + $this->assertArrayHasKey('size', $fileData); |
|
76 | + $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + public function testGatherMemoryData() |
|
81 | + { |
|
82 | + $memoryUsage = memory_get_peak_usage(); |
|
83 | + $allowedLimit = ini_get('memory_limit'); |
|
84 | + $profiler = new PhpQuickProfiler(); |
|
85 | + $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
86 | + |
|
87 | + $this->assertInternalType('array', $gatheredMemoryData); |
|
88 | + $this->assertEquals(2, count($gatheredMemoryData)); |
|
89 | + $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
90 | + $this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
|
91 | + $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
92 | + $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
|
93 | + } |
|
94 | + |
|
95 | + public function testSetProfiledQueries() |
|
96 | + { |
|
97 | + $profiledQueries = $this->dataProfiledQueries(); |
|
98 | + $profiler = new PhpQuickProfiler(); |
|
99 | + $profiler->setProfiledQueries($profiledQueries); |
|
100 | + |
|
101 | + $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
102 | + } |
|
103 | + |
|
104 | + public function testGatherQueryData() |
|
105 | + { |
|
106 | + $profiledQueries = $this->dataProfiledQueries(); |
|
107 | + $profiledQueriesSql = array(); |
|
108 | + $profiledQueriesTime = array(); |
|
109 | + foreach ($profiledQueries as $queryData) { |
|
110 | + array_push($profiledQueriesSql, $queryData['sql']); |
|
111 | + array_push($profiledQueriesTime, $queryData['time']); |
|
112 | + } |
|
113 | + |
|
114 | + $profiler = new PhpQuickProfiler(); |
|
115 | + $profiler->setProfiledQueries($profiledQueries); |
|
116 | + $gatheredQueryData = $profiler->gatherQueryData(self::$dbConnection); |
|
117 | + |
|
118 | + $this->assertInternalType('array', $gatheredQueryData); |
|
119 | + $this->assertEquals(count($profiledQueries), count($gatheredQueryData)); |
|
120 | + foreach ($gatheredQueryData as $queryData) { |
|
121 | + $this->assertInternalType('array', $queryData); |
|
122 | + $this->assertArrayHasKey('sql', $queryData); |
|
123 | + $this->assertContains($queryData['sql'], $profiledQueriesSql); |
|
124 | + $this->assertArrayHasKey('explain', $queryData); |
|
125 | + $this->assertInternaltype('array', $queryData['explain']); |
|
126 | + $this->assertGreaterThan(0, count($queryData['explain'])); |
|
127 | + $this->assertArrayHasKey('time', $queryData); |
|
128 | + $this->assertContains($queryData['time'], $profiledQueriesTime); |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + public function testGatherQueryDataInternalProfiler() |
|
133 | + { |
|
134 | + $profiledQueries = $this->dataProfiledQueries(); |
|
135 | + $dbConnection = self::$dbConnection; |
|
136 | + $dbConnection->queries = $profiledQueries; |
|
137 | + $profiler = new PhpQuickProfiler(); |
|
138 | + $profiler->gatherQueryData($dbConnection); |
|
139 | + |
|
140 | + $this->assertAttributeSame($profiledQueries, 'profiledQueries', $profiler); |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @dataProvider dataProfiledQueries |
|
145 | + */ |
|
146 | + public function testExplainQuery($sql, $parameters) |
|
147 | + { |
|
148 | + $profiler = new PhpQuickProfiler(); |
|
149 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
150 | + |
|
151 | + $explainedQuery = $reflectedMethod->invokeArgs( |
|
152 | + $profiler, |
|
153 | + array(self::$dbConnection, $sql, $parameters) |
|
154 | + ); |
|
155 | + $this->assertInternalType('array', $explainedQuery); |
|
156 | + $this->assertGreaterThan(0, count($explainedQuery)); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @expectedException Exception |
|
161 | + */ |
|
162 | + public function testExplainQueryBadQueryException() |
|
163 | + { |
|
164 | + $invalidQuery = 'SELECT * FROM `fake_table`'; |
|
165 | + $profiler = new PhpQuickProfiler(); |
|
166 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
167 | + |
|
168 | + $reflectedMethod->invokeArgs( |
|
169 | + $profiler, |
|
170 | + array(self::$dbConnection, $invalidQuery) |
|
171 | + ); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @expectedException Exception |
|
176 | + */ |
|
177 | + public function testExplainQueryBadParametersException() |
|
178 | + { |
|
179 | + $query = 'SELECT * FROM `testing` WHERE `title` = :title'; |
|
180 | + $invalidParams = array('id' => 1); |
|
181 | + $profiler = new PhpQuickProfiler(); |
|
182 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
183 | + |
|
184 | + $reflectedMethod->invokeArgs( |
|
185 | + $profiler, |
|
186 | + array(self::$dbConnection, $query, $invalidParams) |
|
187 | + ); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * @dataProvider dataConnectionDrivers |
|
192 | + */ |
|
193 | + public function testGetExplainQuery($driver, $prefix) |
|
194 | + { |
|
195 | + $query = 'SELECT * FROM `testing`'; |
|
196 | + $profiler = new PhpQuickProfiler(); |
|
197 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
198 | + |
|
199 | + $explainQuery = $reflectedMethod->invokeArgs( |
|
200 | + $profiler, |
|
201 | + array($query, $driver) |
|
202 | + ); |
|
203 | + |
|
204 | + $explainPrefix = str_replace($query, '', $explainQuery); |
|
205 | + $explainPrefix = trim($explainPrefix); |
|
206 | + $this->assertEquals($prefix, $explainPrefix); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * @expectedException Exception |
|
211 | + */ |
|
212 | + public function testGetExplainQueryUnsupportedDriver() |
|
213 | + { |
|
214 | + $query = 'SELECT * FROM `testing`'; |
|
215 | + $unsupportedDriver = 'zz'; |
|
216 | + $profiler = new PhpQuickProfiler(); |
|
217 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
218 | + |
|
219 | + $reflectedMethod->invokeArgs( |
|
220 | + $profiler, |
|
221 | + array($query, $unsupportedDriver) |
|
222 | + ); |
|
223 | + } |
|
224 | + |
|
225 | + public function testGatherSpeedData() |
|
226 | + { |
|
227 | + $elapsedTime = 1.234; |
|
228 | + $startTime = microtime(true) - $elapsedTime; |
|
229 | + $allowedTime = ini_get('max_execution_time'); |
|
230 | + $profiler = new PhpQuickProfiler($startTime); |
|
231 | + $gatheredSpeedData = $profiler->gatherSpeedData(); |
|
232 | + |
|
233 | + $this->assertInternalType('array', $gatheredSpeedData); |
|
234 | + $this->assertEquals(2, count($gatheredSpeedData)); |
|
235 | + $this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
|
236 | + $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
|
237 | + $this->assertArrayHasKey('allowed', $gatheredSpeedData); |
|
238 | + $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
|
239 | + } |
|
240 | + |
|
241 | + public function testDisplay() |
|
242 | + { |
|
243 | + $console = new Console(); |
|
244 | + $profiler = new PhpQuickProfiler(); |
|
245 | + |
|
246 | + $reflectedProfiler = new ReflectionClass(get_class($profiler)); |
|
247 | + $reflectedProperty = $reflectedProfiler->getProperty('startTime'); |
|
248 | + $reflectedProperty->setAccessible(true); |
|
249 | + $startTime = $reflectedProperty->getValue($profiler); |
|
250 | + |
|
251 | + $expectedDisplay = new Display(); |
|
252 | + $expectedDisplay->setStartTime($startTime); |
|
253 | + $expectedDisplay->setConsole($console); |
|
254 | + $expectedDisplay->setFileData($profiler->gatherFileData()); |
|
255 | + $expectedDisplay->setMemoryData($profiler->gatherMemoryData()); |
|
256 | + $expectedDisplay->setQueryData($profiler->gatherQueryData()); |
|
257 | + $expectedDisplay->setSpeedData($profiler->gatherSpeedData()); |
|
258 | + ob_start(); |
|
259 | + $expectedDisplay->__invoke(); |
|
260 | + ob_end_clean(); |
|
261 | + |
|
262 | + $display = new Display(); |
|
263 | + $profiler->setConsole($console); |
|
264 | + $profiler->setDisplay($display); |
|
265 | + ob_start(); |
|
266 | + $profiler->display(); |
|
267 | + ob_end_clean(); |
|
268 | + |
|
269 | + $this->assertAttributeEquals($expectedDisplay, 'display', $profiler); |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * @expectedException Exception |
|
274 | + */ |
|
275 | + public function testDisplayNothingSetException() |
|
276 | + { |
|
277 | + $profiler = new PhpQuickProfiler(); |
|
278 | + $profiler->display(); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * @expectedException Exception |
|
283 | + */ |
|
284 | + public function testDisplayNoConsoleException() |
|
285 | + { |
|
286 | + $display = new Display(); |
|
287 | + $profiler = new PhpQuickProfiler(); |
|
288 | + $profiler->setDisplay($display); |
|
289 | + $profiler->display(); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * @expectedException Exception |
|
294 | + */ |
|
295 | + public function testDisplayNoDisplayException() |
|
296 | + { |
|
297 | + $console = new Console(); |
|
298 | + $profiler = new PhpQuickProfiler(); |
|
299 | + $profiler->setConsole($console); |
|
300 | + $profiler->display(); |
|
301 | + } |
|
302 | + |
|
303 | + public function dataProfiledQueries() |
|
304 | + { |
|
305 | + return array( |
|
306 | + array( |
|
307 | + 'sql' => "SELECT * FROM testing", |
|
308 | + 'parameters' => array(), |
|
309 | + 'time' => 25 |
|
310 | + ), |
|
311 | + array( |
|
312 | + 'sql' => "SELECT id FROM testing WHERE title = :title", |
|
313 | + 'parameters' => array('title' => 'beta'), |
|
314 | + 'time' => 5 |
|
315 | + ) |
|
316 | + ); |
|
317 | + } |
|
318 | + |
|
319 | + public function dataConnectionDrivers() |
|
320 | + { |
|
321 | + return array( |
|
322 | + array( |
|
323 | + 'driver' => 'mysql', |
|
324 | + 'prefix' => 'EXPLAIN' |
|
325 | + ), |
|
326 | + array( |
|
327 | + 'driver' => 'sqlite', |
|
328 | + 'prefix' => 'EXPLAIN QUERY PLAN' |
|
329 | + ) |
|
330 | + ); |
|
331 | + } |
|
332 | + |
|
333 | + protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName) |
|
334 | + { |
|
335 | + $reflectedConsole = new ReflectionClass(get_class($profiler)); |
|
336 | + $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
337 | + $reflectedMethod->setAccessible(true); |
|
338 | + return $reflectedMethod; |
|
339 | + } |
|
340 | + |
|
341 | + public static function tearDownAfterClass() |
|
342 | + { |
|
343 | + self::$dbConnection = null; |
|
344 | + } |
|
345 | 345 | } |
@@ -14,371 +14,371 @@ |
||
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 | - $unit = 'ms'; |
|
295 | - } elseif ($time > 60) { |
|
296 | - $time /= 60; |
|
297 | - $unit = 'm'; |
|
298 | - } |
|
299 | - $time = number_format($time, $percision); |
|
300 | - return "{$time} {$unit}"; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Formatter for human-readable memory |
|
305 | - * Only handles time up to a few gigs gracefully |
|
306 | - * |
|
307 | - * @param double $size |
|
308 | - * @param integer $percision |
|
309 | - */ |
|
310 | - protected function getReadableMemory($size, $percision = 2) |
|
311 | - { |
|
312 | - $unitOptions = array('b', 'k', 'M', 'G'); |
|
313 | - |
|
314 | - $base = log($size, 1024); |
|
315 | - |
|
316 | - $memory = round(pow(1024, $base - floor($base)), $percision); |
|
317 | - $unit = $unitOptions[floor($base)]; |
|
318 | - return "{$memory} {$unit}"; |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * @param array $messages |
|
323 | - * @param string $type |
|
324 | - * @return array |
|
325 | - */ |
|
326 | - protected function filterMessages($messages, $type) |
|
327 | - { |
|
328 | - return array_filter($messages, function ($message) use ($type) { |
|
329 | - return $message['type'] == $type; |
|
330 | - }); |
|
331 | - } |
|
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 | + $unit = 'ms'; |
|
295 | + } elseif ($time > 60) { |
|
296 | + $time /= 60; |
|
297 | + $unit = 'm'; |
|
298 | + } |
|
299 | + $time = number_format($time, $percision); |
|
300 | + return "{$time} {$unit}"; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Formatter for human-readable memory |
|
305 | + * Only handles time up to a few gigs gracefully |
|
306 | + * |
|
307 | + * @param double $size |
|
308 | + * @param integer $percision |
|
309 | + */ |
|
310 | + protected function getReadableMemory($size, $percision = 2) |
|
311 | + { |
|
312 | + $unitOptions = array('b', 'k', 'M', 'G'); |
|
313 | + |
|
314 | + $base = log($size, 1024); |
|
315 | + |
|
316 | + $memory = round(pow(1024, $base - floor($base)), $percision); |
|
317 | + $unit = $unitOptions[floor($base)]; |
|
318 | + return "{$memory} {$unit}"; |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * @param array $messages |
|
323 | + * @param string $type |
|
324 | + * @return array |
|
325 | + */ |
|
326 | + protected function filterMessages($messages, $type) |
|
327 | + { |
|
328 | + return array_filter($messages, function ($message) use ($type) { |
|
329 | + return $message['type'] == $type; |
|
330 | + }); |
|
331 | + } |
|
332 | 332 | |
333 | - public function __invoke() |
|
334 | - { |
|
335 | - $consoleMeta = $this->getConsoleMeta(); |
|
336 | - $speedMeta = $this->getSpeedMeta(); |
|
337 | - $queryMeta = $this->getQueryMeta(); |
|
338 | - $memoryMeta = $this->getMemoryMeta(); |
|
339 | - $fileMeta = $this->getFileMeta(); |
|
340 | - |
|
341 | - $header = array( |
|
342 | - 'console' => array_sum($consoleMeta), |
|
343 | - 'speed' => $speedMeta['elapsed'], |
|
344 | - 'query' => $queryMeta['count'], |
|
345 | - 'memory' => $memoryMeta['used'], |
|
346 | - 'files' => $fileMeta['count'] |
|
347 | - ); |
|
348 | - |
|
349 | - $consoleMessages = $this->getConsoleMessages(); |
|
350 | - $queryList = $this->getQueryList(); |
|
351 | - $fileList = $this->getFileList(); |
|
352 | - |
|
353 | - $console = array( |
|
354 | - 'meta' => $consoleMeta, |
|
355 | - 'messages' => $consoleMessages |
|
356 | - ); |
|
357 | - |
|
358 | - $speed = array( |
|
359 | - 'meta' => $speedMeta, |
|
360 | - 'messages' => $this->filterMessages($consoleMessages, 'speed') |
|
361 | - ); |
|
362 | - |
|
363 | - $query = array( |
|
364 | - 'meta' => $queryMeta, |
|
365 | - 'messages' => $queryList |
|
366 | - ); |
|
367 | - |
|
368 | - $memory = array( |
|
369 | - 'meta' => $memoryMeta, |
|
370 | - 'messages' => $this->filterMessages($consoleMessages, 'memory') |
|
371 | - ); |
|
372 | - |
|
373 | - $files = array( |
|
374 | - 'meta' => $fileMeta, |
|
375 | - 'messages' => $fileList |
|
376 | - ); |
|
377 | - |
|
378 | - // todo is this really the best way to load these? |
|
379 | - $styles = file_get_contents(__DIR__ . "/../{$this->options['style_path']}"); |
|
380 | - $script = file_get_contents(__DIR__ . "/../{$this->options['script_path']}"); |
|
381 | - |
|
382 | - require_once __DIR__ .'/../asset/display.html'; |
|
383 | - } |
|
333 | + public function __invoke() |
|
334 | + { |
|
335 | + $consoleMeta = $this->getConsoleMeta(); |
|
336 | + $speedMeta = $this->getSpeedMeta(); |
|
337 | + $queryMeta = $this->getQueryMeta(); |
|
338 | + $memoryMeta = $this->getMemoryMeta(); |
|
339 | + $fileMeta = $this->getFileMeta(); |
|
340 | + |
|
341 | + $header = array( |
|
342 | + 'console' => array_sum($consoleMeta), |
|
343 | + 'speed' => $speedMeta['elapsed'], |
|
344 | + 'query' => $queryMeta['count'], |
|
345 | + 'memory' => $memoryMeta['used'], |
|
346 | + 'files' => $fileMeta['count'] |
|
347 | + ); |
|
348 | + |
|
349 | + $consoleMessages = $this->getConsoleMessages(); |
|
350 | + $queryList = $this->getQueryList(); |
|
351 | + $fileList = $this->getFileList(); |
|
352 | + |
|
353 | + $console = array( |
|
354 | + 'meta' => $consoleMeta, |
|
355 | + 'messages' => $consoleMessages |
|
356 | + ); |
|
357 | + |
|
358 | + $speed = array( |
|
359 | + 'meta' => $speedMeta, |
|
360 | + 'messages' => $this->filterMessages($consoleMessages, 'speed') |
|
361 | + ); |
|
362 | + |
|
363 | + $query = array( |
|
364 | + 'meta' => $queryMeta, |
|
365 | + 'messages' => $queryList |
|
366 | + ); |
|
367 | + |
|
368 | + $memory = array( |
|
369 | + 'meta' => $memoryMeta, |
|
370 | + 'messages' => $this->filterMessages($consoleMessages, 'memory') |
|
371 | + ); |
|
372 | + |
|
373 | + $files = array( |
|
374 | + 'meta' => $fileMeta, |
|
375 | + 'messages' => $fileList |
|
376 | + ); |
|
377 | + |
|
378 | + // todo is this really the best way to load these? |
|
379 | + $styles = file_get_contents(__DIR__ . "/../{$this->options['style_path']}"); |
|
380 | + $script = file_get_contents(__DIR__ . "/../{$this->options['script_path']}"); |
|
381 | + |
|
382 | + require_once __DIR__ .'/../asset/display.html'; |
|
383 | + } |
|
384 | 384 | } |
@@ -5,43 +5,43 @@ |
||
5 | 5 | // namespace hack on microtime functionality |
6 | 6 | function microtime() |
7 | 7 | { |
8 | - return 1450355136.5706; |
|
8 | + return 1450355136.5706; |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | // namespace hack on included files functionality |
12 | 12 | function get_included_files() |
13 | 13 | { |
14 | - return array( |
|
15 | - 'index.php', |
|
16 | - 'src/Class.php' |
|
17 | - ); |
|
14 | + return array( |
|
15 | + 'index.php', |
|
16 | + 'src/Class.php' |
|
17 | + ); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | // namespace hack on filesize |
21 | 21 | function filesize($filename) |
22 | 22 | { |
23 | - return strlen($filename) * 100; |
|
23 | + return strlen($filename) * 100; |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | // namespace hack on memory usage |
27 | 27 | function memory_get_usage() |
28 | 28 | { |
29 | - return 12345678; |
|
29 | + return 12345678; |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | // namespace hack on memory usage |
33 | 33 | function memory_get_peak_usage() |
34 | 34 | { |
35 | - return 123456789; |
|
35 | + return 123456789; |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | // namespace hack on ini settings |
39 | 39 | function ini_get($setting) |
40 | 40 | { |
41 | - if ($setting == 'memory_limit') { |
|
42 | - return '128M'; |
|
43 | - } elseif ($setting == 'max_execution_time') { |
|
44 | - return '30'; |
|
45 | - } |
|
46 | - return \ini_get($setting); |
|
41 | + if ($setting == 'memory_limit') { |
|
42 | + return '128M'; |
|
43 | + } elseif ($setting == 'max_execution_time') { |
|
44 | + return '30'; |
|
45 | + } |
|
46 | + return \ini_get($setting); |
|
47 | 47 | } |
@@ -9,220 +9,220 @@ |
||
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 | - public function testGetConsoleMeta() |
|
91 | - { |
|
92 | - $expectedMeta = array( |
|
93 | - 'log' => 1, |
|
94 | - 'memory' => 0, |
|
95 | - 'error' => 0, |
|
96 | - 'speed' => 2 |
|
97 | - ); |
|
98 | - $console = new Console(); |
|
99 | - $console->log('testing words'); |
|
100 | - $console->logSpeed('now'); |
|
101 | - $console->logSpeed(); |
|
102 | - $display = new Display(); |
|
103 | - $display->setConsole($console); |
|
104 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMeta'); |
|
105 | - |
|
106 | - $consoleMeta = $reflectedMethod->invoke($display); |
|
107 | - $this->assertEquals($expectedMeta, $consoleMeta); |
|
108 | - } |
|
109 | - |
|
110 | - public function testGetConsoleMessages() |
|
111 | - { |
|
112 | - $console = new Console(); |
|
113 | - $testLog = 'testing more words'; |
|
114 | - $console->log($testLog); |
|
115 | - $console->logMemory(); |
|
116 | - $testException = new Exception('test exception'); |
|
117 | - $console->logError($testException); |
|
118 | - $console->logSpeed(); |
|
119 | - $display = new Display(); |
|
120 | - $display->setConsole($console); |
|
121 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMessages'); |
|
122 | - |
|
123 | - $consoleMessages = $reflectedMethod->invoke($display); |
|
124 | - foreach ($consoleMessages as $message) { |
|
125 | - $this->assertArrayHasKey('message', $message); |
|
126 | - $this->assertInternalType('string', $message['message']); |
|
127 | - $this->assertArrayHasKey('type', $message); |
|
128 | - $this->assertInternalType('string', $message['type']); |
|
129 | - switch ($message['type']) { |
|
130 | - case 'log': |
|
131 | - $expectedMessage = print_r($testLog, true); |
|
132 | - $this->assertEquals($expectedMessage, $message['message']); |
|
133 | - break; |
|
134 | - case 'memory': |
|
135 | - $expectedMessage = 'PHP'; |
|
136 | - $this->assertEquals($expectedMessage, $message['message']); |
|
137 | - $this->assertArrayHasKey('data', $message); |
|
138 | - $this->assertInternalType('string', $message['data']); |
|
139 | - $expectedData = memory_get_usage(); |
|
140 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
141 | - $expectedData = $reflectedMethod->invokeArgs($display, array($expectedData)); |
|
142 | - $this->assertEquals($expectedData, $message['data']); |
|
143 | - break; |
|
144 | - case 'error': |
|
145 | - $expectedMessage = sprintf( |
|
146 | - "Line %s: %s in %s", |
|
147 | - $testException->getLine(), |
|
148 | - $testException->getMessage(), |
|
149 | - $testException->getFile() |
|
150 | - ); |
|
151 | - $this->assertEquals($expectedMessage, $message['message']); |
|
152 | - break; |
|
153 | - case 'speed': |
|
154 | - $expectedMessage = 'Point in Time'; |
|
155 | - $this->assertEquals($expectedMessage, $message['message']); |
|
156 | - $this->assertArrayHasKey('data', $message); |
|
157 | - $this->assertInternalType('string', $message['data']); |
|
158 | - $expectedData = microtime(true); |
|
159 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
160 | - $expectedData = $reflectedMethod->invokeArgs($display, array($expectedData)); |
|
161 | - $this->assertEquals($expectedData, $message['data']); |
|
162 | - break; |
|
163 | - } |
|
164 | - } |
|
165 | - } |
|
166 | - |
|
167 | - public function testGetSpeedMeta() |
|
168 | - { |
|
169 | - $elapsedTime = 1234.678; |
|
170 | - $allowedTime = 30; |
|
171 | - $display = new Display(); |
|
172 | - $display->setSpeedData(array( |
|
173 | - 'elapsed' => $elapsedTime, |
|
174 | - 'allowed' => $allowedTime |
|
175 | - )); |
|
176 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
177 | - $elapsedTime = $reflectedMethod->invokeArgs($display, array($elapsedTime)); |
|
178 | - $allowedTime = $reflectedMethod->invokeArgs($display, array($allowedTime, 0)); |
|
179 | - $expectedMeta = array( |
|
180 | - 'elapsed' => $elapsedTime, |
|
181 | - 'allowed' => $allowedTime |
|
182 | - ); |
|
183 | - |
|
184 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getSpeedMeta'); |
|
185 | - $speedMeta = $reflectedMethod->invoke($display); |
|
186 | - $this->assertEquals($expectedMeta, $speedMeta); |
|
187 | - } |
|
188 | - |
|
189 | - public function testGetReadableTime() |
|
190 | - { |
|
191 | - $timeTest = array( |
|
192 | - '.032432' => '32.432 ms', |
|
193 | - '24.3781' => '24.378 s', |
|
194 | - '145.123' => '2.419 m' |
|
195 | - ); |
|
196 | - $display = new Display(); |
|
197 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
198 | - |
|
199 | - foreach ($timeTest as $rawTime => $expectedTime) { |
|
200 | - $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime)); |
|
201 | - $this->assertEquals($expectedTime, $readableTime); |
|
202 | - } |
|
203 | - } |
|
204 | - |
|
205 | - public function testGetReadableMemory() |
|
206 | - { |
|
207 | - $memoryTest = array( |
|
208 | - '314' => '314 b', |
|
209 | - '7403' => '7.23 k', |
|
210 | - '2589983' => '2.47 M' |
|
211 | - ); |
|
212 | - $display = new Display(); |
|
213 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
214 | - |
|
215 | - foreach ($memoryTest as $rawMemory => $expectedMemory) { |
|
216 | - $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory)); |
|
217 | - $this->assertEquals($expectedMemory, $readableMemory); |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - protected function getAccessibleMethod(Display $display, $methodName) |
|
222 | - { |
|
223 | - $reflectedConsole = new ReflectionClass(get_class($display)); |
|
224 | - $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
225 | - $reflectedMethod->setAccessible(true); |
|
226 | - return $reflectedMethod; |
|
227 | - } |
|
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 | + public function testGetConsoleMeta() |
|
91 | + { |
|
92 | + $expectedMeta = array( |
|
93 | + 'log' => 1, |
|
94 | + 'memory' => 0, |
|
95 | + 'error' => 0, |
|
96 | + 'speed' => 2 |
|
97 | + ); |
|
98 | + $console = new Console(); |
|
99 | + $console->log('testing words'); |
|
100 | + $console->logSpeed('now'); |
|
101 | + $console->logSpeed(); |
|
102 | + $display = new Display(); |
|
103 | + $display->setConsole($console); |
|
104 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMeta'); |
|
105 | + |
|
106 | + $consoleMeta = $reflectedMethod->invoke($display); |
|
107 | + $this->assertEquals($expectedMeta, $consoleMeta); |
|
108 | + } |
|
109 | + |
|
110 | + public function testGetConsoleMessages() |
|
111 | + { |
|
112 | + $console = new Console(); |
|
113 | + $testLog = 'testing more words'; |
|
114 | + $console->log($testLog); |
|
115 | + $console->logMemory(); |
|
116 | + $testException = new Exception('test exception'); |
|
117 | + $console->logError($testException); |
|
118 | + $console->logSpeed(); |
|
119 | + $display = new Display(); |
|
120 | + $display->setConsole($console); |
|
121 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getConsoleMessages'); |
|
122 | + |
|
123 | + $consoleMessages = $reflectedMethod->invoke($display); |
|
124 | + foreach ($consoleMessages as $message) { |
|
125 | + $this->assertArrayHasKey('message', $message); |
|
126 | + $this->assertInternalType('string', $message['message']); |
|
127 | + $this->assertArrayHasKey('type', $message); |
|
128 | + $this->assertInternalType('string', $message['type']); |
|
129 | + switch ($message['type']) { |
|
130 | + case 'log': |
|
131 | + $expectedMessage = print_r($testLog, true); |
|
132 | + $this->assertEquals($expectedMessage, $message['message']); |
|
133 | + break; |
|
134 | + case 'memory': |
|
135 | + $expectedMessage = 'PHP'; |
|
136 | + $this->assertEquals($expectedMessage, $message['message']); |
|
137 | + $this->assertArrayHasKey('data', $message); |
|
138 | + $this->assertInternalType('string', $message['data']); |
|
139 | + $expectedData = memory_get_usage(); |
|
140 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
141 | + $expectedData = $reflectedMethod->invokeArgs($display, array($expectedData)); |
|
142 | + $this->assertEquals($expectedData, $message['data']); |
|
143 | + break; |
|
144 | + case 'error': |
|
145 | + $expectedMessage = sprintf( |
|
146 | + "Line %s: %s in %s", |
|
147 | + $testException->getLine(), |
|
148 | + $testException->getMessage(), |
|
149 | + $testException->getFile() |
|
150 | + ); |
|
151 | + $this->assertEquals($expectedMessage, $message['message']); |
|
152 | + break; |
|
153 | + case 'speed': |
|
154 | + $expectedMessage = 'Point in Time'; |
|
155 | + $this->assertEquals($expectedMessage, $message['message']); |
|
156 | + $this->assertArrayHasKey('data', $message); |
|
157 | + $this->assertInternalType('string', $message['data']); |
|
158 | + $expectedData = microtime(true); |
|
159 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
160 | + $expectedData = $reflectedMethod->invokeArgs($display, array($expectedData)); |
|
161 | + $this->assertEquals($expectedData, $message['data']); |
|
162 | + break; |
|
163 | + } |
|
164 | + } |
|
165 | + } |
|
166 | + |
|
167 | + public function testGetSpeedMeta() |
|
168 | + { |
|
169 | + $elapsedTime = 1234.678; |
|
170 | + $allowedTime = 30; |
|
171 | + $display = new Display(); |
|
172 | + $display->setSpeedData(array( |
|
173 | + 'elapsed' => $elapsedTime, |
|
174 | + 'allowed' => $allowedTime |
|
175 | + )); |
|
176 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
177 | + $elapsedTime = $reflectedMethod->invokeArgs($display, array($elapsedTime)); |
|
178 | + $allowedTime = $reflectedMethod->invokeArgs($display, array($allowedTime, 0)); |
|
179 | + $expectedMeta = array( |
|
180 | + 'elapsed' => $elapsedTime, |
|
181 | + 'allowed' => $allowedTime |
|
182 | + ); |
|
183 | + |
|
184 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getSpeedMeta'); |
|
185 | + $speedMeta = $reflectedMethod->invoke($display); |
|
186 | + $this->assertEquals($expectedMeta, $speedMeta); |
|
187 | + } |
|
188 | + |
|
189 | + public function testGetReadableTime() |
|
190 | + { |
|
191 | + $timeTest = array( |
|
192 | + '.032432' => '32.432 ms', |
|
193 | + '24.3781' => '24.378 s', |
|
194 | + '145.123' => '2.419 m' |
|
195 | + ); |
|
196 | + $display = new Display(); |
|
197 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
198 | + |
|
199 | + foreach ($timeTest as $rawTime => $expectedTime) { |
|
200 | + $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime)); |
|
201 | + $this->assertEquals($expectedTime, $readableTime); |
|
202 | + } |
|
203 | + } |
|
204 | + |
|
205 | + public function testGetReadableMemory() |
|
206 | + { |
|
207 | + $memoryTest = array( |
|
208 | + '314' => '314 b', |
|
209 | + '7403' => '7.23 k', |
|
210 | + '2589983' => '2.47 M' |
|
211 | + ); |
|
212 | + $display = new Display(); |
|
213 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
214 | + |
|
215 | + foreach ($memoryTest as $rawMemory => $expectedMemory) { |
|
216 | + $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory)); |
|
217 | + $this->assertEquals($expectedMemory, $readableMemory); |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + protected function getAccessibleMethod(Display $display, $methodName) |
|
222 | + { |
|
223 | + $reflectedConsole = new ReflectionClass(get_class($display)); |
|
224 | + $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
225 | + $reflectedMethod->setAccessible(true); |
|
226 | + return $reflectedMethod; |
|
227 | + } |
|
228 | 228 | } |