diff --git a/tests/model/MongoStatementTest.php b/tests/model/MongoStatementTest.php index bef39ef..a85f626 100644 --- a/tests/model/MongoStatementTest.php +++ b/tests/model/MongoStatementTest.php @@ -207,7 +207,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase public function testFetch() { Config::set('PROFILER', true); - Config::set('PROFILER_DETAILS', true); + Config::set('PROFILER_DETAILS', false); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod()->setRequestForFetch(); @@ -216,6 +216,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase $result = $this->stmt->fetch(); $this->assertEquals('prev', $result->next); $this->assertFalse($this->stmt->fetch()); + $this->assertContains('Queries not counted.', Profiler::getInstance()->getCli()); } /** diff --git a/tests/model/MySQLiStatementTest.php b/tests/model/MySQLiStatementTest.php index 22c13be..2a66c89 100644 --- a/tests/model/MySQLiStatementTest.php +++ b/tests/model/MySQLiStatementTest.php @@ -220,6 +220,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testClose() { + Config::set('PROFILER', true); Config::set('PROFILER_DETAILS', false); $this->assertAttributeEquals(null, 'result', $this->stmt); $this->setDriverGetConnectionMethod(); @@ -227,6 +228,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase $this->assertAttributeNotEquals(null, 'result', $this->stmt); $this->stmt->close(); $this->assertAttributeEquals(null, 'result', $this->stmt); + $this->assertContains('Queries not counted.', Profiler::getInstance()->getCli()); } /** diff --git a/tests/util/profiler/ProfilerTest.php b/tests/util/profiler/ProfilerTest.php index c3d4a90..2f2022c 100644 --- a/tests/util/profiler/ProfilerTest.php +++ b/tests/util/profiler/ProfilerTest.php @@ -72,6 +72,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase public function testStartEndNoCommandProfiler() { Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $profiler = Profiler::getInstance(); $profiler->start(); $result = $profiler->end(''); @@ -90,6 +91,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase public function testStartEndWithCommandProfiler() { Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); @@ -105,14 +107,146 @@ class ProfilerTest extends PHPUnit_Framework_TestCase /** * @runInSeparateProcess */ + public function testStartEndWithCommandProfilerWithNonDetails() + { + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', false); + + $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); + $profiler = Profiler::getInstance(); + $profiler->start(); + $result = $profiler->end(''); + $this->assertNotEquals('', $result); + $this->assertContains('Queries not counted.', $result); + } + + /** + * @runInSeparateProcess + */ + public function testStartEndWithCommandProfilerWithNonDetailsButExistingQueries() + { + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', false); + + $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); + $profiler = Profiler::getInstance(); + $cmdProfiler = $profiler->profilerCommand('command', 'type'); + $profiler->start(); + $result = $profiler->end(''); + $this->assertNotEquals('', $result); + $this->assertStringEndsNotWith(']
', $result); + $this->assertContains('Queries: 1', $result); + } + + /** + * @runInSeparateProcess + */ + public function testStartEndWithCommandProfilerWithDetailsButNonQueries() + { + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); + + $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); + $profiler = Profiler::getInstance(); + $profiler->start(); + $result = $profiler->end(''); + $this->assertNotEquals('', $result); + $this->assertStringEndsWith(']
', $result); + $this->assertContains('Queries: 0', $result); + } + + /** + * @runInSeparateProcess + */ public function testGetJSON() { Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); + + $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); + $profiler = Profiler::getInstance(); + $cmdProfiler = $profiler->profilerCommand('command', 'type'); + $fire_php_mock = $this->getMockBuilder('FirePHP') + ->disableOriginalConstructor() + ->setMethods(array('__construct', 'fb')) + ->getMock(); + $fire_php_mock->expects($this->exactly(2)) + ->method('fb') + ->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // Expected "Queries: 1" + $reflection_property = new ReflectionProperty('FirePHP', 'instance'); + $reflection_property->setAccessible(true); + $reflection_property->setValue($fire_php_mock); + $this->assertNull($profiler->getJson()); + } + + /** + * @runInSeparateProcess + */ + public function testGetJSONWithNonDetails() + { + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', false); + + $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); + $profiler = Profiler::getInstance(); + $fire_php_mock = $this->getMockBuilder('FirePHP') + ->disableOriginalConstructor() + ->setMethods(array('__construct', 'fb')) + ->getMock(); + $fire_php_mock->expects($this->exactly(2)) + ->method('fb') + ->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries: 1'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // expected "Queries not counted" + $reflection_property = new ReflectionProperty('FirePHP', 'instance'); + $reflection_property->setAccessible(true); + $reflection_property->setValue($fire_php_mock); + $this->assertNull($profiler->getJson()); + } + + /** + * @runInSeparateProcess + */ + public function testGetJSONWithNonDetailsButExistingQueries() + { + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', false); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); $cmdProfiler = $profiler->profilerCommand('command', 'type'); - $this->assertNull($profiler->getJson()); + $fire_php_mock = $this->getMockBuilder('FirePHP') + ->disableOriginalConstructor() + ->setMethods(array('__construct', 'fb')) + ->getMock(); + $fire_php_mock->expects($this->exactly(2)) + ->method('fb') + ->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not counted'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // expected "Queries: 1" + $reflection_property = new ReflectionProperty('FirePHP', 'instance'); + $reflection_property->setAccessible(true); + $reflection_property->setValue($fire_php_mock); + $this->assertNull($profiler->getJson()); + } + + /** + * @runInSeparateProcess + */ + public function testGetJSONWithDetailsButNonQueries() + { + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); + + $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); + $profiler = Profiler::getInstance(); + $fire_php_mock = $this->getMockBuilder('FirePHP') + ->disableOriginalConstructor() + ->setMethods(array('__construct', 'fb')) + ->getMock(); + $fire_php_mock->expects($this->exactly(2)) + ->method('fb') + ->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not counted'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 1'))); // expected "Queries: 0" + $reflection_property = new ReflectionProperty('FirePHP', 'instance'); + $reflection_property->setAccessible(true); + $reflection_property->setValue($fire_php_mock); + $this->assertNull($profiler->getJson()); } /** @@ -121,12 +255,60 @@ class ProfilerTest extends PHPUnit_Framework_TestCase public function testGetCLI() { Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); $cmdProfiler = $profiler->profilerCommand('command', 'type'); $result = $profiler->getCli(); $this->assertNotNull($result); + $this->assertContains('Queries: 1', Profiler::getInstance()->getCli()); + } + + /** + * @runInSeparateProcess + */ + public function testGetCLIWithNonDetails() + { + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', false); + + $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); + $profiler = Profiler::getInstance(); + $result = $profiler->getCli(); + $this->assertNotNull($result); + $this->assertContains('Queries not counted', Profiler::getInstance()->getCli()); + } + + /** + * @runInSeparateProcess + */ + public function testGetCLIWithNonDetailsButExistingQueries() + { + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', false); + + $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); + $profiler = Profiler::getInstance(); + $cmdProfiler = $profiler->profilerCommand('command', 'type'); + $result = $profiler->getCli(); + $this->assertNotNull($result); + $this->assertContains('Queries: 1', Profiler::getInstance()->getCli()); + } + + /** + * @runInSeparateProcess + */ + public function testGetCLIWithDetailsButNonQueries() + { + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); + + $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); + $profiler = Profiler::getInstance(); + $result = $profiler->getCli(); + $this->assertNotNull($result); + $this->assertContains('Queries: 0', Profiler::getInstance()->getCli()); } public function tearDown() diff --git a/util/profiler/Profiler.php b/util/profiler/Profiler.php index e883b4c..a0c5067 100644 --- a/util/profiler/Profiler.php +++ b/util/profiler/Profiler.php @@ -80,8 +80,12 @@ class Profiler $queriesTime += $query->getElapsed(); } $html = '
' - . 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.
' - . 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]
'; + . 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.
'; + if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) { + $html .= 'Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.
'; + } else { + $html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]
'; + } $html .= $temp; $html .= '
'; return $html; @@ -99,7 +103,11 @@ class Profiler $table[] = array($query->getType(), round($query->getElapsed() * 1000, 2), $query->getCommand()); $queriesTime += $query->getElapsed(); } - FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table); + if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) { + FB::table('Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.', $table); + } else { + FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table); + } } public function getCli() @@ -112,8 +120,12 @@ class Profiler $queriesTime += $query->getElapsed(); } $html = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT); - $html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL - . 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL; + $html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL; + if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) { + $html .= 'Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.' . PHP_EOL; + } else { + $html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL; + } $html .= $temp; return $html; }