Completed
Push — master ( 77fd29...f7cf81 )
by Jacob
02:48
created
tests/unit/PhpQuickProfilerTest.php 3 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -230,6 +230,9 @@
 block discarded – undo
230 230
         );
231 231
     }
232 232
 
233
+    /**
234
+     * @param string $methodName
235
+     */
233 236
     protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName)
234 237
     {
235 238
         $reflectedConsole = new ReflectionClass(get_class($profiler));
Please login to merge, or discard this patch.
Unused Use Statements   -2 removed lines patch added patch discarded remove patch
@@ -3,9 +3,7 @@
 block discarded – undo
3 3
 namespace Particletree\Pqp;
4 4
 
5 5
 use PDO;
6
-use PHPUnit_Framework_Testcase;
7 6
 use ReflectionClass;
8
-use ReflectionMethod;
9 7
 
10 8
 class PhpQuickProfilerTest extends PHPUnit_Framework_TestCase
11 9
 {
Please login to merge, or discard this patch.
Indentation   +220 added lines, -220 removed lines patch added patch discarded remove patch
@@ -10,18 +10,18 @@  discard block
 block discarded – undo
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
-        self::$dbConnection->exec("
15
+	public static function setUpBeforeClass()
16
+	{
17
+		self::$dbConnection = new PDO('sqlite::memory:');
18
+		self::$dbConnection->exec("
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
-        );
24
-        self::$dbConnection->exec("
23
+		);
24
+		self::$dbConnection->exec("
25 25
             INSERT INTO `testing`
26 26
                 (`title`)
27 27
             VALUES
@@ -29,217 +29,217 @@  discard block
 block discarded – undo
29 29
                 ('beta'),
30 30
                 ('charlie'),
31 31
                 ('delta');"
32
-        );
33
-    }
34
-
35
-    public function testConstruct()
36
-    {
37
-        $startTime = microtime(true);
38
-
39
-        $profiler = new PhpQuickProfiler();
40
-        $this->assertAttributeEquals($startTime, 'startTime', $profiler);
41
-
42
-        $profiler = new PhpQuickProfiler($startTime);
43
-        $this->assertAttributeEquals($startTime, 'startTime', $profiler);
44
-    }
45
-
46
-    public function testSetConsole()
47
-    {
48
-        $console = new Console();
49
-        $profiler = new PhpQuickProfiler();
50
-        $profiler->setConsole($console);
51
-
52
-        $this->assertAttributeSame($console, 'console', $profiler);
53
-    }
54
-
55
-    public function testSetDisplay()
56
-    {
57
-        $display = new Display();
58
-        $profiler = new PhpQuickProfiler();
59
-        $profiler->setDisplay($display);
60
-
61
-        $this->assertAttributeSame($display, 'display', $profiler);
62
-    }
63
-
64
-    public function testGatherFileData()
65
-    {
66
-        $files = get_included_files();
67
-        $profiler = new PhpQuickProfiler();
68
-        $gatheredFileData = $profiler->gatherFileData();
69
-
70
-        $this->assertInternalType('array', $gatheredFileData);
71
-        $this->assertEquals(count($files), count($gatheredFileData));
72
-        foreach ($gatheredFileData as $fileData) {
73
-            $this->assertInternalType('array', $fileData);
74
-            $this->assertArrayHasKey('name', $fileData);
75
-            $this->assertContains($fileData['name'], $files);
76
-            $this->assertArrayHasKey('size', $fileData);
77
-            $this->assertEquals($fileData['size'], filesize($fileData['name']));
78
-        }
79
-    }
80
-
81
-    public function testGatherMemoryData()
82
-    {
83
-        $memoryUsage = memory_get_peak_usage();
84
-        $allowedLimit = ini_get('memory_limit');
85
-        $profiler = new PhpQuickProfiler();
86
-        $gatheredMemoryData = $profiler->gatherMemoryData();
87
-
88
-        $this->assertInternalType('array', $gatheredMemoryData);
89
-        $this->assertEquals(2, count($gatheredMemoryData));
90
-        $this->assertArrayHasKey('used', $gatheredMemoryData);
91
-        $this->assertEquals($memoryUsage, $gatheredMemoryData['used']);
92
-        $this->assertArrayHasKey('allowed', $gatheredMemoryData);
93
-        $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']);
94
-    }
95
-
96
-    public function testSetProfiledQueries()
97
-    {
98
-        $profiledQueries = $this->dataProfiledQueries();
99
-        $profiler = new PhpQuickProfiler();
100
-        $profiler->setProfiledQueries($profiledQueries);
101
-
102
-        $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler);
103
-    }
104
-
105
-    /**
106
-     * @dataProvider dataProfiledQueries
107
-     */
108
-    public function testExplainQuery($sql, $parameters)
109
-    {
110
-        $profiler = new PhpQuickProfiler();
111
-        $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery');
112
-
113
-        $explainedQuery = $reflectedMethod->invokeArgs(
114
-            $profiler,
115
-            array(self::$dbConnection, $sql, $parameters)
116
-        );
117
-        $this->assertInternalType('array', $explainedQuery);
118
-        $this->assertGreaterThan(0, count($explainedQuery));
119
-    }
120
-
121
-    /**
122
-     * @expectedException Exception
123
-     */
124
-    public function testExplainQueryBadQueryException()
125
-    {
126
-        $invalidQuery = 'SELECT * FROM `fake_table`';
127
-        $profiler = new PhpQuickProfiler();
128
-        $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery');
129
-
130
-        $explainedQuery = $reflectedMethod->invokeArgs(
131
-            $profiler,
132
-            array(self::$dbConnection, $invalidQuery)
133
-        );
134
-    }
135
-
136
-    /**
137
-     * @expectedException Exception
138
-     */
139
-    public function testExplainQueryBadParametersException()
140
-    {
141
-        $query = 'SELECT * FROM `testing` WHERE `title` = :title';
142
-        $invalidParams = array('id' => 1);
143
-        $profiler = new PhpQuickProfiler();
144
-        $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery');
145
-
146
-        $explainedQuery = $reflectedMethod->invokeArgs(
147
-            $profiler,
148
-            array(self::$dbConnection, $query, $invalidParams)
149
-        );
150
-    }
151
-
152
-    /**
153
-     * @dataProvider dataConnectionDrivers
154
-     */
155
-    public function testGetExplainQuery($driver, $prefix)
156
-    {
157
-        $query = 'SELECT * FROM `testing`';
158
-        $profiler = new PhpQuickProfiler();
159
-        $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery');
160
-
161
-        $explainQuery = $reflectedMethod->invokeArgs(
162
-            $profiler,
163
-            array($query, $driver)
164
-        );
165
-
166
-        $explainPrefix = str_replace($query, '', $explainQuery);
167
-        $explainPrefix = trim($explainPrefix);
168
-        $this->assertEquals($prefix, $explainPrefix);
169
-    }
170
-
171
-    /**
172
-     * @expectedException Exception
173
-     */
174
-    public function testGetExplainQueryUnsupportedDriver()
175
-    {
176
-        $query = 'SELECT * FROM `testing`';
177
-        $unsupportedDriver = 'zz';
178
-        $profiler = new PhpQuickProfiler();
179
-        $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery');
180
-
181
-        $explainQuery = $reflectedMethod->invokeArgs(
182
-            $profiler,
183
-            array($query, $unsupportedDriver)
184
-        );
185
-    }
186
-
187
-    public function testGatherSpeedData()
188
-    {
189
-        $elapsedTime = 1.234;
190
-        $startTime = microtime(true) - $elapsedTime;
191
-        $allowedTime = ini_get('max_execution_time');
192
-        $profiler = new PhpQuickProfiler($startTime);
193
-        $gatheredSpeedData = $profiler->gatherSpeedData();
194
-
195
-        $this->assertInternalType('array', $gatheredSpeedData);
196
-        $this->assertEquals(2, count($gatheredSpeedData));
197
-        $this->assertArrayHasKey('elapsed', $gatheredSpeedData);
198
-        $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']);
199
-        $this->assertArrayHasKey('allowed', $gatheredSpeedData);
200
-        $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']);
201
-    }
202
-
203
-    public function dataProfiledQueries()
204
-    {
205
-        return array(
206
-            array(
207
-              'sql' => "SELECT * FROM testing",
208
-              'parameters' => array(),
209
-              'time' => 25
210
-            ),
211
-            array(
212
-              'sql' => "SELECT id FROM testing WHERE title = :title",
213
-              'parameters' => array('title' => 'beta'),
214
-              'time' => 5
215
-            )
216
-        );
217
-    }
218
-
219
-    public function dataConnectionDrivers()
220
-    {
221
-        return array(
222
-            array(
223
-                'driver' => 'mysql',
224
-                'prefix' => 'EXPLAIN'
225
-            ),
226
-            array(
227
-                'driver' => 'sqlite',
228
-                'prefix' => 'EXPLAIN QUERY PLAN'
229
-            )
230
-        );
231
-    }
232
-
233
-    protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName)
234
-    {
235
-        $reflectedConsole = new ReflectionClass(get_class($profiler));
236
-        $reflectedMethod = $reflectedConsole->getMethod($methodName);
237
-        $reflectedMethod->setAccessible(true);
238
-        return $reflectedMethod;
239
-    }
240
-
241
-    public static function tearDownAfterClass()
242
-    {
243
-        self::$dbConnection = null;
244
-    }
32
+		);
33
+	}
34
+
35
+	public function testConstruct()
36
+	{
37
+		$startTime = microtime(true);
38
+
39
+		$profiler = new PhpQuickProfiler();
40
+		$this->assertAttributeEquals($startTime, 'startTime', $profiler);
41
+
42
+		$profiler = new PhpQuickProfiler($startTime);
43
+		$this->assertAttributeEquals($startTime, 'startTime', $profiler);
44
+	}
45
+
46
+	public function testSetConsole()
47
+	{
48
+		$console = new Console();
49
+		$profiler = new PhpQuickProfiler();
50
+		$profiler->setConsole($console);
51
+
52
+		$this->assertAttributeSame($console, 'console', $profiler);
53
+	}
54
+
55
+	public function testSetDisplay()
56
+	{
57
+		$display = new Display();
58
+		$profiler = new PhpQuickProfiler();
59
+		$profiler->setDisplay($display);
60
+
61
+		$this->assertAttributeSame($display, 'display', $profiler);
62
+	}
63
+
64
+	public function testGatherFileData()
65
+	{
66
+		$files = get_included_files();
67
+		$profiler = new PhpQuickProfiler();
68
+		$gatheredFileData = $profiler->gatherFileData();
69
+
70
+		$this->assertInternalType('array', $gatheredFileData);
71
+		$this->assertEquals(count($files), count($gatheredFileData));
72
+		foreach ($gatheredFileData as $fileData) {
73
+			$this->assertInternalType('array', $fileData);
74
+			$this->assertArrayHasKey('name', $fileData);
75
+			$this->assertContains($fileData['name'], $files);
76
+			$this->assertArrayHasKey('size', $fileData);
77
+			$this->assertEquals($fileData['size'], filesize($fileData['name']));
78
+		}
79
+	}
80
+
81
+	public function testGatherMemoryData()
82
+	{
83
+		$memoryUsage = memory_get_peak_usage();
84
+		$allowedLimit = ini_get('memory_limit');
85
+		$profiler = new PhpQuickProfiler();
86
+		$gatheredMemoryData = $profiler->gatherMemoryData();
87
+
88
+		$this->assertInternalType('array', $gatheredMemoryData);
89
+		$this->assertEquals(2, count($gatheredMemoryData));
90
+		$this->assertArrayHasKey('used', $gatheredMemoryData);
91
+		$this->assertEquals($memoryUsage, $gatheredMemoryData['used']);
92
+		$this->assertArrayHasKey('allowed', $gatheredMemoryData);
93
+		$this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']);
94
+	}
95
+
96
+	public function testSetProfiledQueries()
97
+	{
98
+		$profiledQueries = $this->dataProfiledQueries();
99
+		$profiler = new PhpQuickProfiler();
100
+		$profiler->setProfiledQueries($profiledQueries);
101
+
102
+		$this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler);
103
+	}
104
+
105
+	/**
106
+	 * @dataProvider dataProfiledQueries
107
+	 */
108
+	public function testExplainQuery($sql, $parameters)
109
+	{
110
+		$profiler = new PhpQuickProfiler();
111
+		$reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery');
112
+
113
+		$explainedQuery = $reflectedMethod->invokeArgs(
114
+			$profiler,
115
+			array(self::$dbConnection, $sql, $parameters)
116
+		);
117
+		$this->assertInternalType('array', $explainedQuery);
118
+		$this->assertGreaterThan(0, count($explainedQuery));
119
+	}
120
+
121
+	/**
122
+	 * @expectedException Exception
123
+	 */
124
+	public function testExplainQueryBadQueryException()
125
+	{
126
+		$invalidQuery = 'SELECT * FROM `fake_table`';
127
+		$profiler = new PhpQuickProfiler();
128
+		$reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery');
129
+
130
+		$explainedQuery = $reflectedMethod->invokeArgs(
131
+			$profiler,
132
+			array(self::$dbConnection, $invalidQuery)
133
+		);
134
+	}
135
+
136
+	/**
137
+	 * @expectedException Exception
138
+	 */
139
+	public function testExplainQueryBadParametersException()
140
+	{
141
+		$query = 'SELECT * FROM `testing` WHERE `title` = :title';
142
+		$invalidParams = array('id' => 1);
143
+		$profiler = new PhpQuickProfiler();
144
+		$reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery');
145
+
146
+		$explainedQuery = $reflectedMethod->invokeArgs(
147
+			$profiler,
148
+			array(self::$dbConnection, $query, $invalidParams)
149
+		);
150
+	}
151
+
152
+	/**
153
+	 * @dataProvider dataConnectionDrivers
154
+	 */
155
+	public function testGetExplainQuery($driver, $prefix)
156
+	{
157
+		$query = 'SELECT * FROM `testing`';
158
+		$profiler = new PhpQuickProfiler();
159
+		$reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery');
160
+
161
+		$explainQuery = $reflectedMethod->invokeArgs(
162
+			$profiler,
163
+			array($query, $driver)
164
+		);
165
+
166
+		$explainPrefix = str_replace($query, '', $explainQuery);
167
+		$explainPrefix = trim($explainPrefix);
168
+		$this->assertEquals($prefix, $explainPrefix);
169
+	}
170
+
171
+	/**
172
+	 * @expectedException Exception
173
+	 */
174
+	public function testGetExplainQueryUnsupportedDriver()
175
+	{
176
+		$query = 'SELECT * FROM `testing`';
177
+		$unsupportedDriver = 'zz';
178
+		$profiler = new PhpQuickProfiler();
179
+		$reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery');
180
+
181
+		$explainQuery = $reflectedMethod->invokeArgs(
182
+			$profiler,
183
+			array($query, $unsupportedDriver)
184
+		);
185
+	}
186
+
187
+	public function testGatherSpeedData()
188
+	{
189
+		$elapsedTime = 1.234;
190
+		$startTime = microtime(true) - $elapsedTime;
191
+		$allowedTime = ini_get('max_execution_time');
192
+		$profiler = new PhpQuickProfiler($startTime);
193
+		$gatheredSpeedData = $profiler->gatherSpeedData();
194
+
195
+		$this->assertInternalType('array', $gatheredSpeedData);
196
+		$this->assertEquals(2, count($gatheredSpeedData));
197
+		$this->assertArrayHasKey('elapsed', $gatheredSpeedData);
198
+		$this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']);
199
+		$this->assertArrayHasKey('allowed', $gatheredSpeedData);
200
+		$this->assertEquals($allowedTime, $gatheredSpeedData['allowed']);
201
+	}
202
+
203
+	public function dataProfiledQueries()
204
+	{
205
+		return array(
206
+			array(
207
+			  'sql' => "SELECT * FROM testing",
208
+			  'parameters' => array(),
209
+			  'time' => 25
210
+			),
211
+			array(
212
+			  'sql' => "SELECT id FROM testing WHERE title = :title",
213
+			  'parameters' => array('title' => 'beta'),
214
+			  'time' => 5
215
+			)
216
+		);
217
+	}
218
+
219
+	public function dataConnectionDrivers()
220
+	{
221
+		return array(
222
+			array(
223
+				'driver' => 'mysql',
224
+				'prefix' => 'EXPLAIN'
225
+			),
226
+			array(
227
+				'driver' => 'sqlite',
228
+				'prefix' => 'EXPLAIN QUERY PLAN'
229
+			)
230
+		);
231
+	}
232
+
233
+	protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName)
234
+	{
235
+		$reflectedConsole = new ReflectionClass(get_class($profiler));
236
+		$reflectedMethod = $reflectedConsole->getMethod($methodName);
237
+		$reflectedMethod->setAccessible(true);
238
+		return $reflectedMethod;
239
+	}
240
+
241
+	public static function tearDownAfterClass()
242
+	{
243
+		self::$dbConnection = null;
244
+	}
245 245
 }
Please login to merge, or discard this patch.
src/PhpQuickProfiler.php 1 patch
Indentation   +186 added lines, -186 removed lines patch added patch discarded remove patch
@@ -17,190 +17,190 @@
 block discarded – undo
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
-            if ($query['function'] !== 'perform') {
115
-                continue;
116
-            }
117
-
118
-            array_push($data, array(
119
-                'sql'     => $query['statement'],
120
-                'explain' => $this->explainQuery($dbConnection, $query['statement'], $query['bind_values']),
121
-                'time'    => $query['duration']
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
-
198
-        $this->display->setConsole($this->console);
199
-        $this->display->setFileData($this->gatherFileData());
200
-        $this->display->setMemoryData($this->gatherMemoryData());
201
-        $this->display->setQueryData($this->gatherQueryData($dbConnection));
202
-        $this->display->setSpeedData($this->gatherSpeedData());
203
-
204
-        $this->display->__invoke();
205
-    }
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
+			if ($query['function'] !== 'perform') {
115
+				continue;
116
+			}
117
+
118
+			array_push($data, array(
119
+				'sql'     => $query['statement'],
120
+				'explain' => $this->explainQuery($dbConnection, $query['statement'], $query['bind_values']),
121
+				'time'    => $query['duration']
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
+
198
+		$this->display->setConsole($this->console);
199
+		$this->display->setFileData($this->gatherFileData());
200
+		$this->display->setMemoryData($this->gatherMemoryData());
201
+		$this->display->setQueryData($this->gatherQueryData($dbConnection));
202
+		$this->display->setSpeedData($this->gatherSpeedData());
203
+
204
+		$this->display->__invoke();
205
+	}
206 206
 }
Please login to merge, or discard this patch.
tests/bootstrap.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-require_once __DIR__ . '/../vendor/autoload.php';
4
-require_once __DIR__ . '/function-overrides.php';
3
+require_once __DIR__.'/../vendor/autoload.php';
4
+require_once __DIR__.'/function-overrides.php';
Please login to merge, or discard this patch.
tests/function-overrides.php 1 patch
Indentation   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -5,37 +5,37 @@
 block discarded – undo
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_peak_usage()
28 28
 {
29
-    return 123456789;
29
+	return 123456789;
30 30
 }
31 31
 
32 32
 // namespace hack on ini settings
33 33
 function ini_get($setting)
34 34
 {
35
-    if ($setting == 'memory_limit') {
36
-        return '128M';
37
-    } elseif ($setting == 'max_execution_time') {
38
-        return '30';
39
-    }
40
-    return \ini_get($setting);
35
+	if ($setting == 'memory_limit') {
36
+		return '128M';
37
+	} elseif ($setting == 'max_execution_time') {
38
+		return '30';
39
+	}
40
+	return \ini_get($setting);
41 41
 }
Please login to merge, or discard this patch.