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); }
function test_router_route() { assert_empty(route()); $r = route("get", "/index", "my_func"); assert_length_of($r, 1); 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], 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_output_before_filter() { function before_render($content_or_func, $layout, $locals, $view_path) { if (is_callable($content_or_func)) { } elseif (file_exists($view_path) && !array_key_exists('content', $locals)) { // a view file but not a layout $view_path = file_path(option('views_dir'), basename($content_or_func, ".html.php") . "_filtered.html.php"); } else { # it's a string $content_or_func .= "∞FILTERED∞"; } return array($content_or_func, $layout, $locals, $view_path); } $lorem = "Lorem ipsum dolor sit amet."; $html = render("Lorem %s dolor sit amet.", null, array('ipsum')); assert_match("/{$lorem∞FILTERED∞}/", $html); $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, array('lorem' => $lorem)); assert_match("/FILTERED/i", $html); assert_match("/{$lorem}/", $html); }
function test_functional_routing() { $path = TESTS_DOC_ROOT . '03-routing.php/'; $response = test_request($path . 'route0', 'GET'); assert_equal($response, 'route0'); $response = test_request($path . 'route1', 'GET'); assert_equal($response, 'route1'); $response = test_request($path . 'route2', 'GET'); assert_equal($response, 10); $response = test_request($path . 'route3', 'GET'); assert_equal($response, 20); $response = test_request($path . 'route4', 'GET'); assert_equal($response, 20); $response = test_request($path . 'route5', 'GET'); assert_equal($response, 'human'); $response = test_request($path . 'route5b', 'GET'); assert_equal($response, 'human'); $response = test_request($path . 'route6', 'GET'); assert_equal($response, 'human'); $response = test_request($path . 'route6b', 'GET'); assert_equal($response, 'human10'); $response = test_request($path . 'route6c', 'GET'); assert_equal($response, 'human10'); $response = test_request($path . 'route7/123', 'GET'); assert_equal($response, 123); $response = test_request($path . 'route7b/123', 'GET'); assert_equal($response, 123); $response = test_request($path . 'route7c/123', 'GET'); assert_equal($response, 1230); $response = test_request($path . 'route7d/123', 'GET'); assert_equal($response, 1230); $response = test_request($path . 'route7e/123', 'GET'); assert_equal($response, 2460); $response = test_request($path . 'route7f/123', 'GET'); assert_equal($response, 2460); $response = test_request($path . 'route7g', 'GET'); assert_equal($response, 200); $response = test_request($path . 'route7h', 'GET'); assert_equal($response, 200); $response = test_request($path . 'route8/123', 'GET'); assert_equal($response, 123); $response = test_request($path . 'route8b/123', 'GET'); assert_equal($response, 123); $response = test_request($path . 'route9/123', 'GET'); assert_equal($response, 2460); $response = test_request($path . 'route9b/123', 'GET'); assert_equal($response, 2460); $response = test_request($path . 'route10/123', 'GET'); assert_equal($response, 2460); $response = test_request($path . 'route10b/123', 'GET'); assert_equal($response, 2460); /* http methods dispatching */ $response = test_request($path . 'route11', 'GET'); assert_equal($response, 'GET'); $response = test_request($path . 'route11', 'POST'); var_dump($response); assert_equal($response, 'POST'); $response = test_request($path . 'route11', 'PUT'); assert_equal($response, 'PUT'); $response = test_request($path . 'route11', 'DELETE'); assert_equal($response, 'DELETE'); $response = test_request($path . 'route11', 'HEAD', true); assert_header($response, 'X-LIM-CTL', 'route11'); /* undefined route */ $response = test_request($path . 'unknown_route', 'GET'); assert_match('/Page not found/', $response); }
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); }
function test_http_response_status_code() { $response = test_request(TESTS_DOC_ROOT . '02-outputs.php/render0', 'GET', true); assert_match("/HTTP\\/1\\./", $response); assert_status($response, 200); }
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); }
function test_main_require_once_dir() { $root = dirname(dirname(__FILE__)); ob_start(); assert_empty(require_once_dir($root)); $files = require_once_dir($root, "AUTHORS"); assert_empty(ob_get_contents()); ob_clean(); assert_length_of($files, 1); assert_match('/AUTHORS$/', $files[0]); ob_start(); $files = require_once_dir($root, "CHANGES", false); assert_not_empty(ob_get_contents()); ob_clean(); $lib = $root.'/lib'; // pb because it loads abstract.php that conflict with tests that use abstracts // $limonade = $lib.'/limonade'; // // $files = require_once_dir($limonade); // assert_not_empty($files); $tests_lib = $root.'/tests/data/lib0'; $libs = array('a', 'b', 'c'); foreach($libs as $lib) assert_false(defined('TEST_LIB_'.strtoupper($lib))); $files = require_once_dir($tests_lib); assert_not_empty($files); assert_length_of($files, 3); foreach($libs as $lib) assert_true(defined('TEST_LIB_'.strtoupper($lib))); assert_empty(require_once_dir($root.'/tests/data/')); assert_true(is_array(require_once_dir($root.'/tests/data/'))); assert_empty(require_once_dir($root.'/tests/data/unknown_dir')); assert_true(is_array(require_once_dir($root.'/tests/data/unknown_dir'))); }
it("returns css stylesheet html tag", function () { $tag = stylesheet_link_tag("master"); assert_equal($tag, '<link href="/stylesheets/master.css" media="screen" rel="stylesheet" type="text/css" />'); }); it("merges attributes for stylesheet helper", function () { $tag = stylesheet_link_tag("master", array("media" => "print")); assert_equal($tag, '<link href="/stylesheets/master.css" media="print" rel="stylesheet" type="text/css" />'); }); }); describe("Helpers -> javascript_include_tag", function () { it("create html tag for javascript tag", function () { $tag = javascript_include_tag("master"); assert_equal($tag, '<script type="text/javascript" src="/javascripts/master.js"></script>'); }); }); describe("Helpers -> image_tag", function () { it("returns html tag for image", function () { $tag = image_tag("cupcake.png"); assert_equal($tag, '<img alt="cupcake" src="/images/cupcake.png" />'); }); it("it has image attributes", function () { $tag = image_tag("cupcake.png", array("class" => "main")); assert_match("/class=\"main\"/", $tag); }); }); describe("Helpers -> truncate", function () { it("should default to appending ...", function () { $str = "This is a fairly long string to test with!"; assert_equal(truncate($str, 5), "Th..."); }); });
function test_main_require_once_dir() { $root = dirname(dirname(__FILE__)); ob_start(); assert_empty(require_once_dir($root)); $files = require_once_dir($root, "AUTHORS"); ob_clean(); assert_length_of($files, 1); assert_match('/AUTHORS$/', $files[0]); $lib = $root . '/lib'; $limonade = $lib . '/limonade'; $files = require_once_dir($limonade); assert_not_empty($files); $tests_lib = $root . '/tests/data/lib0'; $libs = array('a', 'b', 'c'); foreach ($libs as $lib) { assert_false(defined('TEST_LIB_' . strtoupper($lib))); } $files = require_once_dir($tests_lib); assert_not_empty($files); assert_length_of($files, 3); foreach ($libs as $lib) { assert_true(defined('TEST_LIB_' . strtoupper($lib))); } assert_empty(require_once_dir($root . '/tests/data/')); assert_true(is_array(require_once_dir($root . '/tests/data/'))); assert_empty(require_once_dir($root . '/tests/data/unknown_dir')); assert_true(is_array(require_once_dir($root . '/tests/data/unknown_dir'))); }
public function test_match() { assert_match('/[a-z]{3}/', 'abc'); assert_fails(function () { assert_match('/[a-z]{3}/', '111'); }); }
function test_router_route() { assert_empty(route()); $r = route("get", "/index", "my_func"); assert_length_of($r, 1); assert_length_of($r[0], 4); 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"); $r = route("put", "/blog/:id", "my_update_func"); assert_length_of($r, 2); assert_length_of($r[1], 4); 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"); }
<?php describe("Session Management", function () { before(function () { $cookie_store = new CookieStore(array("session_key" => "_session_management", "secret" => "abcdef")); $session = new Session($cookie_store); return array($session); }); it("sets the session store", function ($args) { $session = $args[0]; assert_match("/CookieStore/", get_class($session->session_store)); }); it("sets value to session", function ($args) { $session = $args[0]; $session->set("id", "100"); assert_equal($session->get("id"), "100"); }); it("clears session", function ($args) { $session = $args[0]; $session->set("id", "100"); $session->clear(); assert_null($session->get('id')); }); it("saves and verifies data", function ($args) { $session = $args[0]; $session->set("id", "100"); $session->set("name", "fernyb"); $session->save(); $data = $session->session_store->session_data; $session_data = $session->session_store->verify($data); assert_equal($session_data["id"], "100");