@@ -17,186 +17,186 @@ |
||
17 | 17 | class PhpQuickProfiler |
18 | 18 | { |
19 | 19 | |
20 | - /** @var integer */ |
|
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 $profiled_queries |
|
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) |
|
107 | - { |
|
108 | - if (empty($this->profiledQueries) && property_exists($dbConnection, 'queries')) { |
|
109 | - $this->setProfiledQueries($dbConnection->queries); |
|
110 | - } |
|
111 | - |
|
112 | - $data = array(); |
|
113 | - foreach ($this->profiledQueries as $query) { |
|
114 | - array_push($data, array( |
|
115 | - 'sql' => $query['sql'], |
|
116 | - 'explain' => $this->explainQuery($dbConnection, $query['sql'], $query['parameters']), |
|
117 | - 'time' => $query['time'] |
|
118 | - )); |
|
119 | - } |
|
120 | - return $data; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Attempts to explain a query |
|
125 | - * |
|
126 | - * @param object $dbConnection |
|
127 | - * @param string $query |
|
128 | - * @param array $parameters |
|
129 | - * @throws Exception |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - protected function explainQuery($dbConnection, $query, $parameters = array()) |
|
133 | - { |
|
134 | - $driver = $dbConnection->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
135 | - $query = $this->getExplainQuery($query, $driver); |
|
136 | - $statement = $dbConnection->prepare($query); |
|
137 | - if ($statement === false) { |
|
138 | - throw new Exception('Invalid query passed to explainQuery method'); |
|
139 | - } |
|
140 | - $statement->execute($parameters); |
|
141 | - $result = $statement->fetch(\PDO::FETCH_ASSOC); |
|
142 | - if ($result === false) { |
|
143 | - throw new Exception('Query could not be explained with given parameters'); |
|
144 | - } |
|
145 | - return $result; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Attempts to figure out what kind of explain query format the db wants |
|
150 | - * |
|
151 | - * @param string $query |
|
152 | - * @param string $driver |
|
153 | - * @throws Exception |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - protected function getExplainQuery($query, $driver) |
|
157 | - { |
|
158 | - if ($driver == 'mysql') { |
|
159 | - return "EXPLAIN {$query}"; |
|
160 | - } elseif ($driver == 'sqlite') { |
|
161 | - return "EXPLAIN QUERY PLAN {$query}"; |
|
162 | - } |
|
163 | - throw new Exception('Could not process db driver'); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Get data about speed of the application |
|
168 | - * |
|
169 | - * @returns array |
|
170 | - */ |
|
171 | - public function gatherSpeedData() |
|
172 | - { |
|
173 | - $elapsedTime = microtime(true) - $this->startTime; |
|
174 | - $elapsedTime = round($elapsedTime, 3); |
|
175 | - $allowedTime = ini_get('max_execution_time'); |
|
176 | - return array( |
|
177 | - 'elapsed' => $elapsedTime, |
|
178 | - 'allowed' => $allowedTime |
|
179 | - ); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Triggers end display of the profiling data |
|
184 | - * |
|
185 | - * @param object $dbConnection |
|
186 | - * @throws Exception |
|
187 | - */ |
|
188 | - public function display($dbConnection = null) |
|
189 | - { |
|
190 | - if (!isset($this->display)) { |
|
191 | - throw new Exception('Display object has not been injected into Profiler'); |
|
192 | - } |
|
193 | - |
|
194 | - $this->display->setConsole($this->console); |
|
195 | - $this->display->setFileData($this->gatherFileData()); |
|
196 | - $this->display->setMemoryData($this->gatherMemoryData()); |
|
197 | - $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
198 | - $this->display->setSpeedData($this->gatherSpeedData()); |
|
199 | - |
|
200 | - $this->display->__invoke(); |
|
201 | - } |
|
20 | + /** @var integer */ |
|
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 $profiled_queries |
|
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) |
|
107 | + { |
|
108 | + if (empty($this->profiledQueries) && property_exists($dbConnection, 'queries')) { |
|
109 | + $this->setProfiledQueries($dbConnection->queries); |
|
110 | + } |
|
111 | + |
|
112 | + $data = array(); |
|
113 | + foreach ($this->profiledQueries as $query) { |
|
114 | + array_push($data, array( |
|
115 | + 'sql' => $query['sql'], |
|
116 | + 'explain' => $this->explainQuery($dbConnection, $query['sql'], $query['parameters']), |
|
117 | + 'time' => $query['time'] |
|
118 | + )); |
|
119 | + } |
|
120 | + return $data; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Attempts to explain a query |
|
125 | + * |
|
126 | + * @param object $dbConnection |
|
127 | + * @param string $query |
|
128 | + * @param array $parameters |
|
129 | + * @throws Exception |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + protected function explainQuery($dbConnection, $query, $parameters = array()) |
|
133 | + { |
|
134 | + $driver = $dbConnection->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
135 | + $query = $this->getExplainQuery($query, $driver); |
|
136 | + $statement = $dbConnection->prepare($query); |
|
137 | + if ($statement === false) { |
|
138 | + throw new Exception('Invalid query passed to explainQuery method'); |
|
139 | + } |
|
140 | + $statement->execute($parameters); |
|
141 | + $result = $statement->fetch(\PDO::FETCH_ASSOC); |
|
142 | + if ($result === false) { |
|
143 | + throw new Exception('Query could not be explained with given parameters'); |
|
144 | + } |
|
145 | + return $result; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Attempts to figure out what kind of explain query format the db wants |
|
150 | + * |
|
151 | + * @param string $query |
|
152 | + * @param string $driver |
|
153 | + * @throws Exception |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + protected function getExplainQuery($query, $driver) |
|
157 | + { |
|
158 | + if ($driver == 'mysql') { |
|
159 | + return "EXPLAIN {$query}"; |
|
160 | + } elseif ($driver == 'sqlite') { |
|
161 | + return "EXPLAIN QUERY PLAN {$query}"; |
|
162 | + } |
|
163 | + throw new Exception('Could not process db driver'); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Get data about speed of the application |
|
168 | + * |
|
169 | + * @returns array |
|
170 | + */ |
|
171 | + public function gatherSpeedData() |
|
172 | + { |
|
173 | + $elapsedTime = microtime(true) - $this->startTime; |
|
174 | + $elapsedTime = round($elapsedTime, 3); |
|
175 | + $allowedTime = ini_get('max_execution_time'); |
|
176 | + return array( |
|
177 | + 'elapsed' => $elapsedTime, |
|
178 | + 'allowed' => $allowedTime |
|
179 | + ); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Triggers end display of the profiling data |
|
184 | + * |
|
185 | + * @param object $dbConnection |
|
186 | + * @throws Exception |
|
187 | + */ |
|
188 | + public function display($dbConnection = null) |
|
189 | + { |
|
190 | + if (!isset($this->display)) { |
|
191 | + throw new Exception('Display object has not been injected into Profiler'); |
|
192 | + } |
|
193 | + |
|
194 | + $this->display->setConsole($this->console); |
|
195 | + $this->display->setFileData($this->gatherFileData()); |
|
196 | + $this->display->setMemoryData($this->gatherMemoryData()); |
|
197 | + $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
198 | + $this->display->setSpeedData($this->gatherSpeedData()); |
|
199 | + |
|
200 | + $this->display->__invoke(); |
|
201 | + } |
|
202 | 202 | } |
@@ -10,19 +10,19 @@ discard block |
||
10 | 10 | class PhpQuickProfilerTest extends PHPUnit_Framework_TestCase |
11 | 11 | { |
12 | 12 | |
13 | - protected static $dbConnection; |
|
13 | + protected static $dbConnection; |
|
14 | 14 | |
15 | - public static function setUpBeforeClass() |
|
16 | - { |
|
17 | - self::$dbConnection = new PDO('sqlite::memory:'); |
|
18 | - $createTable = " |
|
15 | + public static function setUpBeforeClass() |
|
16 | + { |
|
17 | + self::$dbConnection = new PDO('sqlite::memory:'); |
|
18 | + $createTable = " |
|
19 | 19 | CREATE TABLE IF NOT EXISTS `testing` ( |
20 | 20 | `id` integer PRIMARY KEY AUTOINCREMENT, |
21 | 21 | `title` varchar(60) NOT NULL |
22 | 22 | );"; |
23 | - self::$dbConnection->exec($createTable); |
|
23 | + self::$dbConnection->exec($createTable); |
|
24 | 24 | |
25 | - $hydrateTable = " |
|
25 | + $hydrateTable = " |
|
26 | 26 | INSERT INTO `testing` |
27 | 27 | (`title`) |
28 | 28 | VALUES |
@@ -30,245 +30,245 @@ discard block |
||
30 | 30 | ('beta'), |
31 | 31 | ('charlie'), |
32 | 32 | ('delta');"; |
33 | - self::$dbConnection->exec($hydrateTable); |
|
34 | - } |
|
35 | - |
|
36 | - public function testConstruct() |
|
37 | - { |
|
38 | - $startTime = microtime(true); |
|
39 | - |
|
40 | - $profiler = new PhpQuickProfiler(); |
|
41 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
42 | - |
|
43 | - $profiler = new PhpQuickProfiler($startTime); |
|
44 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
45 | - } |
|
46 | - |
|
47 | - public function testSetConsole() |
|
48 | - { |
|
49 | - $console = new Console(); |
|
50 | - $profiler = new PhpQuickProfiler(); |
|
51 | - $profiler->setConsole($console); |
|
52 | - |
|
53 | - $this->assertAttributeSame($console, 'console', $profiler); |
|
54 | - } |
|
55 | - |
|
56 | - public function testSetDisplay() |
|
57 | - { |
|
58 | - $display = new Display(); |
|
59 | - $profiler = new PhpQuickProfiler(); |
|
60 | - $profiler->setDisplay($display); |
|
61 | - |
|
62 | - $this->assertAttributeSame($display, 'display', $profiler); |
|
63 | - } |
|
64 | - |
|
65 | - public function testGatherFileData() |
|
66 | - { |
|
67 | - $files = get_included_files(); |
|
68 | - $profiler = new PhpQuickProfiler(); |
|
69 | - $gatheredFileData = $profiler->gatherFileData(); |
|
70 | - |
|
71 | - $this->assertInternalType('array', $gatheredFileData); |
|
72 | - $this->assertEquals(count($files), count($gatheredFileData)); |
|
73 | - foreach ($gatheredFileData as $fileData) { |
|
74 | - $this->assertInternalType('array', $fileData); |
|
75 | - $this->assertArrayHasKey('name', $fileData); |
|
76 | - $this->assertContains($fileData['name'], $files); |
|
77 | - $this->assertArrayHasKey('size', $fileData); |
|
78 | - $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - public function testGatherMemoryData() |
|
83 | - { |
|
84 | - $memoryUsage = memory_get_peak_usage(); |
|
85 | - $allowedLimit = ini_get('memory_limit'); |
|
86 | - $profiler = new PhpQuickProfiler(); |
|
87 | - $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
88 | - |
|
89 | - $this->assertInternalType('array', $gatheredMemoryData); |
|
90 | - $this->assertEquals(2, count($gatheredMemoryData)); |
|
91 | - $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
92 | - $this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
|
93 | - $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
94 | - $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
|
95 | - } |
|
96 | - |
|
97 | - public function testSetProfiledQueries() |
|
98 | - { |
|
99 | - $profiledQueries = $this->dataProfiledQueries(); |
|
100 | - $profiler = new PhpQuickProfiler(); |
|
101 | - $profiler->setProfiledQueries($profiledQueries); |
|
102 | - |
|
103 | - $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
104 | - } |
|
105 | - |
|
106 | - public function testGatherQueryData() |
|
107 | - { |
|
108 | - $profiledQueries = $this->dataProfiledQueries(); |
|
109 | - $profiledQueriesSql = array(); |
|
110 | - $profiledQueriesTime = array(); |
|
111 | - foreach ($profiledQueries as $queryData) { |
|
112 | - array_push($profiledQueriesSql, $queryData['sql']); |
|
113 | - array_push($profiledQueriesTime, $queryData['time']); |
|
114 | - } |
|
115 | - |
|
116 | - $profiler = new PhpQuickProfiler(); |
|
117 | - $profiler->setProfiledQueries($profiledQueries); |
|
118 | - $gatheredQueryData = $profiler->gatherQueryData(self::$dbConnection); |
|
119 | - |
|
120 | - $this->assertInternalType('array', $gatheredQueryData); |
|
121 | - $this->assertEquals(count($profiledQueries), count($gatheredQueryData)); |
|
122 | - foreach ($gatheredQueryData as $queryData) { |
|
123 | - $this->assertInternalType('array', $queryData); |
|
124 | - $this->assertArrayHasKey('sql', $queryData); |
|
125 | - $this->assertContains($queryData['sql'], $profiledQueriesSql); |
|
126 | - $this->assertArrayHasKey('explain', $queryData); |
|
127 | - $this->assertInternaltype('array', $queryData['explain']); |
|
128 | - $this->assertGreaterThan(0, count($queryData['explain'])); |
|
129 | - $this->assertArrayHasKey('time', $queryData); |
|
130 | - $this->assertContains($queryData['time'], $profiledQueriesTime); |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @dataProvider dataProfiledQueries |
|
136 | - */ |
|
137 | - public function testExplainQuery($sql, $parameters) |
|
138 | - { |
|
139 | - $profiler = new PhpQuickProfiler(); |
|
140 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
141 | - |
|
142 | - $explainedQuery = $reflectedMethod->invokeArgs( |
|
143 | - $profiler, |
|
144 | - array(self::$dbConnection, $sql, $parameters) |
|
145 | - ); |
|
146 | - $this->assertInternalType('array', $explainedQuery); |
|
147 | - $this->assertGreaterThan(0, count($explainedQuery)); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @expectedException Exception |
|
152 | - */ |
|
153 | - public function testExplainQueryBadQueryException() |
|
154 | - { |
|
155 | - $invalidQuery = 'SELECT * FROM `fake_table`'; |
|
156 | - $profiler = new PhpQuickProfiler(); |
|
157 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
158 | - |
|
159 | - $reflectedMethod->invokeArgs( |
|
160 | - $profiler, |
|
161 | - array(self::$dbConnection, $invalidQuery) |
|
162 | - ); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @expectedException Exception |
|
167 | - */ |
|
168 | - public function testExplainQueryBadParametersException() |
|
169 | - { |
|
170 | - $query = 'SELECT * FROM `testing` WHERE `title` = :title'; |
|
171 | - $invalidParams = array('id' => 1); |
|
172 | - $profiler = new PhpQuickProfiler(); |
|
173 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
174 | - |
|
175 | - $reflectedMethod->invokeArgs( |
|
176 | - $profiler, |
|
177 | - array(self::$dbConnection, $query, $invalidParams) |
|
178 | - ); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * @dataProvider dataConnectionDrivers |
|
183 | - */ |
|
184 | - public function testGetExplainQuery($driver, $prefix) |
|
185 | - { |
|
186 | - $query = 'SELECT * FROM `testing`'; |
|
187 | - $profiler = new PhpQuickProfiler(); |
|
188 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
189 | - |
|
190 | - $explainQuery = $reflectedMethod->invokeArgs( |
|
191 | - $profiler, |
|
192 | - array($query, $driver) |
|
193 | - ); |
|
194 | - |
|
195 | - $explainPrefix = str_replace($query, '', $explainQuery); |
|
196 | - $explainPrefix = trim($explainPrefix); |
|
197 | - $this->assertEquals($prefix, $explainPrefix); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * @expectedException Exception |
|
202 | - */ |
|
203 | - public function testGetExplainQueryUnsupportedDriver() |
|
204 | - { |
|
205 | - $query = 'SELECT * FROM `testing`'; |
|
206 | - $unsupportedDriver = 'zz'; |
|
207 | - $profiler = new PhpQuickProfiler(); |
|
208 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
209 | - |
|
210 | - $reflectedMethod->invokeArgs( |
|
211 | - $profiler, |
|
212 | - array($query, $unsupportedDriver) |
|
213 | - ); |
|
214 | - } |
|
215 | - |
|
216 | - public function testGatherSpeedData() |
|
217 | - { |
|
218 | - $elapsedTime = 1.234; |
|
219 | - $startTime = microtime(true) - $elapsedTime; |
|
220 | - $allowedTime = ini_get('max_execution_time'); |
|
221 | - $profiler = new PhpQuickProfiler($startTime); |
|
222 | - $gatheredSpeedData = $profiler->gatherSpeedData(); |
|
223 | - |
|
224 | - $this->assertInternalType('array', $gatheredSpeedData); |
|
225 | - $this->assertEquals(2, count($gatheredSpeedData)); |
|
226 | - $this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
|
227 | - $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
|
228 | - $this->assertArrayHasKey('allowed', $gatheredSpeedData); |
|
229 | - $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
|
230 | - } |
|
231 | - |
|
232 | - public function dataProfiledQueries() |
|
233 | - { |
|
234 | - return array( |
|
235 | - array( |
|
236 | - 'sql' => "SELECT * FROM testing", |
|
237 | - 'parameters' => array(), |
|
238 | - 'time' => 25 |
|
239 | - ), |
|
240 | - array( |
|
241 | - 'sql' => "SELECT id FROM testing WHERE title = :title", |
|
242 | - 'parameters' => array('title' => 'beta'), |
|
243 | - 'time' => 5 |
|
244 | - ) |
|
245 | - ); |
|
246 | - } |
|
247 | - |
|
248 | - public function dataConnectionDrivers() |
|
249 | - { |
|
250 | - return array( |
|
251 | - array( |
|
252 | - 'driver' => 'mysql', |
|
253 | - 'prefix' => 'EXPLAIN' |
|
254 | - ), |
|
255 | - array( |
|
256 | - 'driver' => 'sqlite', |
|
257 | - 'prefix' => 'EXPLAIN QUERY PLAN' |
|
258 | - ) |
|
259 | - ); |
|
260 | - } |
|
261 | - |
|
262 | - protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName) |
|
263 | - { |
|
264 | - $reflectedConsole = new ReflectionClass(get_class($profiler)); |
|
265 | - $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
266 | - $reflectedMethod->setAccessible(true); |
|
267 | - return $reflectedMethod; |
|
268 | - } |
|
269 | - |
|
270 | - public static function tearDownAfterClass() |
|
271 | - { |
|
272 | - self::$dbConnection = null; |
|
273 | - } |
|
33 | + self::$dbConnection->exec($hydrateTable); |
|
34 | + } |
|
35 | + |
|
36 | + public function testConstruct() |
|
37 | + { |
|
38 | + $startTime = microtime(true); |
|
39 | + |
|
40 | + $profiler = new PhpQuickProfiler(); |
|
41 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
42 | + |
|
43 | + $profiler = new PhpQuickProfiler($startTime); |
|
44 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
45 | + } |
|
46 | + |
|
47 | + public function testSetConsole() |
|
48 | + { |
|
49 | + $console = new Console(); |
|
50 | + $profiler = new PhpQuickProfiler(); |
|
51 | + $profiler->setConsole($console); |
|
52 | + |
|
53 | + $this->assertAttributeSame($console, 'console', $profiler); |
|
54 | + } |
|
55 | + |
|
56 | + public function testSetDisplay() |
|
57 | + { |
|
58 | + $display = new Display(); |
|
59 | + $profiler = new PhpQuickProfiler(); |
|
60 | + $profiler->setDisplay($display); |
|
61 | + |
|
62 | + $this->assertAttributeSame($display, 'display', $profiler); |
|
63 | + } |
|
64 | + |
|
65 | + public function testGatherFileData() |
|
66 | + { |
|
67 | + $files = get_included_files(); |
|
68 | + $profiler = new PhpQuickProfiler(); |
|
69 | + $gatheredFileData = $profiler->gatherFileData(); |
|
70 | + |
|
71 | + $this->assertInternalType('array', $gatheredFileData); |
|
72 | + $this->assertEquals(count($files), count($gatheredFileData)); |
|
73 | + foreach ($gatheredFileData as $fileData) { |
|
74 | + $this->assertInternalType('array', $fileData); |
|
75 | + $this->assertArrayHasKey('name', $fileData); |
|
76 | + $this->assertContains($fileData['name'], $files); |
|
77 | + $this->assertArrayHasKey('size', $fileData); |
|
78 | + $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + public function testGatherMemoryData() |
|
83 | + { |
|
84 | + $memoryUsage = memory_get_peak_usage(); |
|
85 | + $allowedLimit = ini_get('memory_limit'); |
|
86 | + $profiler = new PhpQuickProfiler(); |
|
87 | + $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
88 | + |
|
89 | + $this->assertInternalType('array', $gatheredMemoryData); |
|
90 | + $this->assertEquals(2, count($gatheredMemoryData)); |
|
91 | + $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
92 | + $this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
|
93 | + $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
94 | + $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
|
95 | + } |
|
96 | + |
|
97 | + public function testSetProfiledQueries() |
|
98 | + { |
|
99 | + $profiledQueries = $this->dataProfiledQueries(); |
|
100 | + $profiler = new PhpQuickProfiler(); |
|
101 | + $profiler->setProfiledQueries($profiledQueries); |
|
102 | + |
|
103 | + $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
104 | + } |
|
105 | + |
|
106 | + public function testGatherQueryData() |
|
107 | + { |
|
108 | + $profiledQueries = $this->dataProfiledQueries(); |
|
109 | + $profiledQueriesSql = array(); |
|
110 | + $profiledQueriesTime = array(); |
|
111 | + foreach ($profiledQueries as $queryData) { |
|
112 | + array_push($profiledQueriesSql, $queryData['sql']); |
|
113 | + array_push($profiledQueriesTime, $queryData['time']); |
|
114 | + } |
|
115 | + |
|
116 | + $profiler = new PhpQuickProfiler(); |
|
117 | + $profiler->setProfiledQueries($profiledQueries); |
|
118 | + $gatheredQueryData = $profiler->gatherQueryData(self::$dbConnection); |
|
119 | + |
|
120 | + $this->assertInternalType('array', $gatheredQueryData); |
|
121 | + $this->assertEquals(count($profiledQueries), count($gatheredQueryData)); |
|
122 | + foreach ($gatheredQueryData as $queryData) { |
|
123 | + $this->assertInternalType('array', $queryData); |
|
124 | + $this->assertArrayHasKey('sql', $queryData); |
|
125 | + $this->assertContains($queryData['sql'], $profiledQueriesSql); |
|
126 | + $this->assertArrayHasKey('explain', $queryData); |
|
127 | + $this->assertInternaltype('array', $queryData['explain']); |
|
128 | + $this->assertGreaterThan(0, count($queryData['explain'])); |
|
129 | + $this->assertArrayHasKey('time', $queryData); |
|
130 | + $this->assertContains($queryData['time'], $profiledQueriesTime); |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @dataProvider dataProfiledQueries |
|
136 | + */ |
|
137 | + public function testExplainQuery($sql, $parameters) |
|
138 | + { |
|
139 | + $profiler = new PhpQuickProfiler(); |
|
140 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
141 | + |
|
142 | + $explainedQuery = $reflectedMethod->invokeArgs( |
|
143 | + $profiler, |
|
144 | + array(self::$dbConnection, $sql, $parameters) |
|
145 | + ); |
|
146 | + $this->assertInternalType('array', $explainedQuery); |
|
147 | + $this->assertGreaterThan(0, count($explainedQuery)); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @expectedException Exception |
|
152 | + */ |
|
153 | + public function testExplainQueryBadQueryException() |
|
154 | + { |
|
155 | + $invalidQuery = 'SELECT * FROM `fake_table`'; |
|
156 | + $profiler = new PhpQuickProfiler(); |
|
157 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
158 | + |
|
159 | + $reflectedMethod->invokeArgs( |
|
160 | + $profiler, |
|
161 | + array(self::$dbConnection, $invalidQuery) |
|
162 | + ); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @expectedException Exception |
|
167 | + */ |
|
168 | + public function testExplainQueryBadParametersException() |
|
169 | + { |
|
170 | + $query = 'SELECT * FROM `testing` WHERE `title` = :title'; |
|
171 | + $invalidParams = array('id' => 1); |
|
172 | + $profiler = new PhpQuickProfiler(); |
|
173 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
174 | + |
|
175 | + $reflectedMethod->invokeArgs( |
|
176 | + $profiler, |
|
177 | + array(self::$dbConnection, $query, $invalidParams) |
|
178 | + ); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * @dataProvider dataConnectionDrivers |
|
183 | + */ |
|
184 | + public function testGetExplainQuery($driver, $prefix) |
|
185 | + { |
|
186 | + $query = 'SELECT * FROM `testing`'; |
|
187 | + $profiler = new PhpQuickProfiler(); |
|
188 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
189 | + |
|
190 | + $explainQuery = $reflectedMethod->invokeArgs( |
|
191 | + $profiler, |
|
192 | + array($query, $driver) |
|
193 | + ); |
|
194 | + |
|
195 | + $explainPrefix = str_replace($query, '', $explainQuery); |
|
196 | + $explainPrefix = trim($explainPrefix); |
|
197 | + $this->assertEquals($prefix, $explainPrefix); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * @expectedException Exception |
|
202 | + */ |
|
203 | + public function testGetExplainQueryUnsupportedDriver() |
|
204 | + { |
|
205 | + $query = 'SELECT * FROM `testing`'; |
|
206 | + $unsupportedDriver = 'zz'; |
|
207 | + $profiler = new PhpQuickProfiler(); |
|
208 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
209 | + |
|
210 | + $reflectedMethod->invokeArgs( |
|
211 | + $profiler, |
|
212 | + array($query, $unsupportedDriver) |
|
213 | + ); |
|
214 | + } |
|
215 | + |
|
216 | + public function testGatherSpeedData() |
|
217 | + { |
|
218 | + $elapsedTime = 1.234; |
|
219 | + $startTime = microtime(true) - $elapsedTime; |
|
220 | + $allowedTime = ini_get('max_execution_time'); |
|
221 | + $profiler = new PhpQuickProfiler($startTime); |
|
222 | + $gatheredSpeedData = $profiler->gatherSpeedData(); |
|
223 | + |
|
224 | + $this->assertInternalType('array', $gatheredSpeedData); |
|
225 | + $this->assertEquals(2, count($gatheredSpeedData)); |
|
226 | + $this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
|
227 | + $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
|
228 | + $this->assertArrayHasKey('allowed', $gatheredSpeedData); |
|
229 | + $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
|
230 | + } |
|
231 | + |
|
232 | + public function dataProfiledQueries() |
|
233 | + { |
|
234 | + return array( |
|
235 | + array( |
|
236 | + 'sql' => "SELECT * FROM testing", |
|
237 | + 'parameters' => array(), |
|
238 | + 'time' => 25 |
|
239 | + ), |
|
240 | + array( |
|
241 | + 'sql' => "SELECT id FROM testing WHERE title = :title", |
|
242 | + 'parameters' => array('title' => 'beta'), |
|
243 | + 'time' => 5 |
|
244 | + ) |
|
245 | + ); |
|
246 | + } |
|
247 | + |
|
248 | + public function dataConnectionDrivers() |
|
249 | + { |
|
250 | + return array( |
|
251 | + array( |
|
252 | + 'driver' => 'mysql', |
|
253 | + 'prefix' => 'EXPLAIN' |
|
254 | + ), |
|
255 | + array( |
|
256 | + 'driver' => 'sqlite', |
|
257 | + 'prefix' => 'EXPLAIN QUERY PLAN' |
|
258 | + ) |
|
259 | + ); |
|
260 | + } |
|
261 | + |
|
262 | + protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName) |
|
263 | + { |
|
264 | + $reflectedConsole = new ReflectionClass(get_class($profiler)); |
|
265 | + $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
266 | + $reflectedMethod->setAccessible(true); |
|
267 | + return $reflectedMethod; |
|
268 | + } |
|
269 | + |
|
270 | + public static function tearDownAfterClass() |
|
271 | + { |
|
272 | + self::$dbConnection = null; |
|
273 | + } |
|
274 | 274 | } |