diff --git a/src/Profiler.php b/src/Profiler.php index a2d4017..3277fe2 100644 --- a/src/Profiler.php +++ b/src/Profiler.php @@ -122,44 +122,18 @@ class Profiler */ public static function query($query, $input_parameters = false, $results = false, $duration = false, $explain = false) { - $log = []; - - /* - if ($input_parameters != 'false' && is_array($input_parameters)) - { - foreach ($input_parameters as $key => $value) - { - $log .= $key . ' => ' . $value; - - $query = str_replace($key, $key, $query); - } - } - - $log = $query . ' ' . $log; - - if (is_array($explain)) - { - 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 .= 'query_time: ' . $duration; - */ - $log = [ - 'query' => $query, - 'parameters' => $input_parameters, - 'results' => $results, + 'query' => $query, + 'parameters' => $input_parameters, + 'results' => $results, 'execution_time' => $duration, - 'explain' => $explain, ]; + if ($explain) + { + $log['explain'] = $explain; + } + self::log($log, false, 'database'); } diff --git a/tests/ProfilerTest.php b/tests/ProfilerTest.php index 9653c3b..140d240 100644 --- a/tests/ProfilerTest.php +++ b/tests/ProfilerTest.php @@ -2,60 +2,62 @@ class ProfilerTest extends PHPUnit_Framework_TestCase { - public function testReport() + public function testProfiler() { - $this->expectOutputRegex('//'); + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['SERVER_NAME'] = '127.0.0.1'; - Pickles\Profiler::report(); - } + file_put_contents('/tmp/pickles.php', ' [ + "local" => "127.0.0.1", + "production" => "123.456.789.0", + ], + "pickles" => [ + "profiler" => true, + ], + ]; + '); - public function testEnabled() - { - $config = Pickles\Config::getInstance(); - $config->data['pickles']['profiler'] = true; + new Pickles\Config('/tmp/pickles.php'); - $this->assertTrue(Pickles\Profiler::enabled()); - } - - public function testEnabledType() - { - $config = Pickles\Config::getInstance(); - $config->data['pickles']['profiler'] = 'timers'; - - $this->assertTrue(Pickles\Profiler::enabled('timers')); - } - - public function testLogAndTimer() - { - Pickles\Profiler::log('timer', 'timer-one'); + Pickles\Profiler::log('i am a string'); Pickles\Profiler::log(['foo' => 'bar']); - Pickles\Profiler::log(new Pickles\Object); - Pickles\Profiler::log('string'); - Pickles\Profiler::log(3.14, 'method', true); - Pickles\Profiler::log('timer', 'timer-one'); - } + Pickles\Profiler::log($this, 'testProfiler'); + Pickles\Profiler::timer('swatch'); + Pickles\Profiler::query('SELECT', ['foo' => 'bar'], ['results'], 1, 'EXPLAIN'); + Pickles\Profiler::timer('swatch'); + Pickles\Profiler::query('SELECT', ['foo' => 'bar'], ['results'], 1); - public function testLogQuery() - { - $explain = [ - [ - 'key' => '', - 'possible_keys' => '', - 'type' => '', - 'rows' => '', - 'Extra' => '', - ], - ]; + $report = Pickles\Profiler::report(); - Pickles\Profiler::query('SELECT * FROM table;'); - Pickles\Profiler::query('SELECT * FROM table WHERE column = ?;', ['foo']); - Pickles\Profiler::query('SELECT * FROM table;', false, $explain); - } - - public function testTimer() - { - Pickles\Profiler::timer('timer-two'); - Pickles\Profiler::timer('timer-two'); + $this->assertEquals(7, count($report)); + $this->assertEquals(7, count($report['logs'])); + $this->assertEquals(5, count($report['logs'][0])); + $this->assertEquals('string', $report['logs'][0]['type']); + $this->assertEquals('i am a string', $report['logs'][0]['details']); + $this->assertEquals('array', $report['logs'][1]['type']); + $this->assertEquals(['foo' => 'bar'], $report['logs'][1]['details']); + $this->assertEquals('object', $report['logs'][2]['type']); + $this->assertEquals(['class' => 'ProfilerTest', 'method' => 'testProfiler()'], $report['logs'][2]['details']); + $this->assertEquals('timer', $report['logs'][3]['type']); + $this->assertEquals('swatch', $report['logs'][3]['details']['name']); + $this->assertEquals('start', $report['logs'][3]['details']['action']); + $this->assertEquals('database', $report['logs'][4]['type']); + $this->assertEquals('SELECT', $report['logs'][4]['details']['query']); + $this->assertEquals(['foo' => 'bar'], $report['logs'][4]['details']['parameters']); + $this->assertEquals(['results'], $report['logs'][4]['details']['results']); + $this->assertEquals(1, $report['logs'][4]['details']['execution_time']); + $this->assertEquals('EXPLAIN', $report['logs'][4]['details']['explain']); + $this->assertEquals('timer', $report['logs'][5]['type']); + $this->assertEquals('swatch', $report['logs'][5]['details']['name']); + $this->assertEquals('stop', $report['logs'][5]['details']['action']); + $this->assertEquals('database', $report['logs'][6]['type']); + $this->assertEquals('SELECT', $report['logs'][6]['details']['query']); + $this->assertEquals(['foo' => 'bar'], $report['logs'][6]['details']['parameters']); + $this->assertEquals(['results'], $report['logs'][6]['details']['results']); + $this->assertEquals(1, $report['logs'][6]['details']['execution_time']); + $this->assertFalse(isset($report['logs'][6]['details']['explain'])); } }