Пример #1
0
function test_request_method()
{
    $env = env();
    $env['SERVER']['REQUEST_METHOD'] = null;
    assert_trigger_error("request_method");
    $methods = request_methods();
    foreach ($methods as $method) {
        $env['SERVER']['REQUEST_METHOD'] = $method;
        assert_equal(request_method($env), $method);
    }
    $env['SERVER']['REQUEST_METHOD'] = "POST";
    $env['POST']['_method'] = "PUT";
    assert_equal(request_method($env), "PUT");
    $env['POST']['_method'] = "DELETE";
    assert_equal(request_method($env), "DELETE");
    $env['POST']['_method'] = "UNKOWN";
    assert_trigger_error('request_method', array($env));
    assert_false(request_method());
}
Пример #2
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");
}
Пример #3
0
function test_test_trigger_error()
{
    assert_trigger_error("my_triggering_error_func", array(true));
}