Skip to content

Commit

Permalink
adds logging to ShortRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
henderjon committed Oct 28, 2014
1 parent 71dccb8 commit 89239ad
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Router/AbstractRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function parseRequest($path){

protected function logRequest(array $context = []){
if($this->logger InstanceOf Log\LoggerInterface){
$this->logger->info(__CLASS__, $context);
$this->logger->info(get_class($this), $context);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Router/ShortRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ function match($path){
foreach($this->patterns as $regex => $controller){
$matches = [];
if( preg_match($regex, $path, $matches) ){

$this->logRequest([
"request.pattern" => $regex,
"request.path" => $path,
"pattern.matches" => $matches,
]);

return call_user_func($controller, $matches);
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPUnit/Router/CliRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Chevron\Kernel\Router;

class TestLog extends \Psr\Log\AbstractLogger {
class CliTestLog extends \Psr\Log\AbstractLogger {
protected $container;
function log($level, $message, array $context = []){
$this->container = "{$level}|{$message}|" . count($context);
Expand Down Expand Up @@ -40,14 +40,14 @@ function test_logRequest(){
$_argv = ["frontControl", "name/space/class/method", "-f", "--val", "asdf"];

$router = new Router\CliRouter;
$logger = new TestLog;
$logger = new CliTestLog;

$router->setLogger($logger);

$path = $_argv[1];
$result = $router->match($path, array_slice($_argv, 2));

$expected = "info|Chevron\\Kernel\\Router\\AbstractRouter|7";
$expected = "info|Chevron\\Kernel\\Router\\CliRouter|7";

$this->assertEquals($expected, $logger->getLog());

Expand Down
38 changes: 38 additions & 0 deletions tests/PHPUnit/Router/ShortRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

use Chevron\Kernel\Router;

class ShortTestLog extends \Psr\Log\AbstractLogger {
protected $container;
function log($level, $message, array $context = []){
$this->container = "{$level}|{$message}|" . count($context);
}
function getLog(){
return $this->container;
}
}

class ShortRouterTest extends PHPUnit_Framework_TestCase {

function test_match_1(){
Expand Down Expand Up @@ -75,4 +85,32 @@ function test_match_3(){

}

function test_log_match(){

$path = "/namespace2/action.html";

$router = new Router\ShortRouter;

$logger = new ShortTestLog;

$router->setLogger($logger);

$Obj = new stdClass;

$router->regex("/namespace2\/(?P<act>.*?)\.html\$/i", function($matches)use($Obj){
$Obj->prop = ucwords($matches["act"]);
});

$router->regex("/namespace2\/(?P<act>.*)\$/i", function($matches)use($Obj){
$Obj->prop = strtr($matches["act"], ".", "-");
});

$router->match($path);

$expected = "info|Chevron\\Kernel\\Router\\ShortRouter|3";

$this->assertEquals($expected, $logger->getLog());

}

}

0 comments on commit 89239ad

Please sign in to comment.