From f9f179b45dc60afb6cf38a6c1ebf653c74c524bb Mon Sep 17 00:00:00 2001 From: Joshua Sherman Date: Wed, 15 Jan 2014 00:40:34 -0500 Subject: [PATCH] Profiler tests and some rework Abandoned private constructor and cleaned up the code a bit. --- classes/Profiler.php | 73 ++++++++++++---------------------- tests/classes/ProfilerTest.php | 73 ++++++++++++++++++++-------------- 2 files changed, 70 insertions(+), 76 deletions(-) diff --git a/classes/Profiler.php b/classes/Profiler.php index d855cbc..6693216 100644 --- a/classes/Profiler.php +++ b/classes/Profiler.php @@ -33,17 +33,6 @@ */ class Profiler { - /** - * Config - * - * Profiler configuration - * - * @static - * @access private - * @var array - */ - private static $config; - /** * Profile * @@ -53,7 +42,7 @@ class Profiler * @access private * @var array */ - private static $profile = array(); + private static $profile = []; /** * Queries @@ -75,19 +64,7 @@ class Profiler * @access private * @var array */ - private static $timers = array(); - - /** - * Constructor - * - * Private constructor since this class is interfaced wtih statically. - * - * @access private - */ - private function __construct() - { - - } + private static $timers = []; /** * Enabled @@ -100,15 +77,11 @@ class Profiler */ public static function enabled(/* polymorphic */) { - // Grabs the config object if we don't have one yet - if (self::$config == null) - { - $config = Config::getInstance(); - self::$config = $config->pickles['profiler']; - } + $config = Config::getInstance(); + $config = $config->pickles['profiler']; // Checks if we're set to boolean true - if (self::$config === true) + if ($config === true) { return true; } @@ -118,7 +91,7 @@ class Profiler foreach ($types as $type) { - if (stripos(self::$config, $type) !== false) + if (stripos($config, $type) !== false) { return true; } @@ -178,13 +151,13 @@ class Profiler break; } - self::$profile[] = array( + self::$profile[] = [ 'log' => $log, 'type' => $data_type, 'time' => $time, 'elapsed' => $time - $_SERVER['REQUEST_TIME_FLOAT'], 'memory' => memory_get_usage(), - ); + ]; } /** @@ -206,11 +179,11 @@ class Profiler if ($input_parameters != 'false' && is_array($input_parameters)) { - $log .= '
'; + $log .= '
'; foreach ($input_parameters as $key => $value) { - $log .= '
' . $key . ' => ' . $value . ''; + $log .= '
' . $key . ' => ' . $value . ''; $query = str_replace($key, '' . $key . '', $query); } @@ -220,19 +193,19 @@ class Profiler if (is_array($explain)) { - $log .= '
'; + $log .= '
'; foreach ($explain as $table) { - $log .= '
Possible Keys => ' . ($table['possible_keys'] == '' ? 'NONE' : $table['possible_keys']) . '' - . '
Key => ' . ($table['key'] == '' ? 'NONE' : $table['key']) . '' - . '
Type => ' . $table['type'] . '' - . '
Rows => ' . $table['rows'] . '' - . ($table['Extra'] != '' ? '
Extra => ' . $table['Extra'] . '' : ''); + $log .= '
Possible Keys => ' . ($table['possible_keys'] == '' ? 'NONE' : $table['possible_keys']) . '' + . '
Key => ' . ($table['key'] == '' ? 'NONE' : $table['key']) . '' + . '
Type => ' . $table['type'] . '' + . '
Rows => ' . $table['rows'] . '' + . ($table['Extra'] != '' ? '
Extra => ' . $table['Extra'] . '' : ''); } } - $log .= '

Speed: ' . number_format($duration * 100, 3) . ' ms'; + $log .= '

Speed: ' . number_format($duration * 100, 3) . ' ms'; self::log($log, false, 'database'); } @@ -275,6 +248,8 @@ class Profiler * Generates the Profiler report that is displayed by the Controller. * Contains all the HTML needed to display the data properly inline on the * page. Will generally be displayed after the closing HTML tag. + * + * @todo Thinking this should return the report and not necessarily echo it */ public static function report() { @@ -317,7 +292,7 @@ class Profiler }
- PICKLES Profiler

+ PICKLES Profiler

-

+

assertTrue(Profiler::enabled()); - } - - public function testDisabled() - { - // $this->assertFalse(Profiler::enabled()); - } - - public function testEnabledType() + public function testReport() { + $this->expectOutputRegex('//'); + Profiler::report(); } public function testDisabledType() { + $config = Config::getInstance(); + $config->data['pickles']['profiler'] = false; + $this->assertFalse(Profiler::enabled('timers')); } - public function testLogArray() + public function testTimerDisabled() { - + $this->assertFalse(Profiler::timer('disabled')); } - - public function testLogObject() + public function testReportNothing() { + $this->expectOutputRegex('/There is nothing to profile/'); + Profiler::report(); } - public function testLogTimer() + public function testEnabled() { + $config = Config::getInstance(); + $config->data['pickles']['profiler'] = true; + $this->assertTrue(Profiler::enabled()); } - public function testLogString() + public function testEnabledType() { + $config = Config::getInstance(); + $config->data['pickles']['profiler'] = 'timers'; + $this->assertTrue(Profiler::enabled('timers')); + } + + public function testLogAndTimer() + { + Profiler::log('timer', 'timer-one'); + Profiler::log(['foo' => 'bar']); + Profiler::log(new Object); + Profiler::log('string'); + Profiler::log(3.14, 'method', true); + Profiler::log('timer', 'timer-one'); } public function testLogQuery() { + $explain = [ + [ + 'key' => '', + 'possible_keys' => '', + 'type' => '', + 'rows' => '', + 'Extra' => '', + ], + ]; + Profiler::logQuery('SELECT * FROM table;'); + Profiler::logQuery('SELECT * FROM table WHERE column = ?;', ['foo']); + Profiler::logQuery('SELECT * FROM table;', false, $explain); } - public function testTimerStart() + public function testTimer() { - - } - - public function testTimerEnd() - { - - } - - public function testReport() - { - + Profiler::timer('timer-two'); + Profiler::timer('timer-two'); } }