示例#1
0
function test_router_build_route()
{
    assert_trigger_error('route_build', array('UNKOWN', '/', 'aaa'));
    /* testing route returned array */
    $r = route_build("GET", "/index", 'get_index');
    assert_equal($r["method"], "GET");
    assert_equal($r["pattern"], "#^/index(?:/*?)?\$#i");
    assert_empty($r["names"]);
    assert_equal($r["function"], "get_index");
    /* testing very simple route with no parameters */
    assert_match($r["pattern"], "/index");
    assert_no_match($r["pattern"], "/other");
    assert_no_match($r["pattern"], "/");
    assert_no_match($r["pattern"], "/index/1");
    /* testing empty route */
    $r = route_build("GET", "/", 'get_index');
    assert_match($r["pattern"], "/");
    assert_match($r["pattern"], "");
    assert_no_match($r["pattern"], "/test2");
    $r = route_build("GET", "", 'get_index');
    assert_match($r["pattern"], "/");
    assert_match($r["pattern"], "");
    assert_no_match($r["pattern"], "/test2");
    /* testing single asterisk routes */
    $r = route_build("GET", "/test/*", 'get_index');
    assert_match($r["pattern"], "/test");
    assert_match($r["pattern"], "/Test");
    assert_match($r["pattern"], "/test/");
    assert_match($r["pattern"], "/test/truc");
    assert_match($r["pattern"], "/test/truc/");
    assert_match($r["pattern"], "/test/truc////");
    assert_no_match($r["pattern"], "/test/truc/2");
    assert_no_match($r["pattern"], "/test2");
    assert_equal($r["names"][0], 0);
    preg_match($r["pattern"], "/test/foo////", $matches);
    assert_length_of($matches, 2);
    assert_equal($matches[1], "foo");
    $r = route_build("GET", "/test/*/two", 'get_index');
    assert_match($r["pattern"], "/test/truc/two");
    assert_match($r["pattern"], "/test/truc/two/");
    assert_no_match($r["pattern"], "/test");
    assert_no_match($r["pattern"], "/test/");
    assert_no_match($r["pattern"], "/test/truc");
    assert_no_match($r["pattern"], "/test/truc/2");
    assert_no_match($r["pattern"], "/test2");
    assert_no_match($r["pattern"], "/test/truc/2/two");
    assert_no_match($r["pattern"], "/test/truc/two/three");
    assert_equal($r["names"][0], 0);
    preg_match($r["pattern"], "/test/foo/two/", $matches);
    assert_length_of($matches, 2);
    assert_equal($matches[1], "foo");
    /* testing single asterisk routes with params names */
    $r = route_build("GET", array("/test/*/two", array("first")), 'get_index');
    assert_match($r["pattern"], "/test/truc/two");
    assert_match($r["pattern"], "/test/truc/two/");
    assert_equal($r["names"][0], "first");
    /* testing double asterisk routes */
    $r = route_build("GET", "/test/**", 'get_index');
    assert_match($r["pattern"], "/test");
    assert_match($r["pattern"], "/TEST");
    assert_match($r["pattern"], "/test/");
    assert_match($r["pattern"], "/test/truc");
    assert_match($r["pattern"], "/test/truc/2");
    assert_no_match($r["pattern"], "/test2");
    assert_equal($r["names"][0], 0);
    preg_match($r["pattern"], "/test/foo", $matches);
    assert_length_of($matches, 2);
    assert_equal($matches[1], "foo");
    $r = route_build("GET", "/test/**/two/", 'get_index');
    assert_match($r["pattern"], "/test/truc/two");
    assert_match($r["pattern"], "/test/truc/one/two");
    assert_match($r["pattern"], "/Test/truc/one/two/");
    assert_no_match($r["pattern"], "/test");
    assert_no_match($r["pattern"], "/test/");
    assert_no_match($r["pattern"], "/test/truc");
    assert_no_match($r["pattern"], "/test/truc/2");
    assert_no_match($r["pattern"], "/test2");
    assert_no_match($r["pattern"], "/test/truc/one/two/three");
    preg_match($r["pattern"], "/test/foo/bar/two", $matches);
    assert_length_of($matches, 2);
    assert_equal($matches[1], "foo/bar");
    /* testing named parameters routes */
    $r = route_build("GET", "/test/:bob", 'get_index');
    assert_match($r["pattern"], "/test");
    assert_match($r["pattern"], "/test/");
    assert_match($r["pattern"], "/test/truc");
    assert_no_match($r["pattern"], "/test/truc/2");
    assert_no_match($r["pattern"], "/test2");
    assert_equal($r["names"][0], "bob");
    /* testing regexp route */
    $r = route_build("GET", "^/my/(\\d+)/own/regexp", 'get_index');
    assert_match($r["pattern"], "/my/12/own/regexp");
    /* testing a complex route and parameters names*/
    $r = route_build("GET", "/test/:my/*/complex/**/:route", 'get_index');
    assert_match($r["pattern"], "/test/my/first/complex/very-big/route/69");
    assert_no_match($r["pattern"], "/test/truc/2");
    assert_equal($r["names"][0], "my");
    assert_equal($r["names"][1], 1);
    assert_equal($r["names"][2], "2");
    assert_equal($r["names"][3], "route");
    /* testing typical route used for static files */
    $r = route_build("GET", "/*.jpg/:size", 'get_index');
    assert_match($r["pattern"], "/limonade.jpg");
    assert_match($r["pattern"], "/limonade.jpg/");
    assert_match($r["pattern"], "/limonade.jpg/thumb");
    /* testing a complex route and parameters names*/
    $path = "/test/:my/*/complex/**/:route";
    $params = array("mmy", "second", "lazy", null);
    $r = route_build("GET", array($path, $params), 'get_index');
    assert_match($r["pattern"], "/test/my/first/complex/very-big/route/69");
    assert_no_match($r["pattern"], "/test/truc/2");
    assert_equal($r["names"][0], "mmy");
    assert_equal($r["names"][1], "second");
    assert_equal($r["names"][2], "lazy");
    assert_equal($r["names"][3], "route");
    /* testing a route with special characters */
    $r = route_build("GET", "/mañana/:when", 'get_index');
    assert_match($r["pattern"], "/mañana/tomorrow");
}
示例#2
0
/**
 * Add route if required params are provided.
 * Delete all routes if null is passed as a unique argument
 * Return all routes
 *
 * @access private
 * @param string $method
 * @param string $path_or_array
 * @param string $func
 * @return array
 */
function route()
{
    static $routes = array();
    $nargs = func_num_args();
    if ($nargs > 0) {
        $args = func_get_args();
        if ($nargs === 1 && is_null($args[0])) {
            $routes = array();
        } else {
            if ($nargs < 3) {
                trigger_error("Missing arguments for route()", E_USER_ERROR);
            } else {
                $method = $args[0];
                $path_or_array = $args[1];
                $func = $args[2];
                $routes[] = route_build($method, $path_or_array, $func);
            }
        }
    }
    return $routes;
}