xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / tests / util.php (d7933c8c402d0da9889ddba168478a7b04d648b1) (9,508B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/util.inc.php");
require_once($INC . "/log.inc.php");

rg_log_set_file("util.log");

$rg_no_db = TRUE;
require_once("common.php");


// Test template functions
function inc($v) { return $v + 1; }
function dec($v) { return $v - 1; }
rg_template_func("inc", "inc");
rg_template_func("dec", "dec");
$_rg = array("uid" => 5);
$r = trim(rg_template("func.txt", $_rg));
$e = "5 + 1 = 6 | 5 - 1 = 4 | 4";
if (strcmp($r, $e) != 0) {
	rg_log("template func1 test failed [$r] != [$e]");
	exit(1);
}


$id = rg_id(16);
if (strlen($id) != 16) {
	rg_log("Cannot generate an id!");
	exit(1);
}


// rg_array2string
$a = 5;
$e = "5";
$r = rg_array2string($a);
if ($r !== $e) {
	rg_log("array2string is not working for integers ($r != $a)!");
	exit(1);
}
$a = "6";
$e = "6";
$r = rg_array2string($a);
if ($r !== $e) {
	rg_log("array2string is not working for strings ($r != $a)!");
	exit(1);
}


@mkdir("util.tmp", 0700, TRUE);
file_put_contents("util.tmp/file1", "aaa");
file_put_contents("util.tmp/file2", "bbb");
$r = rg_rmdir("util.tmp");
if ($r !== TRUE) {
	rg_log("Cannot delete dir (" . rg_util_error() . ")!");
	exit(1);
}


rg_log("util.php: testing function rg_prepare_replace_helper...");
$what = array(); $values = array();
$a = array("a" => "b");
rg_prepare_replace_helper($a, "", $what, $values);
$w = rg_array2string($what);
$v = rg_array2string($values);
$ew = "a=[/@@a@@/uU]";
$ev = "a=[b]";
if ((strcmp($w, $ew) != 0) || (strcmp($v, $ev) != 0)) {
	rg_log("Wrong prepare_replace: [$w] != [$ew] OR [$v] != [$ev]!");
	exit(1);
}

$what = array(); $values = array();
$a = array("ri" => array("repo_id" => "1", "name" => "repo1"));
rg_prepare_replace_helper($a, "", $what, $values);
$w = rg_array2string($what);
$v = rg_array2string($values);
$ew = "ri::repo_id=[/@@ri\:\:repo_id@@/uU] ri::name=[/@@ri\:\:name@@/uU]";
$ev = "ri::repo_id=[1] ri::name=[repo1]";
if ((strcmp($w, $ew) != 0) || (strcmp($v, $ev) != 0)) {
	rg_log("Wrong prepare_replace: [$w] != [$ew] OR [$v] != [$ev]!");
	exit(1);
}


$r = rg_exec("/xxxx");
if ($r['ok'] == 1) {
	rg_log("util.php: running non existing command does not return 0!");
	print_r($r);
	exit(1);
}

$r = rg_exec("ls");
if ($r['ok'] != 1) {
	rg_log("util.php: cannot run a command!");
	print_r($r);
	exit(1);
}

$r = rg_exec("./util_exit_code.sh 5");
if ($r['code'] != 5) {
	rg_log("util.php: error code seems to not be propageted!");
	print_r($r);
	exit(1);
}

$t = "test rg_template_table(dir, data, more) with no data";
rg_log($t);
$data = array();
$r = rg_template_table("t1", $data, array("a" => "A"));
$e = "XAX";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template_table(dir, data, more) with data";
rg_log($t);
$data = array(array("a" => "A", "b" => "B"), array("a" => "A2", "b" => "B2"));
$r = rg_template_table("t2", $data, array("c" => "C"));
$e = "HEADCABCA2B2CFOOTC";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (false)";
rg_log($t);
$data = array("X" => "0", "A" => "Avalue", "B" => "Bvalue");
$r = rg_template("t3/c1", $data);
$e = "XXBvalueYY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (true)";
rg_log($t);
$data = array("X" => "1", "A" => "Avalue", "B" => "Bvalue");
$r = rg_template("t3/c1", $data);
$e = "XXAvalueYY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (multiline)";
rg_log($t);
$data = array("X" => "1", "A" => "Avalue", "B" => "Bvalue");
$r = rg_template("t3/c1", $data);
$e = "XXAvalueYY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (nested 1)";
rg_log($t);
$data = array("X" => "1", "Y" => "1", "A" => "Avalue", "B" => "Bvalue",
	"R" => "Rvalue", "T" => "Tvalue");
$r = rg_template("t3/c3", $data);
$e = "XXRvalueZZYY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (nested 2)";
rg_log($t);
$data = array("X" => "1", "Y" => "0", "A" => "Avalue", "B" => "Bvalue",
	"R" => "Rvalue", "T" => "Tvalue");
$r = rg_template("t3/c3", $data);
$e = "XXTvalueZZYY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (nested 3)";
rg_log($t);
$data = array("X" => "0", "Y" => "1", "A" => "Avalue", "B" => "Bvalue",
	"R" => "Rvalue", "T" => "Tvalue");
$r = rg_template("t3/c3", $data);
$e = "XXBvalueYY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (nested nested 1)";
rg_log($t);
$data = array("X" => "1", "Y" => "1", "Z" => "1");
$r = rg_template("t3/c4", $data);
$r = preg_replace('/\s/', '', $r);
$e = "XXTRUE_LEVEL_2YY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (nested nested 2)";
rg_log($t);
$data = array("X" => "1", "Y" => "0", "Z" => "1");
$r = rg_template("t3/c4", $data);
$r = preg_replace('/\s/', '', $r);
$e = "XXFALSE_LEVEL_1YY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (nested nested 2)";
rg_log($t);
$data = array("X" => "0", "Y" => "1", "Z" => "1");
$r = rg_template("t3/c4", $data);
$r = preg_replace('/\s/', '', $r);
$e = "XXFALSE_LEVEL_0YY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (nested nested 3)";
rg_log($t);
$data = array("X" => "0", "Y" => "0", "Z" => "0");
$r = rg_template("t3/c5", $data);
$r = preg_replace('/\s/', '', $r);
$e = "XX-X0Y0Z0-YY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (nested nested 4)";
rg_log($t);
$data = array("X" => "0", "Y" => "1", "Z" => "0");
$r = rg_template("t3/c5", $data);
$r = preg_replace('/\s/', '', $r);
$e = "XX-X0Y1Z0-YY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (empty)";
rg_log($t);
$data = array();
$r = rg_template("t3/c6", $data);
$r = preg_replace('/\s/', '', $r);
$e = "A";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (quotes)";
rg_log($t);
$data = array("a" => "abc");
$r = rg_template("t3/c6b", $data);
$r = preg_replace('/\s/', '', $r);
$e = "AY";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_template with conditional formating (!empty)";
rg_log($t);
$data = array("AAA" => "");
$r = rg_template("t3/c7", $data);
$r = preg_replace('/\s/', '', $r);
$e = "B";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=$r e=$e)!");
	exit(1);
}

$t = "test rg_replace_lookup";
rg_log($t);
$data = array("AAA" => "1", "BBB" => "2", "CCC" => "1");
$r = rg_replace_lookup($data, "@@AAA@@@@BBB@@@@CCC@@");
$e = "121";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=[$r] e=[$e])!");
	exit(1);
}

$t = "test rg_template with conditional formating (a variable contains '{{')";
rg_log($t);
$data = array("AAA" => "1", "BBB" => "}}", "CCC" => "{{");
$r = rg_template("t3/c8", $data);
$r = preg_replace('/\s/', '', $r);
$e = "}}";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=[$r] e=[$e])!");
	exit(1);
}

$t = "test rg_template with conditional formating: false branch is empty)";
rg_log($t);
$data = array("X" => "abc");
$r = rg_template("t3/c9", $data);
$r = preg_replace('/\s/', '', $r);
$e = "XXBLABLABLAabcYYabc";
if (strcmp($r, $e) != 0) {
	rg_log("util.php: $t: not working (r=[$r] e=[$e])!");
	exit(1);
}

$t = "test rg_copy_tree";
rg_log($t);
$r = rg_copy_tree("tree1", "tree1.copy", 0755);
if ($r !== TRUE) {
	rg_log("util.php: $t: not working!");
	exit(1);
}
if (!file_exists("tree1.copy/a/f2")) {
	rg_log("util.php: $t(2): not working!");
	exit(1);
}

$t = "rg_dir_load";
rg_log($t);
$x = rg_dir_load("/non_existing_dir");
if ($x !== FALSE) {
	rg_log("$t(non_existing) is not working right!");
	exit(1);
}

$t = "rg_dir_load_pattern";
rg_log($t);
$x = rg_dir_load_pattern("util/dir_pattern", "err-.*");
$x2 = implode("", $x);
$e = "err-asasasas";
if (strcmp($x2, $e) != 0) {
	rg_log("$t() is not working right ($r != $e)!");
	exit(1);
}

$src = array();
$a = array("u" => "uval", "HTML:A" => "Aval");
$x = rg_array_merge($src, "X", $a);
if (strcmp($x['X::u'], "uval") != 0) {
	rg_log("array_merge is not working correctly (2)!");
	print_r($x);
	exit(1);
}
if (strcmp($x['HTML:X::A'], "Aval") != 0) {
	rg_log("array_merge is not working correctly (1)!");
	print_r($x);
	exit(1);
}


// rg_re_repo_git($organization, $user, $repo)
$r = rg_re_repo_git(1, 'u1<', 'repo2ș');
$e = 'git://localhost/u1%3C/repo2%C8%99';
if (strcmp($r, $e) != 0) {
	rg_log("re_repo_git is wrong [$r] != [$e]!");
	exit(1);
}

rg_log("OK!");
?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master