function test_test_simple_assertions() { assert_true(true); assert_false(false); assert_equal("aaa", "aaa"); assert_not_equal("aaas", "aaa"); }
function test_http_ua_accepts() { $env = env(); $env['SERVER']['HTTP_ACCEPT'] = null; assert_true(http_ua_accepts('text/plain')); $env['SERVER']['HTTP_ACCEPT'] = 'text/html'; assert_true(http_ua_accepts('html')); $env['SERVER']['HTTP_ACCEPT'] = 'text/*; application/json'; assert_true(http_ua_accepts('html')); assert_true(http_ua_accepts('text/html')); assert_true(http_ua_accepts('text/plain')); assert_true(http_ua_accepts('application/json')); assert_false(http_ua_accepts('image/png')); assert_false(http_ua_accepts('png')); assert_true(defined('TESTS_DOC_ROOT'), "Undefined 'TESTS_DOC_ROOT' constant"); $response = test_request(TESTS_DOC_ROOT.'05-content_negociation.php', 'GET', false, array(), array("Accept: image/png")); assert_equal("Oops", $response); $response = test_request(TESTS_DOC_ROOT.'05-content_negociation.php', 'GET', false, array(), array("Accept: text/html")); assert_equal("<h1>HTML</h1>", $response); $response = test_request(TESTS_DOC_ROOT.'05-content_negociation.php', 'GET', false, array(), array("Accept: application/json")); assert_equal("json", $response); }
function test_file_is_binary() { assert_false(file_is_binary('my_file.txt')); assert_false(file_is_binary('my_file.TXT')); assert_false(file_is_binary('my_file.css')); assert_false(file_is_binary('my_file.csv')); assert_true(file_is_binary('my_file.jpg')); assert_true(file_is_binary('my_file.swf')); }
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()); }
function test_router_find_route() { route("get", "/index", "my_index_func"); route("get", "/new", "my_new_func"); route("post", "/create", "my_create_func"); route("get", "/edit/:id", "my_edit_func"); route("put", "/update/:id", "my_update_func"); route("delete", "/delete/:id", "my_delete_func"); route("get", "^/list/(\\d+)", "my_list_func"); $routes = route("get", "/*.jpg/:size", "my_jpeg"); assert_length_of($routes, 8); $r = route_find("GET", "/unkown"); assert_false($r); $r = route_find("GET", "/delete"); assert_false($r); $r = route_find("POST", "/create"); assert_equal($r["function"], "my_create_func"); $r = route_find("GET", "/edit"); assert_equal($r["function"], "my_edit_func"); $r = route_find("GET", "/edit/120"); assert_equal($r["function"], "my_edit_func"); assert_equal($r["params"]["id"], 120); $r = route_find("GET", "/limonade.jpg/thumb", 'my_jpeg'); assert_equal($r["function"], "my_jpeg"); assert_equal($r["params"][0], "limonade"); assert_equal($r["params"]["size"], "thumb"); route("get", "/index/*", "my_index_func2"); $routes = route("delete", "/delete/:id/:confirm", "my_delete_func2"); assert_length_of($routes, 10); $r = route_find("GET", "/index"); assert_equal($r["function"], "my_index_func"); $r = route_find("GET", "/index/ok"); assert_equal($r["function"], "my_index_func2"); $r = route_find("DELETE", "/delete"); assert_equal($r["function"], "my_delete_func"); $r = route_find("DELETE", "/delete/120"); assert_equal($r["function"], "my_delete_func"); $r = route_find("DELETE", "/delete/120/ok"); assert_equal($r["function"], "my_delete_func2"); $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"); /* testing route with special characters */ route("get", "/mañana/:when", "my_special_func"); $r = route_find("GET", "/mañana/123"); assert_equal($r["function"], "my_special_func"); assert_equal($r["params"]["when"], 123); $r = route_find("GET", "/mañana/après demain"); assert_equal($r["function"], "my_special_func"); assert_equal($r["params"]["when"], "après demain"); }
function test_main_url_for() { assert_equal(url_for(''), '/'); assert_equal(url_for('/'), '/'); assert_equal(url_for('test'), '/test'); assert_equal(url_for('mañana'), '/'.rawurlencode("mañana")); assert_equal(url_for('test', 1, 2), '/test/1/2'); assert_equal(url_for('one', 'two', 'three'), '/one/two/three'); assert_equal(url_for('one', 0, 'three'), '/one/0/three'); assert_equal(url_for('one', '', 'three'), '/one/three'); assert_equal(url_for('one', null, 'three'), '/one/three'); assert_equal(url_for('my/hash#test'), '/my/hash#test'); $site_url = 'http://www.limonade-php.net'; assert_true((bool) filter_var_url($site_url)); assert_true((bool) filter_var_url('http://example.com')); assert_true((bool) filter_var_url('http://example.com:2000/')); assert_true((bool) filter_var_url('https://www.example.com:2000')); assert_true((bool) filter_var_url('http://test.example.com/?var1=true&var2=34')); assert_false(filter_var_url('not an url')); assert_equal(url_for($site_url), $site_url); //var_dump(url_for($site_url, 'examples')); assert_equal(url_for($site_url, 'examples'), $site_url.'/examples'); $ssite_url = 'https://www.limonade-php.net'; assert_equal(url_for($ssite_url), $ssite_url); option('base_uri', '?'); $url = url_for('test', array('p1' => 'lorem', 'p2' => 'ipsum')); assert_equal($url,'?/test&p1=lorem&p2=ipsum'); $url = url_for('test', array(0 => 'lorem', 'p2' => 1)); assert_equal($url,'?/test&0=lorem&p2=1'); $url = url_for('test', array('p1' => 'mañana')); assert_equal($url,'?/test&p1='.rawurlencode("mañana")); option('base_uri', '/api'); $url = url_for('test', array('p1' => 'lorem', 'p2' => 'ipsum')); assert_equal($url,'/api/test?p1=lorem&p2=ipsum'); }
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'; $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'))); }
assert_equals("/your regex/", $smoothie->filter); }); should("accept --filter", function () { $smoothie = new Smoothie(array("--filter", "your regex")); assert_equals("/your regex/", $smoothie->filter); }); should("add slashes if missing", function () { $smoothie = new Smoothie(array("-n", "your regex")); assert_equals("/your regex/", $smoothie->filter); }); should("not add slahes if already there", function () { $smoothie = new Smoothie(array("-n", "/your regex/")); assert_equals("/your regex/", $smoothie->filter); }); should("return true if no filter was set", function () { $smoothie = new Smoothie(); assert_true($smoothie->passes_filter("my awesome test")); }); context("tests with passes_filter()", function () { setup(function () { transient()->smoothie = new Smoothie(array("-n", "/my awesome test/")); }); should("return true if filter was set and passes", function () { assert_true(transient()->smoothie->passes_filter("this is my awesome test that is awesome")); }); should("return false if filter was set and does not pass", function () { assert_false(transient()->smoothie->passes_filter("blah")); }); }); }); });
function test_is_property_set() { $c = new TestActiveData(); assert_true($c->is_set('foo')); assert_false($c->is_set('bar')); }
function test_router_find_route() { route("get", "/index", "my_index_func"); route("get", "/new", "my_new_func"); route("post", "/create", "my_create_func"); route("get", "/edit/:id", "my_edit_func"); route("put", "/update/:id", "my_update_func"); route("delete", "/delete/:id", "my_delete_func"); route("get", "^/list/(\\d+)", "my_list_func"); $routes = route("get", "/*.jpg/:size", "my_jpeg"); assert_length_of($routes, 8); $r = route_find("GET", "/unkown"); assert_false($r); $r = route_find("GET", "/delete"); assert_false($r); $r = route_find("POST", "/create"); assert_equal($r["function"], "my_create_func"); $r = route_find("GET", "/edit"); assert_equal($r["function"], "my_edit_func"); $r = route_find("GET", "/edit/120"); assert_equal($r["function"], "my_edit_func"); assert_equal($r["params"]["id"], 120); $r = route_find("GET", "/limonade.jpg/thumb", 'my_jpeg'); assert_equal($r["function"], "my_jpeg"); assert_equal($r["params"][0], "limonade"); assert_equal($r["params"]["size"], "thumb"); route("get", "/index/*", "my_index_func2"); $routes = route("delete", "/delete/:id/:confirm", "my_delete_func2"); assert_length_of($routes, 10); $r = route_find("GET", "/index"); assert_equal($r["function"], "my_index_func"); $r = route_find("GET", "/index/ok"); assert_equal($r["function"], "my_index_func2"); $r = route_find("DELETE", "/delete"); assert_equal($r["function"], "my_delete_func"); $r = route_find("DELETE", "/delete/120"); assert_equal($r["function"], "my_delete_func"); $r = route_find("DELETE", "/delete/120/ok"); assert_equal($r["function"], "my_delete_func2"); $r = route_find("GET", "/list/120"); assert_equal($r["function"], "my_list_func"); }
function expect_no_error($callback, $error = null, $msg = null) { $last_error = mut_fetch_last_error($callback, $error); if (is_null($msg) && !$last_error['matched']) { $msg = 'Expected no error'; if (!$last_error['match']['message']) { $msg .= ' matching ' . $error['match']; $msg .= ' [got: '; $msg .= empty($last_error['message']) ? 'none' : $last_error['message']; $msg .= ']'; } if (!$last_error['match']['severity']) { $msg .= ' with severity '; $msg .= join(', ', mut_get_error_names($error['severity'])); $msg .= ' [got: '; $msg .= join(', ', mut_get_error_names($last_error['severity'])); $msg .= ']'; } } return assert_false($last_error['matched'], $msg); }