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_test_assert_request() { assert_true(defined('TESTS_DOC_ROOT'), "Undefined 'TESTS_DOC_ROOT' constant"); $response = test_request(TESTS_DOC_ROOT . '00-empty.php', 'GET', true); assert_header($response, 'Content-type'); assert_header($response, 'Content-type', 'text/html'); assert_header($response, 'Content-Type'); assert_header($response, 'Content-Type', 'text/html'); }
function test_request_method_is_allowed() { assert_true(request_method_is_allowed("GET")); assert_true(request_method_is_allowed("get")); assert_true(request_method_is_allowed("POST")); assert_true(request_method_is_allowed("PUT")); assert_true(request_method_is_allowed("DELETE")); assert_true(request_method_is_allowed("HEAD")); }
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_functional_session() { $response = test_request(TESTS_DOC_ROOT . '06-session.php/', 'GET', false); // In http://www.php.net/manual/en/function.session-name.php: // // > The session name references the session id in cookies and URLs. It // should contain only alphanumeric characters; it should be short and // descriptive (i.e. for users with enabled cookie warnings). If name is // specified, the name of the current session is changed to its value. assert_true(ctype_alnum($response)); assert_equal($response, "LIMONADE" . str_replace('.', 'x', LIMONADE)); }
$mapJson = "{\n \"stringMap\": {\n \"a\": \"A\",\n \"b\": \"B\"\n },\n\n \"boolMap\": {\n \"false\": \"False\",\n \"true\": \"True\"\n },\n\n \"byteMap\": {\n \"1\": \"one\",\n \"2\": \"two\"\n },\n \"doubleMap\":\n {\n \"0.2\": \"0.two\",\n \"0.1\": \"0.one\"\n },\n \"enumMap\": {\n\n \"1\": \"male\",\n \"2\": \"female\"\n }\n}"; $mapStruct->readFromJson($mapJson); assert_array_equals($mapStruct->stringMap, $stringMap); assert_array_equals($mapStruct->boolMap, $boolMap); assert_array_equals($mapStruct->byteMap, $byteMap); assert_array_equals($mapStruct->enumMap, $enumMap); /** * Test mixed structure */ $mixedJson = "{\n \"a\": [\n \n ],\n \"b\": [\n {\n \"a\": 5\n\n }\n ],\n \"c\": {\n \"fire\": -191,\n \"flame\": -8\n },\n \"d\": {\n\n \n },\n \"e\": [\n 1, 2, 3, 4\n ]\n}"; $mixedStruct->readFromJson($mixedJson); assert_array_equals($mixedStruct->a, array()); assert_array_equals($mixedStruct->c, array('fire' => -191, 'flame' => -8)); $tSuperStruct = new mySuperSimpleStruct(array('a' => 5)); $tArr = array(0 => $tSuperStruct); assert_true($mixedStruct->b == $tArr); assert_array_equals($mixedStruct->d, array()); assert_array_equals($mixedStruct->e, array(1 => true, 2 => true, 3 => true, 4 => true)); /** * Test Set */ $setJson1 = "{\n \"a\": [\n 8, 16, 4, 15\n ]\n}"; $setStruct->readFromJson($setJson1); assert_array_equals($setStruct->a, array(4 => true, 8 => true, 15 => true, 16 => true)); $setJson2 = "{\n \"a\": [\n \n ]\n}"; $setStruct->readFromJson($setJson2); assert_array_equals($setStruct->a, array()); $setJson3 = "{\n \"a\": [\n 1, 2, 1099511627775\n ]\n}"; test_exception($setStruct, $setJson3); /** * Test for simple structure
context("nested in a context", function () { setup(function () { transient()->setup3 = true; }); should("have called all three setups", function () { assert_equals(true, transient()->setup1); assert_equals(true, transient()->setup2); assert_equals(true, transient()->setup3); }); should("have the proper test description", function () { assert_equals("a context nested in a context nested in a context should have the proper test description", Smoothie::instance()->test_description()); }); }); }); should("have ran teardown", function () { assert_true($GLOBALS['I_RAN_TEARDOWN']); }); context("with no setup", function () { should("work", function () { assert_true(true); }); }); // TODO should("cause a failure", function () { assert_equals(1, 2); }); // TODO should("cause an error", function () { throw new \Exception("exception!"); }); });
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_benchmark() { $bench = benchmark(); assert_true(is_array($bench)); assert_true(array_key_exists('execution_time', $bench)); if (function_exists('memory_get_usage')) { assert_true(defined('LIM_START_MEMORY')); assert_true(array_key_exists('start_memory', $bench)); assert_equal(LIM_START_MEMORY, $bench['start_memory']); } }
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'))); }
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('my/hash#test'), '/my/hash#test'); $site_url = 'http://www.limonade-php.net'; assert_true((bool) filter_var_url($site_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); $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")); }
write_to_read($w, $r); assert_true(!isset($r->im_default)); // Optional gets written (when set) $w = new Tricky2(); $r = new Tricky2(); write_to_read($w, $r); assert_true(!isset($r->im_optional)); // Require gets written (when set) $w = new Tricky3(); $r = new Tricky3(); try { write_to_read($w, $r); assert_true(false, "Expected exception because field is required"); } catch (Exception $ex) { } assert_true(!isset($r->im_required)); ///////////////////////////////// // Mixing Default & Optional & Required ///////////////////////////////// // Default <-> Optional $w = new Tricky1(); $r = new Tricky2(); $w->im_default = 0; $r->im_optional = 10; write_to_read($w, $r); assert_equals(0, $r->im_optional); assert_equals(0, $w->im_default); write_to_read($r, $w); assert_equals(0, $r->im_optional); assert_equals(0, $w->im_default); // Default <-> Required
function test_get_fixtures_for_all_tables() { $dbh = TestDB::get_instance(); $data = $dbh->get_test_data(); $fixtures = new SimpleFixtures($dbh); $fixtures->enqueue('users', $data['users'], array('auto_id' => false, 'auto_ref' => false)); $fixtures->enqueue('user_friends', $data['user_friends'], array('auto_id' => false, 'auto_ref' => false)); $retrieved_data = $fixtures->get('*'); assert_true(isset($retrieved_data['users'])) and assert_equal($retrieved_data['users'], $data['users']); assert_true(isset($retrieved_data['user_friends'])) and assert_equal($retrieved_data['user_friends'], $data['user_friends']); }
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 assert_writeable_dir($path) { assert_true(is_dir($path), "{$path} exists"); assert_true(is_writable($path), "{$path} is writeable"); }
function expect_error($callback, $error = null, $msg = null) { $last_error = mut_fetch_last_error($callback, $error); if (is_null($msg) && !$last_error['matched']) { $msg = 'Expected error'; if (is_null($last_error['match'])) { $msg .= ' [got: none]'; } else { 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_true($last_error['matched'], $msg); }
public function find_what_is_true___() { $hells_circles = 9; assert_true($hells_circles + 1 == __); }
echo "FAIL: [{$msg}]\n"; } } // The following test vectors were taken from RFC 6070. // https://www.ietf.org/rfc/rfc6070.txt $pbkdf2_vectors = array(array('algorithm' => 'sha1', 'password' => "password", 'salt' => "salt", 'iterations' => 1, 'keylength' => 20, 'output' => "0c60c80f961f0e71f3a9b524af6012062fe037a6"), array('algorithm' => 'sha1', 'password' => "password", 'salt' => "salt", 'iterations' => 2, 'keylength' => 20, 'output' => "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957"), array('algorithm' => 'sha1', 'password' => "password", 'salt' => "salt", 'iterations' => 4096, 'keylength' => 20, 'output' => "4b007901b765489abead49d926f721d065a429c1"), array('algorithm' => 'sha1', 'password' => "passwordPASSWORDpassword", 'salt' => "saltSALTsaltSALTsaltSALTsaltSALTsalt", 'iterations' => 4096, 'keylength' => 25, 'output' => "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038"), array('algorithm' => 'sha1', 'password' => "password", 'salt' => "salt", 'iterations' => 4096, 'keylength' => 16, 'output' => "56fa6aa75548099dcc37d7f03425e0c3")); foreach ($pbkdf2_vectors as $test) { $realOut = pbkdf2($test['algorithm'], $test['password'], $test['salt'], $test['iterations'], $test['keylength'], false); assert_true($realOut === $test['output'], "PBKDF2 vector"); } $good_hash = create_hash("foobar"); assert_true(validate_password("foobar", $good_hash), "Correct password"); assert_true(validate_password("foobar2", $good_hash) === false, "Wrong password"); $h1 = explode(":", create_hash("")); $h2 = explode(":", create_hash("")); assert_true($h1[HASH_PBKDF2_INDEX] != $h2[HASH_PBKDF2_INDEX], "Different hashes"); assert_true($h1[HASH_SALT_INDEX] != $h2[HASH_SALT_INDEX], "Different salts"); assert_true(slow_equals("", ""), "Slow equals empty string"); assert_true(slow_equals("abcdef", "abcdef"), "Slow equals normal string"); assert_true(slow_equals("aaaaaaaaaa", "aaaaaaaaab") === false, "Slow equals different"); assert_true(slow_equals("aa", "a") === false, "Slow equals different length 1"); assert_true(slow_equals("a", "aa") === false, "Slow equals different length 2"); echo "Example hash: {$good_hash}\n"; // benchmark for ($i = 0; $i < 25; $i++) { $count = pow(2, $i); $start = microtime(true); $hash = pbkdf2("sha256", "password", "salt", $count, 32); $time = microtime(true) - $start; printf("%10d iterations: %f seconds\n", $count, $time); }
foreach ($assets_of_type as $index => $val) { $assets_of_type[$index] = MatrixDAL::quote($val); } if (!empty($assets_of_type)) { $asset_ids_set = '(' . implode(', ', $assets_of_type) . ')'; $sql = 'DELETE FROM sq_ast_attr_val WHERE assetid in ' . $asset_ids_set; $query = MatrixDAL::preparePdoQuery($sql); MatrixDAL::execPdoQuery($query); $sql = 'DELETE FROM sq_ast_lnk WHERE minorid in ' . $asset_ids_set; $query = MatrixDAL::preparePdoQuery($sql); MatrixDAL::execPdoQuery($query); $sql = 'DELETE FROM sq_ast WHERE type_code = :type_code'; $query = MatrixDAL::preparePdoQuery($sql); MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE); MatrixDAL::execPdoQuery($query); } $sql = 'DELETE FROM sq_ast_attr WHERE type_code = :type_code OR owning_type_code = :owning_type_code'; $query = MatrixDAL::preparePdoQuery($sql); MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE); MatrixDAL::bindValueToPdo($query, 'owning_type_code', $DELETING_ASSET_TYPE); MatrixDAL::execPdoQuery($query); $sql = 'DELETE FROM sq_ast_typ WHERE type_code = :type_code'; $query = MatrixDAL::preparePdoQuery($sql); MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE); MatrixDAL::execPdoQuery($query); $sql = 'DELETE FROM sq_ast_typ_inhd WHERE type_code = :type_code'; $query = MatrixDAL::preparePdoQuery($sql); MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE); MatrixDAL::execPdoQuery($query); assert_true(unlink(dirname(dirname(__FILE__)) . '/data/private/db/asset_types.inc'), 'failed removing asset_types.inc'); echo "\nDone\n";
public function the_at_operator() { $get_error_reporting_level = function () { return error_reporting(); }; $old_reporting_level = error_reporting(); assert_true($old_reporting_level !== 0); assert_that($get_error_reporting_level())->is_identical_to(__); assert_that(@$get_error_reporting_level())->is_identical_to(__); // Virgil says: the @ operator suppresses every error you // get, making debugging almost impossible. It's also incredibly // confusing the first time you see it, as it's almost totally // un-Google-able! }