Added simple test
This commit is contained in:
parent
43b8604d97
commit
91e005d2d4
2 changed files with 92 additions and 21 deletions
|
|
@ -9,17 +9,11 @@ class Config {
|
|||
|
||||
}
|
||||
|
||||
// Very simple router, two levels deep, module and controller
|
||||
// First deep element in path modules/{module}/controllers/index.php -> /module
|
||||
// Second deep element in path modules/{module}/controllers/{controller}.php -> /module/controller
|
||||
// Third deep element and more in path are variables into controller function
|
||||
// Filter the elements path with slugify function.
|
||||
|
||||
function get_controller($controller, $args=[]) {
|
||||
|
||||
if(is_file($controller)) {
|
||||
|
||||
include($controller);
|
||||
include_once($controller);
|
||||
|
||||
return call_user_func_array('controller', $args);
|
||||
|
||||
|
|
@ -40,20 +34,7 @@ function get_route($path_info) {
|
|||
|
||||
if($path_info!='') {
|
||||
|
||||
$arr_path=explode('/', $path_info);
|
||||
|
||||
if($arr_path[count($arr_path)-1]=='') {
|
||||
|
||||
unset($arr_path[count($arr_path)-1]);
|
||||
|
||||
}
|
||||
|
||||
unset($arr_path[0]);
|
||||
|
||||
foreach($arr_path as $k => $info) {
|
||||
|
||||
$arr_path[$k]=Utils::slugify($info);
|
||||
}
|
||||
$arr_path=filter_path($path_info);
|
||||
|
||||
switch (count($arr_path)) {
|
||||
|
||||
|
|
@ -104,3 +85,36 @@ function get_route($path_info) {
|
|||
|
||||
|
||||
}
|
||||
|
||||
function filter_path($path_info) {
|
||||
|
||||
$final_path=[];
|
||||
|
||||
$arr_path=explode('/', $path_info);
|
||||
|
||||
if($arr_path[count($arr_path)-1]=='') {
|
||||
|
||||
unset($arr_path[count($arr_path)-1]);
|
||||
|
||||
}
|
||||
|
||||
$z=1;
|
||||
|
||||
unset($arr_path[0]);
|
||||
|
||||
foreach($arr_path as $k => $info) {
|
||||
|
||||
$v=Utils::slugify($info);
|
||||
|
||||
if($v!='') {
|
||||
|
||||
$final_path[$z]=$v;
|
||||
|
||||
$z++;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return $final_path;
|
||||
|
||||
}
|
||||
|
|
|
|||
57
tests/RouterTest.php
Normal file
57
tests/RouterTest.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
// Simple test for simple routing
|
||||
|
||||
include('libraries/Routes.php');
|
||||
include('libraries/Utils.php');
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
\PhangoApp\PhaRouter\Config::$home_module='welcome';
|
||||
|
||||
class RouterTest extends TestCase {
|
||||
|
||||
//Test routes
|
||||
|
||||
public function testGetHome() {
|
||||
|
||||
$path_info='';
|
||||
|
||||
$this->assertEquals('Welcome to the real world!', \PhangoApp\PhaRouter\get_route($path_info));
|
||||
|
||||
}
|
||||
|
||||
public function testGetGoodController() {
|
||||
|
||||
$path_info='/welcome/index/good';
|
||||
|
||||
$this->assertEquals('All things are good!', \PhangoApp\PhaRouter\get_route($path_info));
|
||||
|
||||
}
|
||||
|
||||
|
||||
//Test paths format for not poison
|
||||
|
||||
public function testSimplePath() {
|
||||
|
||||
$this->assertEquals([], \PhangoApp\PhaRouter\filter_path(''));
|
||||
|
||||
$this->assertEquals([], \PhangoApp\PhaRouter\filter_path('/'));
|
||||
|
||||
$this->assertEquals([1 => 'nono'], \PhangoApp\PhaRouter\filter_path('/ñoño'));
|
||||
|
||||
$this->assertEquals([1 => 'nono', 2 => 'sucks'], \PhangoApp\PhaRouter\filter_path('/ñoño/sucks'));
|
||||
|
||||
$this->assertEquals([], \PhangoApp\PhaRouter\filter_path('/[]'));
|
||||
|
||||
$this->assertEquals([1 => 'asshole'], \PhangoApp\PhaRouter\filter_path('/[asshole]'));
|
||||
|
||||
$this->assertEquals([1 => 'asshole'], \PhangoApp\PhaRouter\filter_path('/"asshole'));
|
||||
|
||||
$this->assertEquals([1 => 'getcool'], \PhangoApp\PhaRouter\filter_path('/""""/getcool'));
|
||||
|
||||
$this->assertEquals([1 => 'etc', 2 => 'passwd'], \PhangoApp\PhaRouter\filter_path('/...../etc/passwd'));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue