Amal_Udjir/tests/Unit/HomepageLayoutTest.php
2025-05-09 05:04:11 +07:00

342 lines
12 KiB
PHP

<?php
namespace Tests\Feature; // Ensure the namespace is correct for feature tests
ob_start();
use Tests\TestCase; // Use Laravel's base TestCase class
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Auth;
class HomepageLayoutTest extends TestCase
{
/**
* A basic feature test example.
* ./vendor/bin/phpunit
*/
public function testLatihanNumberOne():void{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_one'");
$row = $sql[0];
$html = $row->testing_rule;
$hasil_output = $row->output;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
if($result_test == $result_content){
$this->assertStringContainsString($result_test, $result_content);
}else{
$response = $this->get("/phpunit/result-test-student-output/");
$response->assertSeeText('BelajarMembuatHeadingdanParagraphHeadingke-1Headingke-2Headingke-3Headingke-4Headingke-5Headingke-6inicontohparagraph');
}
}
public function testLatihanNumberTwo():void{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_two'");
$row = $sql[0];
$html = $row->testing_rule;
$hasil_output = $row->output;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
if($result_test == $result_content){
$this->assertStringContainsString($result_test, $result_content);
}else{
$response = $this->get("/phpunit/result-test-student-output/");
$test = str_replace(array("\r\n","\r","\n"," "),"",$hasil_output);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
$this->assertEquals($result_test, $result_content);
}
}
public function testLatihanNumberThree():void{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_three'");
$row = $sql[0];
$html = $row->testing_rule;
$hasil_output = $row->output;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
if($result_test == $result_content){
$this->assertStringContainsString($result_test, $result_content);
}else{
$response = $this->get("/phpunit/result-test-student-output/");
$nama = "Ali Akbar";
$umur = 20;
$tinggi = 172.5;
$gaji_pokok = 1000000;
$gaji_bulanan = 0;
$gaji_per_tahun = 0;
$ppn = 0;
$gaji_setelah_ppn = 0;
$gaji_bulanan = $gaji_pokok * 0.5;
$gaji_per_tahun = $gaji_bulanan * 12;
$ppn = $gaji_per_tahun * 0.10;
$gaji_setelah_ppn = $gaji_per_tahun - $ppn;
$response->assertSeeText($umur);
$response->assertSeeText($tinggi);
$response->assertSeeText($gaji_pokok);
$response->assertSeeText($gaji_bulanan);
$response->assertSeeText($gaji_per_tahun);
$response->assertSeeText($ppn);
}
}
public function testLatihanNumberFour():void{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_four'");
$row = $sql[0];
$html = $row->testing_rule;
$hasil_output = $row->output;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
if($result_test == $result_content){
$this->assertStringContainsString($result_test, $result_content);
}else{
$response = $this->get("/phpunit/result-test-student-output/");
$nilai = 75;
if ($nilai >= 70) {
$response->assertSeeText($nilai);
$response->assertSeeText('Anda lulus!');
}
$nilai = 55;
if ($nilai >= 70) {
$response->assertSeeText($nilai);
$response->assertSeeText('Anda lulus!');
} else {
$response->assertSeeText($nilai);
$response->assertSeeText('Anda tidak lulus.');
}
$nilai = "B";
switch ($nilai){
case "A":
$response->assertSeeText($nilai);
$response->assertSeeText('Pertahankan prestasi!');
break;
case "B":
$response->assertSeeText($nilai);
$response->assertSeeText('Cukup baik, terus tingkatkan!');
break;
case "C":
$response->assertSeeText($nilai);
$response->assertSeeText('Kurang baik, terus tingkatkan!');
break;
default:
$response->assertSeeText($nilai);
$response->assertSeeText('Anda tidak lulus.');
}
}
}
public function testLatihanNumberFive():void{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_five'");
$row = $sql[0];
$html = $row->testing_rule;
$hasil_output = $row->output;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
if($result_test == $result_content){
$this->assertStringContainsString($result_test, $result_content);
}else{
$response = $this->get("/phpunit/result-test-student-output/");
$angka = 1;
while ($angka <= 5) {
$response->assertSeeText((string)$angka);
$angka++;
}
for ($i = 1; $i <= 5; $i++) {
$response->assertSeeText((string)$i);
}
$angka = 1;
do {
$response->assertSeeText((string)$angka);
$angka++;
} while ($angka <= 5);
}
}
public function testLatihanNumberSix():void{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_six'");
$row = $sql[0];
$html = $row->testing_rule;
$hasil_output = $row->output;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
if($result_test == $result_content){
$this->assertStringContainsString($result_test, $result_content);
}else{
$response = $this->get("/phpunit/result-test-student-output/");
$bilangan = array(1, 2, 3, 4, 5);
$nama = array("John", "Doe", "Jane");
$status = array(true, false, true);
$response->assertSeeText($bilangan[0]);
$response->assertSeeText($nama[0]);
$response->assertSeeText($status[1] ? "Aktif" : "Tidak Aktif");
$response->assertSeeText($nama[0]);
$bilangan[] = 6;
unset($nama[0]);
$posisi = array_search("Doe", $nama);
$response->assertSeeText($posisi);
$jumlah_nama = count($nama);
$response->assertSeeText($jumlah_nama);
array_push($nama, "Smith");
$elemen_terakhir = array_pop($nama);
$response->assertSeeText($elemen_terakhir);
}
}
public function testConditions(): void
{
}
public function test_addition(){
$a = 2;
$b = 3;
$result = $a + $b;
$response = $this->get('/phpunit/result-test-student-add');
$response->assertStatus(200);
$responseContent = $response->getContent();
$this->assertEquals(
$result,
$responseContent,
"The addition of $a and $b should be $result"
);
}
public function test_variable_value_and_type()
{
$variable = "is php example";
$this->assertEquals("this is php example", $variable);
$this->assertIsString($variable);
}
public function test_php_variable_response()
{
// Send GET request to route /execute-php-variable
$response = $this->get("/execute-php-variable");
$response->assertSee("this is php variable example");
// Checks that the response content is a string
$this->assertIsString($response->getContent());
}
public function test_conditional_statement_output()
{
$response = $this->get("/execute-conditional-php/true");
$response->assertSee("conditional statement example if the condition is true");
$this->assertIsString($response->getContent());
}
public function test_conditional_else_statement_output()
{
$response = $this->get("/execute-conditional-php");
$response->assertSee("conditional statement example if the condition is false");
$this->assertIsString($response->getContent());
}
public function test_loop_output()
{
$response = $this->get("/loop-php-example");
$expectedOutput = "1 2 3 4 5 6 7 8 9 10 this is looping php example";
$response->assertSee($expectedOutput);
$this->assertIsString($response->getContent());
}
public function test_array_output()
{
$response = $this->get("/array-php-example");
$response->assertJson([
'first' => 'this is array php example',
'second' => 'another example',
'third' => 'yet another example'
]);
$this->assertContains('this is array php example', $response->json());
}
public function testAddition(): void
{
$result = $this->add(2, 3);
$this->assertEquals(6, $result);
}
/**
* A method to perform addition.
*
* @param int $a
* @param int $b
* @return int
*/
public function add($a, $b): int
{
return $a + $b;
}
private function normalize($string): string
{
return preg_replace('/\s+/', '', $string); // hilangkan semua spasi, tab, newline
}
// Fungsi untuk mengganti nama variabel dengan placeholder
private function normalizeVariables(string $code): string
{
// Menangkap nama variabel menggunakan ekspresi regular
preg_match_all('/\$(\w+)/', $code, $matches);
// Membuat daftar variabel unik
$uniqueVariables = array_unique($matches[1]);
// Gantikan nama variabel dengan placeholder dinamis
foreach ($uniqueVariables as $index => $variable) {
$code = str_replace('$' . $variable, '$var' . $index, $code); // Ganti variabel dengan placeholder
}
return $code;
}
}