231 lines
7.5 KiB
PHP
231 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature; // Ensure the namespace is correct for feature tests
|
|
|
|
use Tests\TestCase; // Use Laravel's base TestCase class
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
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;
|
|
|
|
$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 testLatihanNumberTwo():void{
|
|
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_two'");
|
|
$row = $sql[0];
|
|
$html = $row->testing_rule;
|
|
$output = $row->output;
|
|
|
|
|
|
$studentFile = storage_path('app/public/uploads/Amal Dermawan Udjir/GuideA2.php');
|
|
if (!file_exists($studentFile)) {
|
|
$this->fail("❌ File siswa tidak ditemukan: $studentFile");
|
|
return;
|
|
}
|
|
// Ambil isi file PHP siswa
|
|
$studentCode = file_get_contents($studentFile);
|
|
|
|
// Normalisasi kunci jawaban dan kode siswa untuk perbandingan
|
|
$normalizedHtml = $this->normalize($html); // Normalisasi kode kunci jawaban
|
|
$normalizedStudentCode = $this->normalize($studentCode); // Normalisasi kode siswa
|
|
if ($normalizedHtml === $normalizedStudentCode) {
|
|
// Jika kode siswa sesuai dengan kunci jawaban
|
|
$this->assertEquals(
|
|
$normalizedHtml,
|
|
$normalizedStudentCode,
|
|
"Struktur kode yang dikirim siswa tidak sesuai dengan kunci jawaban."
|
|
);
|
|
} else {
|
|
// Jika struktur kode tidak sesuai, periksa output dari respons HTTP
|
|
$response = $this->get("/phpunit/result-test-student/");
|
|
$actualOutput = $response->content();
|
|
|
|
// Bandingkan output yang diterima dengan output yang diharapkan
|
|
$this->assertEquals(
|
|
trim($output),
|
|
trim($actualOutput),
|
|
"Output dari respons HTTP tidak sesuai dengan output kunci jawaban."
|
|
);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
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());
|
|
}
|
|
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;
|
|
}
|
|
}
|