Skip to content

Commit 90cfa4f

Browse files
committed
Added examples
Filled phpDoc Improved tests
1 parent 64a2364 commit 90cfa4f

File tree

10 files changed

+165
-55
lines changed

10 files changed

+165
-55
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PhpRouter
1+
# PhpRouter PhpRouter ![alt text](https://img.shields.io/badge/licence-BSD--3--Clause-blue.svg "Licence") ![alt text](https://img.shields.io/badge/tests-19%20%2F%2019-brightgreen.svg "Tests") ![alt text](https://img.shields.io/badge/coverage-100%25-green.svg "Coverage")
22
PHP router inspired on FatFree, CakePHP and own invention.
33

44
It allows to use anonymous functions as callback as well as OOP classes and methods.

examples/.htaccess

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<IfModule mod_rewrite.c>
2+
RewriteEngine on
3+
RewriteCond %{REQUEST_FILENAME} !-f
4+
RewriteCond %{REQUEST_FILENAME} !-d
5+
RewriteCond %{REQUEST_FILENAME} !-l
6+
RewriteRule .* example.php?url=$1 [QSA,L]
7+
</IfModule>

examples/example.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
require __DIR__ . '../vendor/autoload.php';
3+
4+
use PhpRouter\Route;
5+
use PhpRouter\RouteCollection;
6+
use PhpRouter\Router;
7+
use PhpRouter\RouteRequest;
8+
9+
class A
10+
{
11+
function index()
12+
{
13+
echo "I am the index!";
14+
}
15+
16+
static function showParams($params)
17+
{
18+
print_r($params);
19+
}
20+
}
21+
22+
$routing = new RouteCollection();
23+
24+
$routing->attach(new Route('GET /', function(){
25+
echo 'Hello World';
26+
}));
27+
28+
$routing->attach(new Route('GET /page', function(){
29+
echo 'Some page...';
30+
}));
31+
32+
$routing->attach(new Route('GET /page/@id', ['id' => '\d+'], function($params){
33+
echo 'Page no. ' . $params['id'];
34+
}));
35+
36+
$routing->attach(new Route('GET /mac/@mac', ['mac' => '..:..:..:..:..:..'], function($params){
37+
echo 'Mac Address: ' . $params['mac'];
38+
}));
39+
40+
$routing->attach(new Route('GET /index', 'A->index'));
41+
42+
$routing->attach(new Route('GET /index/@data', 'A->showParams'));
43+
44+
(new Router(new RouteRequest(), $routing))->run();

src/Route.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ private function parseRoute($route)
112112
}
113113

114114
/**
115+
* Build regex, required for Router to check if current path mach
116+
*
115117
* @return string
116118
*/
117119
private function preparePattern()
@@ -155,7 +157,11 @@ public function getPattern()
155157
return $this->pattern;
156158
}
157159

158-
160+
/**
161+
* Parse named params and build associative array with them
162+
*
163+
* @param $requestUrl
164+
*/
159165
public function parseParams($requestUrl)
160166
{
161167
preg_match($this->getPattern(), $requestUrl, $values);
@@ -164,7 +170,7 @@ public function parseParams($requestUrl)
164170
unset($values[0]);
165171
$values = array_values($values);
166172

167-
if (count($values) != count($names['name']) + 1) {
173+
if (count($values) == count($names['name'])) {
168174
foreach ($names['name'] as $key => $name) {
169175
$this->namedParams[$name] = $values[$key];
170176
}
@@ -188,6 +194,8 @@ public function dispatch()
188194
}
189195

190196
/**
197+
* Call class and method defined as callback
198+
*
191199
* @param string $class
192200
* @param string $method
193201
* @param array $params

src/RouteCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
class RouteCollection extends RouteIterator
88
{
99
/**
10+
* Attach new Route to the collection
11+
*
1012
* @param Route $route
1113
*/
1214
public function attach(Route $route)

src/RouteRequest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ private function setRequestUrl()
105105
}
106106

107107
/**
108+
* Check if request comes from https or http
109+
*
108110
* @return string
109111
*/
110112
private function getRequestProtocol()

src/Router.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public function __construct(RouteRequest $routeRequest, RouteCollection $routeCo
2828
}
2929

3030
/**
31+
* Run Router and dispatch requested Route
32+
*
3133
* @return mixed
3234
*/
3335
public function run()
@@ -46,6 +48,8 @@ public function run()
4648
}
4749

4850
/**
51+
* Match if current Route match to the request url, method and type
52+
*
4953
* @param Route $route
5054
* @return bool
5155
*/

tests/RouteTest.php

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use PhpRouter\Route;
55
use PHPUnit_Framework_TestCase;
66

7+
require 'Test.php';
8+
79
/**
810
* Class RouteTest
911
*/
@@ -16,94 +18,87 @@ public function shouldCreateCorrectRouteObject()
1618
{
1719
$route = new Route('GET /test/page.html', 'A->index');
1820

21+
$this->assertEquals('|^/test/page.html$|', $route->getPattern());
1922
$this->assertEquals(['GET'], $route->getMethods());
20-
$this->assertEquals('/test/page.html', $route->getUrl());
2123
$this->assertEquals('sync', $route->getType());
24+
$this->assertEquals('/test/page.html', $route->getUrl());
2225
}
2326

2427
/**
2528
* @test
2629
*/
27-
public function shouldCreateRouteWithDefiniedNamedParam()
30+
public function shouldCreateRouteWithNamedParam()
2831
{
29-
$route = new Route('GET|POST /some_page/@id', ['id' => '\d{2}\-\w{4}'], function($param){
30-
return $param['id'];
31-
});
32+
$route = new Route('GET|POST /some_page/@id', 'A->index');
33+
$this->assertEquals('|^/some_page/([\w-]+)$|', $route->getPattern());
34+
}
3235

33-
$route->parseParams('/some_page/12-zaqw');
34-
$this->assertEquals('12-zaqw', $route->dispatch());
36+
/**
37+
* @test
38+
*/
39+
public function shouldCreateRouteWithNamedParamMatchToRegex()
40+
{
41+
$route = new Route('GET|POST /some_page/@id', ['id' => '[a-z]{2}\-[a-z]{4}'], 'A->index');
42+
$this->assertEquals('|^/some_page/([a-z]{2}\-[a-z]{4})$|', $route->getPattern());
3543
}
3644

3745
/**
3846
* @test
3947
*/
40-
public function shouldThrowWrongCallbackException()
48+
public function shouldCallAndExecuteAnonymousFunction()
4149
{
42-
$this->setExpectedException('Exception');
43-
$route = new Route('GET /test.html', 'nothingSpecial');
44-
$route->dispatch();
50+
$route = new Route('GET /test/@data.html [ajax]', ['data' => '[a-z]{2}:[a-z]{3}'], function($params){
51+
return $params['data'];
52+
});
53+
54+
$route->parseParams('/test/xx:www.html');
55+
$this->assertEquals('xx:www', $route->dispatch());
4556
}
4657

4758
/**
4859
* @test
4960
*/
50-
public function shouldThrowInvalidRouteException()
61+
public function shouldCallSpecifiedCallback()
5162
{
52-
$this->setExpectedException('Exception');
53-
new Route('/test.html', 'A->index');
63+
$route = new Route('GET /test/page/@number.html', '\PhpRouter\Test\Test->page');
64+
65+
$route->parseParams('/test/page/42.html');
66+
$this->assertEquals(42, $route->dispatch());
5467
}
5568

5669
/**
5770
* @test
5871
*/
59-
public function shouldThrowInvalidTypeException()
72+
public function shouldThrowWrongCallbackException()
6073
{
6174
$this->setExpectedException('Exception');
62-
new Route('GET /test.html [not_exists]', 'A->index');
75+
(new Route('GET /test.html', 'nothingSpecial'))->dispatch();
6376
}
6477

6578
/**
6679
* @test
6780
*/
68-
public function shouldCallAndExecuteAnonymousFunction()
81+
public function shouldThrowInvalidRouteException()
6982
{
70-
$route = new Route('GET /test.html [ajax]', function(){
71-
echo 'XXX';
72-
});
73-
74-
ob_start();
75-
$route->dispatch();
76-
$result = ob_get_clean();
77-
78-
$this->assertEquals('XXX', $result);
83+
$this->setExpectedException('Exception');
84+
new Route('/test.html', 'A->index');
7985
}
8086

8187
/**
8288
* @test
8389
*/
84-
public function shouldThrowClasOrMethodNotFoundException()
90+
public function shouldThrowInvalidTypeException()
8591
{
8692
$this->setExpectedException('Exception');
87-
$route = new Route('GET /test/page.html', 'A->test');
88-
$route->dispatch();
93+
new Route('GET /test.html [not_exists]', 'A->index');
8994
}
9095

9196
/**
9297
* @test
9398
*/
94-
public function shouldCallSpecifiedCallback()
99+
public function shouldThrowClassOrMethodNotFoundException()
95100
{
96-
// $class = $this->getMockBuilder('Test')->setMethods(['login'])->getMock();
97-
//
98-
// $class->expects($this->any())
99-
// ->method('login');
100-
//
101-
// $route = new Route('GET /test/page.html', 'Test->login');
102-
//
103-
// ob_start();
104-
// $route->dispatch(['someData']);
105-
// $result = ob_get_clean();
106-
//
107-
// $this->assertEquals('logged!', $result);
101+
$this->setExpectedException('Exception');
102+
(new Route('GET /test/page.html', 'A->test'))->dispatch();
108103
}
109104
}

tests/RouterTest.php

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ class RouterTest extends PHPUnit_Framework_TestCase
1616

1717
public function setUp()
1818
{
19-
parent::setUp();
20-
2119
$this->collection = new RouteCollection();
22-
$this->collection->attach(new Route('DELETE /', function(){ return 1; }));
20+
$this->collection->attach(new Route('GET /', function(){ return 1; }));
2321
$this->collection->attach(new Route('GET /about_me', function(){ return 2; }));
24-
$this->collection->attach(new Route('GET /contact', function(){ return 3; }));
25-
$this->collection->attach(new Route('GET /contact [ajax]', function(){ return 4; }));
26-
$this->collection->attach(new Route('POST /contact [ajax]', function(){ return 4; }));
22+
$this->collection->attach(new Route('POST /about_me', function(){ return 3; }));
23+
$this->collection->attach(new Route('GET /contact/@id', ['id' => '\w{3}\:\w{3}'], function($p){ return 'regex_' . $p['id']; }));
24+
$this->collection->attach(new Route('GET /contact/@id', function($p){ return $p['id']; }));
25+
$this->collection->attach(new Route('GET /ajax [ajax]', function(){ return 4; }));
26+
$this->collection->attach(new Route('GET /nonajax', function(){ return 5; }));
27+
28+
parent::setUp();
2729
}
2830

2931
/**
@@ -35,14 +37,19 @@ public function shouldMatchRouteAndDispatch()
3537
$router = new Router($request, $this->collection);
3638

3739
$this->assertEquals(2, $router->run());
40+
41+
$request = $this->getRequestMock('POST', '/about_me');
42+
$router = new Router($request, $this->collection);
43+
44+
$this->assertEquals(3, $router->run());
3845
}
3946

4047
/**
4148
* @test
4249
*/
43-
public function shouldMismatchAjaxRoute()
50+
public function shouldCallAjaxRouteWithAjaxFalse()
4451
{
45-
$request = $this->getRequestMock('GET', '/contacts', true);
52+
$request = $this->getRequestMock('GET', '/ajax');
4653
$router = new Router($request, $this->collection);
4754

4855
$this->assertNull($router->run());
@@ -51,14 +58,41 @@ public function shouldMismatchAjaxRoute()
5158
/**
5259
* @test
5360
*/
54-
public function shouldMatchAjaxRoute()
61+
public function shouldCallAjaxRouteWithAjaxTrue()
5562
{
56-
$request = $this->getRequestMock('POST', '/contact', true);
63+
$request = $this->getRequestMock('GET', '/ajax', true);
5764
$router = new Router($request, $this->collection);
5865

5966
$this->assertEquals(4, $router->run());
6067
}
6168

69+
/**
70+
* @test
71+
*/
72+
public function shouldCallNonAjaxRouteWithAjax()
73+
{
74+
$request = $this->getRequestMock('GET', '/nonajax', true);
75+
$router = new Router($request, $this->collection);
76+
77+
$this->assertEquals(5, $router->run());
78+
}
79+
80+
/**
81+
* @test
82+
*/
83+
public function shouldCallRouteWithParam()
84+
{
85+
$request = $this->getRequestMock('GET', '/contact/test_123');
86+
$router = new Router($request, $this->collection);
87+
88+
$this->assertEquals('test_123', $router->run());
89+
90+
$request = $this->getRequestMock('GET', '/contact/xxx:xxx');
91+
$router = new Router($request, $this->collection);
92+
93+
$this->assertEquals('regex_xxx:xxx', $router->run());
94+
}
95+
6296
/**
6397
* @param string $method
6498
* @param string $url

tests/Test.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace PhpRouter\Test;
3+
4+
/**
5+
* Class Test
6+
* @package PhpRouter\Test
7+
*/
8+
class Test
9+
{
10+
public function page($params)
11+
{
12+
return $params['number'];
13+
}
14+
}

0 commit comments

Comments
 (0)