<?php


namespace Tests\Functional;

use Tests\Support\FunctionalTester;

class FormMethodTest extends \Codeception\Test\Unit
{

    protected FunctionalTester $tester;

    protected function _before()
    {
    }

    // tests
    public function testMethodPOST()
    {
        $I = $this->tester;
        $I->amOnPage('/phpunit/result-test-student/');
        $responseContent = $I->grabPageSource();
        $fileContent = htmlspecialchars_decode($responseContent);

        //\PHPUnit\Framework\Assert::assertMatchesRegularExpression('/\$_POST\s*\[.*\]/', $fileContent, 'Pemeriksaan File pengelolaan form dengan Method POST terdapat kesalahan');
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/<form[^>]*method=["\']post["\']/i', $fileContent, 'Pemeriksaan File pengelolaan form dengan Method POST terdapat kesalahan');
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/<input\s+[^>]*type=["\']text["\']\s+[^>]*name=["\']nama["\']/i', $fileContent, 'Input type text untuk nama tidak ditemukan.');
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/<input\s+[^>]*type=["\']email["\']\s+[^>]*name=["\']email["\']/i', $fileContent, 'Input type email tidak ditemukan.');
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/<select\s+[^>]*name=["\']prodi["\']/i', $fileContent, 'Select untuk prodi tidak ditemukan.');
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/<button\s+[^>]*type=["\']submit["\']/i', $fileContent, 'Tombol submit tidak ditemukan.');
        fwrite(STDOUT, "✅ Pemeriksaan File pengelolaan form dengan Method POST Berhasil.\n");
    }

    public function testMethodGET()
    {
        $I = $this->tester;
        $I->amOnPage('/phpunit/result-test-student/');
        $responseContent = $I->grabPageSource();
        $fileContent = htmlspecialchars_decode($responseContent);

        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/\$_GET\s*\[.*\]/', $fileContent, 'Pemeriksaan File pengelolaan form dengan Method GET terdapat kesalahan');
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/<form[^>]*method=["\']get["\']/i', $fileContent, 'Pemeriksaan File pengelolaan form dengan Method GET terdapat kesalahan');
        fwrite(STDOUT, "✅ Pemeriksaan File pengelolaan form dengan Method GET berhasil.\n");
    }
   
    public function testConnetionDatabase()
    {
        $I = $this->tester;
        $I->amOnPage('/phpunit/result-test-student/');
        $responseContent = $I->grabPageSource();
        $fileContent = htmlspecialchars_decode($responseContent);

        /*$patterns = [
            'servername' => '/\$(servername|server|host|svr|localhost)\s*=\s*[\'"][^\'"]+[\'"]\s*;/',
            'username'   => '/\$(username|user)\s*=\s*[\'"][^\'"]+[\'"]\s*;/',
            'password'   => '/\$(password|pass)\s*=\s*[\'"][^\'"]*[\'"]\s*;/',
            'dbname'     => '/\$(db|dbname|database)\s*=\s*[\'"][^\'"]+[\'"]\s*;/'
        ];
        $errors = [];
        foreach ($patterns as $key => $pattern) {
            if (!preg_match($pattern, $fileContent)) {
                $errors[] = "❌ Variabel untuk $key tidak ditemukan.";
            } else {
                fwrite(STDOUT, "✔ Variabel untuk $key ditemukan.\n");
            }
        }

        // Jika ada error, tampilkan semua sekaligus
        if (!empty($errors)) {
            fwrite(STDERR, implode("\n", $errors) . "\n");
            \PHPUnit\Framework\Assert::fail("Pemeriksaan File Koneksi Periksa variable PHP Anda, Terdapat Kesalahan");
        }*/
        
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/new mysqli\(/', $fileContent, 'Pemeriksaan File Koneksi PHP Terdapat Kesalahan');
        fwrite(STDOUT, "✅ Pemeriksaan File Koneksi PHP berhasil.\n");
    }
    public function testInsertDatabase()
    {
        $I = $this->tester;
        $I->amOnPage('/phpunit/result-test-student/');
        $responseContent = $I->grabPageSource();
        $fileContent = htmlspecialchars_decode($responseContent);
    
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/\binsert\s+into\b/i', $fileContent, 'File operasi CREATE Terdapat Kesalahan');
        fwrite(STDOUT, "✅ Pemeriksaan File Insert PHP berhasil.\n");
    }
    
    public function testUpdateDatabase()
    {
        $I = $this->tester;
        $I->amOnPage('/phpunit/result-test-student/');
        $responseContent = $I->grabPageSource();
        $fileContent = htmlspecialchars_decode($responseContent);
    
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/\bupdate\s+users\s+set\b/i', $fileContent, 'File operasi UPDATE Terdapat Kesalahan');
        fwrite(STDOUT, "✅ Pemeriksaan File Update PHP berhasil.\n");
    }
    
    public function testDeleteDatabase()
    {
        $I = $this->tester;
        $I->amOnPage('/phpunit/result-test-student/');
        $responseContent = $I->grabPageSource();
        $fileContent = htmlspecialchars_decode($responseContent);
    
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/\bdelete\s+from\s+users\b/i', $fileContent, 'File operasi DELETE Terdapat Kesalahan');
        fwrite(STDOUT, "✅ Pemeriksaan File Delete PHP berhasil.\n");
    }
    public function testSelectDatabase()
    {
        $I = $this->tester;
        $I->amOnPage('/phpunit/result-test-student/');
        $responseContent = $I->grabPageSource();
        $fileContent = htmlspecialchars_decode($responseContent);

        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/\sfrom\s+users/i', $fileContent, 'File operasi READ Terdapat Kesalahan');
        fwrite(STDOUT, "✅ Pemeriksaan File Select PHP berhasil.\n");
    }

    
    public function testSession()
    {
        $I = $this->tester;
        $I->amOnPage('/phpunit/result-test-student/');
        $responseContent = $I->grabPageSource();
        $fileContent = htmlspecialchars_decode($responseContent);

        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/\bsession_start\s*\(\s*\)/i', $fileContent, 'Pemeriksaan File Session Terdapat Kesalahan');
        \PHPUnit\Framework\Assert::assertMatchesRegularExpression('/\$_session\s*\[.*\]/i', $fileContent, 'Pemeriksaan File Session Terdapat Kesalahan');
        fwrite(STDOUT, "✅ Pemeriksaan File Session PHP berhasil.\n");
    }
}
