Exemple #1
0
function test_output_render()
{
    $lorem = "Lorem ipsum dolor sit amet.";
    $q_lorem = preg_quote($lorem);
    # Testing standard rendering with sprint string
    assert_equal(render($lorem), $lorem);
    assert_equal(render($lorem, null, array('unused')), $lorem);
    assert_equal(render("Lorem %s dolor sit amet.", null, array('ipsum')), $lorem);
    assert_equal(render("Lorem %s dolor sit amet.", null, array('var1' => 'ipsum')), $lorem);
    $response = test_request(URL_FOR_OUTPUT_TEST . '/render0', 'GET');
    assert_equal($response, $lorem);
    $response = test_request(URL_FOR_OUTPUT_TEST . '/render1', 'GET');
    assert_equal($response, $lorem);
    # Testing rendering with a view (inline function case)
    $view = '_test_output_html_hello_world';
    $html = render($view);
    assert_match("/Hello World/", $html);
    assert_no_match("/{$q_lorem}/", $html);
    $html = render($view, null, array($lorem));
    assert_no_match("/{$q_lorem}/", $html);
    $html = render($view, null, array('lorem' => $lorem));
    assert_match("/{$q_lorem}/", $html);
    # Testing layout option
    $layout = '_test_output_html_my_layout';
    $html = render($lorem, $layout);
    assert_match("/{$q_lorem}/", $html);
    assert_match("/<title>Page title<\\/title>/", $html);
    # Testing layout + view (inline function case)
    $html = render($view, $layout);
    assert_match("/<title>Page title<\\/title>/", $html);
    assert_match("/Hello World/", $html);
    assert_no_match("/{$q_lorem}/", $html);
    $html = render($view, $layout, array('lorem' => $lorem));
    assert_match("/<title>Page title<\\/title>/", $html);
    assert_match("/Hello World/", $html);
    assert_match("/{$q_lorem}/", $html);
    # Testing layout + view (template files case)
    $views_dir = dirname(__FILE__) . '/apps/views/';
    option('views_dir', $views_dir);
    $view = 'hello_world.html.php';
    $layout = 'layouts/default.html.php';
    $html = render($view, $layout);
    assert_match("/<title>Page title<\\/title>/", $html);
    assert_match("/Hello World/", $html);
    assert_no_match("/{$q_lorem}/", $html);
    $html = render($view, $layout, array('lorem' => $lorem));
    assert_match("/<title>Page title<\\/title>/", $html);
    assert_match("/Hello World/", $html);
    assert_match("/{$q_lorem}/", $html);
}
Exemple #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");
}
Exemple #3
0
function test_functional_flash()
{
    $path = TESTS_DOC_ROOT . '07-flash.php/';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
    curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_URL, $path);
    $response = curl_exec($ch);
    assert_no_match("/ON DISPLAY/", $response);
    curl_setopt($ch, CURLOPT_URL, $path . 'two');
    $response = curl_exec($ch);
    assert_match("/ON DISPLAY 2/", $response);
    # Run a HEAD request on a page where there is no new flash
    # message set. Previous flash message should still be
    # there after this request.
    curl_setopt($ch, CURLOPT_URL, $path . 'four');
    curl_setopt($ch, CURLOPT_NOBODY, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $response = curl_exec($ch);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    curl_setopt($ch, CURLOPT_URL, $path . 'three');
    $response = curl_exec($ch);
    assert_match("/ON DISPLAY 3/", $response);
    curl_setopt($ch, CURLOPT_URL, $path . 'four');
    $response = curl_exec($ch);
    assert_match("/ON DISPLAY 4/", $response);
    assert_match("/NO FLASH MESSAGE ON NEXT PAGE/", $response);
    curl_setopt($ch, CURLOPT_URL, $path . 'five');
    $response = curl_exec($ch);
    assert_match("/REDIRECTED FROM INDEX FIVE/", $response);
    assert_match("/ON DISPLAY 6/", $response);
    curl_setopt($ch, CURLOPT_URL, $path . 'six');
    $response = curl_exec($ch);
    assert_no_match("/ON DISPLAY/", $response);
    curl_setopt($ch, CURLOPT_URL, $path . 'two');
    $response = curl_exec($ch);
    assert_no_match("/ON DISPLAY/", $response);
    curl_close($ch);
}
Exemple #4
0
function test_functional_errors()
{
    $path = TESTS_DOC_ROOT . '04-errors.php/';
    $response = test_request($path . 'no-error', 'GET', true);
    assert_status($response, 200);
    $response = test_request($path . 'unknow____url', 'GET', true);
    assert_status($response, 404);
    $response = test_request($path . 'not_found', 'GET', true);
    assert_status($response, 404);
    $response = test_request($path . 'server_error', 'GET', true);
    assert_status($response, 500);
    $response = test_request($path . 'halt', 'GET', true);
    assert_status($response, 500);
    assert_no_match("/This shouldn't be outputed/", $response);
    $response = test_request($path . 'trigger_error', 'GET', true);
    assert_status($response, 500);
    assert_no_match("/This shouldn't be outputed/", $response);
    $response = test_request($path . 'trigger_error/E_USER_WARNING', 'GET', true);
    assert_status($response, 200);
    assert_no_match("/This should be seen/", $response);
    $response = test_request($path . 'trigger_error/E_USER_NOTICE', 'GET', true);
    assert_status($response, 200);
    assert_no_match("/This should be seen/", $response);
    $response = test_request($path . 'halt1234', 'GET', true);
    assert_status($response, 501);
    assert_match("/A personnal error #1234/", $response);
}