diff --git a/system/UnitTests/AssertionHelpers.php b/system/UnitTests/AssertionHelpers.php index ec5211617..484a883b2 100644 --- a/system/UnitTests/AssertionHelpers.php +++ b/system/UnitTests/AssertionHelpers.php @@ -1,17 +1,49 @@ '; +} + +function preFormat($text) { + if (IS_CLI) { + return $text; // Plain text in CLI + } else { + return "
$text"; // HTML preformatted in browser + } +} + +function colorText($text, $color) { + if (IS_CLI) { + // ANSI color codes + $colors = [ + 'red' => "\033[31m", + 'green' => "\033[32m", + 'reset' => "\033[0m", + ]; + return $colors[$color] . $text . $colors['reset']; + } else { + // HTML styles + $styles = [ + 'red' => "$text", + 'green' => "$text", + ]; + return $styles[$color]; + } +} + function assertEqual($expected, $actual, $message = '') { if ($expected !== $actual) { - echo "❌ Assertion failed: $message
" . var_export($expected, true) . ""; - echo "Actual:
" . var_export($actual, true) . ""; + echo colorText('❌ Assertion failed:', 'red') . ' ' . $message . lineBreak(); + echo "Expected: " . preFormat(var_export($expected, true)) . lineBreak(); + echo "Actual: " . preFormat(var_export($actual, true)) . lineBreak(); return false; } else { - echo "✅ Passed: $message
";
+require_once(PROJECT_ROOT . '/config/cis.config.inc.php');
+require_once(PROJECT_ROOT . '/vendor/nategood/httpful/bootstrap.php');
+require_once(PROJECT_ROOT . '/system/UnitTests/AssertionHelpers.php');
+echo "Requirements loaded".LINE_BREAK;
+
+$server = getParam('server', APP_ROOT);
+echo $server.LINE_BREAK;
+$TEST_USER = getParam('user', 'defaultuser'); //if23b236
+echo $TEST_USER.LINE_BREAK;
+$TEST_PW = getParam('pw', 'defaultpass'); //FHCompleteDemo42!
+echo $TEST_PW.LINE_BREAK;
// "Unit Test" Script to Test API Controller frontend/v1/Bookmark.php by calling methods with curated inputs and checking
// for the expected output
-$ROOT = APP_ROOT; // calls itself -> TODO: switch for other machines
-//$ROOT = 'https://ci.dev.technikum-wien.at/';
-//$ROOT = 'https://cis40.dev.technikum-wien.at/';
-$URL = $ROOT.'cis.php/api/frontend/v1/Bookmark/';
+$URL = $server.'/cis.php/api/frontend/v1/Bookmark/';
testGetBookmarks($URL, 'getBookmarks', $TEST_USER, $TEST_PW);
$id = testInsertBookmark($URL, 'insert', $TEST_USER, $TEST_PW);
$id = testUpdateBookmark($URL, 'update', $TEST_USER, $TEST_PW, $id);
testDeleteBookmark($URL, 'delete', $TEST_USER, $TEST_PW, $id);
+if (!IS_CLI) echo "";
function testGetBookmarks($url, $method, $user, $pw) {
- echo "
Test '".$method."' start \r\n";
- echo "
";
+ echo LINE_BREAK.LINE_BREAK."Test '".$method."' start ".LINE_BREAK;
+
try {
$resultPost = \Httpful\Request::get($url.$method)
->expectsJson()
@@ -50,16 +71,16 @@ function testGetBookmarks($url, $method, $user, $pw) {
$assertions[] = assertEqual($resultPost->body->meta->status, "success", "Response Status Success");
if(allTrue($assertions)) {
- echo "Test '".$method."' finished SUCCESS
";
+ echo "Test '".$method."' finished SUCCESS".LINE_BREAK;
} else {
- echo "Test '".$method."' finished FAIL
";
+ echo "Test '".$method."' finished FAIL".LINE_BREAK;
printResponse($resultPost);
}
}
function testInsertBookmark($url, $method, $user, $pw) {
- echo "
Test '".$method."' start \r\n";
- echo "
";
+ echo LINE_BREAK.LINE_BREAK."Test '".$method."' start ".LINE_BREAK;
+ echo LINE_BREAK;
try {
$bodyTitle = 'orf';
$bodyUrl = 'https://orf.at';
@@ -85,9 +106,9 @@ function testInsertBookmark($url, $method, $user, $pw) {
$assertions[] = assertIsString($resultPost->body->meta->status);
$assertions[] = assertEqual("success",$resultPost->body->meta->status, "Response Status Success");
if(allTrue($assertions)) {
- echo "Test '".$method."' finished SUCCESS
";
+ echo "Test '".$method."' finished SUCCESS".LINE_BREAK;
} else {
- echo "Test '".$method."' finished FAIL
";
+ echo "Test '".$method."' finished FAIL".LINE_BREAK;
printResponse($resultPost);
}
@@ -95,8 +116,7 @@ function testInsertBookmark($url, $method, $user, $pw) {
}
function testDeleteBookmark($url, $method, $user, $pw, $id) {
- echo "
Test '".$method."' start \r\n";
- echo "
";
+ echo LINE_BREAK.LINE_BREAK."Test '".$method."' start \n".LINE_BREAK;
try {
$resultPost = \Httpful\Request::post($url.$method.'/'.$id)
@@ -118,16 +138,15 @@ function testDeleteBookmark($url, $method, $user, $pw, $id) {
$assertions[] = assertIsString($resultPost->body->meta->status);
$assertions[] = assertEqual("success",$resultPost->body->meta->status, "Response Status Success");
if(allTrue($assertions)) {
- echo "Test '".$method."' finished SUCCESS
";
+ echo "Test '".$method."' finished SUCCESS".LINE_BREAK;
} else {
- echo "Test '".$method."' finished FAIL
";
+ echo "Test '".$method."' finished FAIL".LINE_BREAK;
printResponse($resultPost);
}
}
function testUpdateBookmark($url, $method, $user, $pw, $id) {
- echo "
Test '".$method."' start \r\n";
- echo "
";
+ echo LINE_BREAK.LINE_BREAK."Test '".$method."' start ".LINE_BREAK;
try {
$bodyTitle = 'orf title updated';
@@ -153,9 +172,9 @@ function testUpdateBookmark($url, $method, $user, $pw, $id) {
$assertions[] = assertIsString($resultPost->body->meta->status);
$assertions[] = assertEqual("success",$resultPost->body->meta->status, "Response Status Success");
if(allTrue($assertions)) {
- echo "Test '".$method."' finished SUCCESS
";
+ echo "Test '".$method."' finished SUCCESS".LINE_BREAK;
} else {
- echo "Test '".$method."' finished FAIL
";
+ echo "Test '".$method."' finished FAIL".LINE_BREAK;
printResponse($resultPost);
}
@@ -163,19 +182,19 @@ function testUpdateBookmark($url, $method, $user, $pw, $id) {
}
function printResponse($resultPost) {
- echo "
";
- echo "Response Body:\n";
+ echo LINE_BREAK;
+ echo "Response Body:";
print_r($resultPost->body);
- echo "
";
- echo "Raw Response:\n";
+ echo LINE_BREAK;
+ echo "Raw Response:";
print_r($resultPost->raw_body);
- echo "
";
- echo "Status Code:\n";
+ echo LINE_BREAK;
+ echo "Status Code:";
print_r($resultPost->code);
- echo "
";
- echo "Headers:\n";
+ echo LINE_BREAK;
+ echo "Headers:";
print_r($resultPost->headers);
- echo "
";
+ echo LINE_BREAK;
}
function allTrue($arr) {