Permalink
Browse files

Cleanup stats.

  • Loading branch information...
piotrooo committed Feb 6, 2019
1 parent d26d700 commit fdc55948729a0ba0f7988f222a81bb4cadf83f69
@@ -15,7 +15,6 @@
use Ouzo\Injection\InjectorConfig;
use Ouzo\OutputDisplayer;
use Ouzo\RedirectHandler;
use Ouzo\Stats\SessionStats;
use Ouzo\Uri\PathProvider;
use Ouzo\Uri\PathProviderInterface;
use Ouzo\Utilities\Arrays;
@@ -49,7 +49,7 @@ public function action()
class MockControllerFactory extends ControllerFactory
{
public function createController(RouteRule $routeRule, RequestParameters $requestParameters)
public function createController(RouteRule $routeRule, RequestParameters $requestParameters, SessionStats $sessionStats)
{
$routeRule = Arrays::first(Route::getRoutes());
@@ -7,6 +7,7 @@
use Ouzo\Config;
use Ouzo\ContentType;
use Ouzo\Controller;
use Ouzo\Db\Stats;
use Ouzo\NoControllerActionException;
use Ouzo\Notice;
use Ouzo\Request\RequestParameters;
@@ -100,6 +101,13 @@ public function receive_params($user, $page)
$this->layout->renderAjax("Param1: $user Param2: $page");
$this->layout->unsetLayout();
}
public function tracing()
{
Stats::trace('select * form table', [], function () {
usleep(800);
});
}
}
class ControllerTest extends ControllerTestCase
@@ -562,4 +570,56 @@ public function shouldReadParametersFromStream()
//then
$this->assertEquals(['key' => 'value'], $this->getAssigned('params'));
}
/**
* @test
*/
public function shouldCountTimeAndNumberOfQueries()
{
//given
Route::get('/simple_test/tracing', 'simple_test#tracing');
//when
$this->get('/simple_test/tracing');
$this->get('/simple_test/tracing');
//then
$this->assertCount(2, $_SESSION['stats_queries']['/simple_test/tracing#']['queries']);
}
/**
* @test
*/
public function shouldTraceInfoAboutHttpRequest()
{
//given
Route::get('/simple_test/tracing', 'simple_test#tracing');
//when
$this->get('/simple_test/tracing', ['param1' => 'value1', 'param2' => 'value2']);
//then
Assert::thatArray($_SESSION['stats_queries']['/simple_test/tracing#']['request_params'][0])
->containsKeyAndValue(['param1' => 'value1', 'param2' => 'value2']);
}
/**
* @test
*/
public function shouldGroupStatsByRequest()
{
//given
Route::allowAll('/simple_test', 'simple_test');
//when
$this->get('/simple_test/tracing', ['param1' => 'value1', 'param2' => 'value2']);
$this->post('/simple_test/params', []);
$this->get('/simple_test/tracing', ['param3' => 'value3', 'param4' => 'value4']);
//then
$queries = $_SESSION['stats_queries'];
Assert::thatArray($queries)->hasSize(2);
Assert::thatArray($queries['/simple_test/tracing#']['request_params'])->hasSize(2);
Assert::thatArray($queries['/simple_test/params#']['request_params'])->hasSize(1);
}
}
@@ -6,16 +6,13 @@
use Ouzo\Db\Stats;
use Ouzo\FrontController;
use Ouzo\Tests\ArrayAssert;
use Ouzo\Utilities\Arrays;
use PHPUnit\Framework\TestCase;
class StatsTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$_SESSION = [];
Stats::reset();
FrontController::$requestId = null;
@@ -41,69 +38,4 @@ public function shouldTraceQueryWithParams()
$this->assertEquals('SELECT * FROM table WHERE id = ?', Stats::$queries[0]['query']);
$this->assertEquals('10', Stats::$queries[0]['params']);
}
/**
* @test
*/
public function shouldGroupByRequest()
{
$this->markTestSkipped('move to controller');
//given
$this->_createTraceRequest('/request1');
$this->_createTraceRequest('/request2');
$this->_createTraceRequest('/request1');
//when
$queries = Stats::$queries;
//then
ArrayAssert::that($queries)->hasSize(2);
}
/**
* @test
*/
public function shouldCountTimeAndNumberOfQueries()
{
$this->markTestSkipped('move to controller');
//when
$this->_createTraceRequest('/request1');
$this->_createTraceRequest('/request2');
$this->_createTraceRequest('/request1');
//then
$this->assertEquals(2, Stats::getRequestNumberOfQueries('/request1#'));
}
/**
* @test
*/
public function shouldTraceInfoAboutHttpRequest()
{
$this->markTestSkipped('move to controller tests');
//given
$this->_createHttpTraceRequest('/request1', ['param1' => 1, 'param2' => 2]);
//when
$queries = Arrays::first(Stats::queries());
//then
ArrayAssert::that($queries['request_params'][0])->hasSize(2)->containsKeyAndValue([
'param1' => 1, 'param2' => 2
]);
}
private function _createTraceRequest($request)
{
$_SERVER['REQUEST_URI'] = $request;
Stats::trace('SELECT * FROM table WHERE id = ?', '10', function () {
usleep(80000);
return "result";
});
}
private function _createHttpTraceRequest($request, $params = [])
{
$_SERVER['REQUEST_URI'] = $request;
}
}
@@ -2,12 +2,10 @@
namespace Ouzo\ExceptionHandling;
use PHPUnit\Framework\TestCase;
class ExceptionLoggerTest extends TestCase
{
/**
* @test
*/
@@ -18,7 +16,5 @@ public function shouldNotWritePasswordToLog()
// then
$this->assertEquals('[<login> => "xxx", <password> => "***"]', $result);
var_dump((new \Exception())->getTraceAsString());
}
}
@@ -3,6 +3,7 @@
* Copyright (c) Ouzo contributors, http://ouzoframework.org
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Ouzo;
use Exception;

0 comments on commit fdc5594

Please sign in to comment.