Skip to content

Commit

Permalink
Adding tests for routes features
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrice Luraine committed Nov 21, 2009
1 parent 8f798ab commit ba6569d
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions tests/router.php
Expand Up @@ -150,20 +150,36 @@ function test_router_route()

$r = route("get", "/index", "my_func");
assert_length_of($r, 1);
assert_length_of($r[0], 4);
assert_length_of($r[0], 5);
assert_equal($r[0]["method"], "GET");
assert_equal($r[0]["pattern"], "#^/index(?:/*?)?$#i");
assert_empty($r[0]["names"]);
assert_equal($r[0]["function"], "my_func");
assert_empty($r[0]["options"]);

$r = route("put", "/blog/:id", "my_update_func");
assert_length_of($r, 2);
assert_length_of($r[1], 4);
assert_length_of($r[1], 5);
assert_equal($r[1]["method"], "PUT");
assert_match($r[1]["pattern"], "/blog/102");
assert_length_of($r[1]["names"], 1);
assert_equal($r[1]["names"][0], "id");
assert_equal($r[1]["function"], "my_update_func");
assert_empty($r[1]["options"]);

$r = route("post", "/blog/:id", "my_post_func", array('params' => array('extra' => 10)));
assert_length_of($r[2], 5);
assert_equal($r[2]["method"], "POST");
assert_match($r[2]["pattern"], "/blog/102");
assert_length_of($r[2]["names"], 1);
assert_equal($r[2]["names"][0], "id");
assert_equal($r[2]["function"], "my_post_func");
assert_not_empty($r[2]["options"]);
assert_not_empty($r[2]["options"]['params']);
assert_equal($r[2]["options"]['params']['extra'], 10);

$r = route("get", "/blog/:id", "my_get_func", array('params' => array('id' => 10)));
assert_match($r[2]["pattern"], "/blog/102");
}

function test_router_find_route()
Expand Down Expand Up @@ -223,6 +239,28 @@ function test_router_find_route()
$r = route_find("GET", "/list/120");
assert_equal($r["function"], "my_list_func");

/* testing parameterized functions */
$extra_p = array(123, 'id' => 123, 'name' => 'abc');

route( "get", "/no/cat/:id", "my_p_func");
route( "get", "/with/cat/:id", "my_p_func", array('params' => $extra_p));
$routes = route( "get", "/indexed/cat/*", "my_p_func", array('params' => $extra_p));

$r = route_find("GET", "/no/cat/21");
assert_equal($r["function"], "my_p_func");
assert_equal($r["params"]["id"], 21);

$r = route_find("GET", "/with/cat/21");
assert_equal($r["function"], "my_p_func");
assert_equal($r["params"]["id"], 21);
assert_equal($r["params"]["name"], "abc");

$r = route_find("GET", "/indexed/cat/21");
assert_equal($r["function"], "my_p_func");
assert_equal($r["params"][0], 21);
assert_equal($r["params"]["id"], 123);
assert_equal($r["params"]["name"], "abc");

}

end_test_case();

0 comments on commit ba6569d

Please sign in to comment.