Amal_Udjir/tests/Unit/HomepageLayoutTest.php

220 lines
7.5 KiB
PHP
Raw Normal View History

2025-04-22 03:10:07 +00:00
<?php
2025-05-07 05:51:49 +00:00
namespace Tests\Feature; // Ensure the namespace is correct for feature tests
2025-05-07 15:02:06 +00:00
ob_start();
2025-05-07 05:51:49 +00:00
use Tests\TestCase; // Use Laravel's base TestCase class
2025-04-22 03:10:07 +00:00
use Illuminate\Support\Facades\DB;
2025-05-07 15:02:06 +00:00
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Auth;
2025-04-22 03:10:07 +00:00
class HomepageLayoutTest extends TestCase
{
/**
* A basic feature test example.
* ./vendor/bin/phpunit
*/
public function testLatihanNumberOne():void{
2025-05-07 15:02:06 +00:00
$response = $this->get("/phpunit/result-test-student/");
2025-05-07 05:51:49 +00:00
2025-05-07 15:02:06 +00:00
$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');
}
2025-04-22 03:10:07 +00:00
}
public function testLatihanNumberTwo():void{
2025-05-07 08:14:29 +00:00
$response = $this->get("/phpunit/result-test-student/");
2025-04-22 03:10:07 +00:00
2025-05-07 15:02:06 +00:00
$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);
}
2025-04-22 03:10:07 +00:00
}
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;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
$this->assertStringContainsString($result_test, $result_content);
}
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;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
$this->assertStringContainsString($result_test, $result_content);
}
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;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
$this->assertStringContainsString($result_test, $result_content);
}
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());
2025-04-28 01:17:26 +00:00
}
2025-04-22 03:10:07 +00:00
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;
}
2025-05-07 08:14:29 +00:00
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;
}
2025-04-22 03:10:07 +00:00
}