Amal_Udjir/tests/Unit/PHPBasicTest.php

88 lines
3.7 KiB
PHP

<?php
namespace Tests\Unit;
use Tests\Support\UnitTester;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;
use Illuminate\Container\Container;
class PHPBasicTest extends \Codeception\Test\Unit
{
protected UnitTester $tester;
protected function _before()
{
}
// tests
public function testLatihanNumberOne()
{
// Simulate fetching the response (Replace with actual logic)
$responseContent = file_get_contents('http://127.0.0.1:8000/phpunit/result-test-student/');
// Fetch expected rule from database using Codeception's DB module
$row = $this->tester->grabFromDatabase('php_testing_rule', 'testing_rule', [
'testing_name' => 'testing_number_one'
]);
// Process expected HTML and response for comparison
$test = str_replace(["\r\n", "\r", "\n", " "], "", $row);
$result_test = htmlspecialchars($test);
$result_content = str_replace(["\r\n", "\r", "\n", " "], "", $responseContent);
// Assert that expected content exists in the response
$this->assertStringContainsString($result_test, $result_content);
}
public function testUploadedFileContainsSession()
{
$fileContent = file_get_contents('http://127.0.0.1:8000/phpunit/result-test-student/');
$this->assertStringContainsString('session_start()', $fileContent, 'File tidak mengandung session_start()');
$this->assertMatchesRegularExpression('/\$_SESSION\s*\[.*\]/', $fileContent, 'File tidak mengandung manipulasi $_SESSION');
}
public function testUploadedFileContainsFormHandling()
{
// Ambil isi file dari URL
$fileContent = file_get_contents('http://127.0.0.1:8000/phpunit/result-test-student/');
$fileContent = htmlspecialchars_decode($fileContent);
$this->assertMatchesRegularExpression('/\$_POST\s*\[.*\]/', $fileContent, 'File tidak mengandung pengelolaan form dengan POST');
$this->assertMatchesRegularExpression('/\$_GET\s*\[.*\]/', $fileContent, 'File tidak mengandung pengelolaan form dengan GET');
$this->assertMatchesRegularExpression('/<form[^>]*method=["\']post["\']/i', $fileContent, 'File tidak mengandung form HTML dengan method POST');
$this->assertMatchesRegularExpression('/<form[^>]*method=["\']get["\']/i', $fileContent, 'File tidak mengandung form HTML dengan method GET');
}
public function testUploadedFileContainsDatabaseOperations()
{
// Ambil isi file dari URL atau lokasi penyimpanan
$fileContent = file_get_contents('http://127.0.0.1:8000/phpunit/result-test-student/');
// Periksa apakah file mengandung kode untuk koneksi database
$this->assertMatchesRegularExpression('/new mysqli\(/', $fileContent, 'File tidak mengandung kode koneksi ke database');
// Periksa apakah file mengandung operasi Create (INSERT INTO)
// $this->assertMatchesRegularExpression('/INSERT INTO users/', $fileContent, 'File tidak mengandung operasi CREATE (INSERT INTO)');
// Periksa apakah file mengandung operasi Read (SELECT)
//$this->assertMatchesRegularExpression('/SELECT id, namea, email FROM users/', $fileContent, 'File tidak mengandung operasi READ (SELECT)');
// Periksa apakah file mengandung operasi Update (UPDATE)
// $this->assertMatchesRegularExpression('/UPDATE users SET/', $fileContent, 'File tidak mengandung operasi UPDATE');
// Periksa apakah file mengandung operasi Delete (DELETE)
//$this->assertMatchesRegularExpression('/DELETE FROM users/', $fileContent, 'File tidak mengandung operasi DELETE');
}
}