<?php
require_once __DIR__ . '/helper/TestResultsManager.php';

use PHPUnit\Framework\TestCase;

class ValidasiFormPHPTest extends TestCase
{
    private $testResultsManager;

    protected function setUp(): void
    {
        $this->testResultsManager = new TestResultsManager();
    }

    public function testValidFormData()
    {
        try {
            $testCase = get_class($this) . '.' . __FUNCTION__;
            $_GET['yourName'] = 'AliyyaPS';
            $_GET['yourAddress'] = 'Sidoarjo';

            ob_start();
            include_once './apps/validasiForm.php';

            $output = ob_get_clean();
            $dom = new DOMDocument();
            $dom->loadHTML($output);

            $titleElemen = $dom->getElementsByTagName('title')->item(0);

            $this->assertEquals('Validasi Form', $titleElemen->nodeValue, 'The text content of the title element on the page should be "Validasi Form""');

            $bodyElemen = $dom->getElementsByTagName('body')->item(0);
            $this->assertStringContainsString('Welcome AliyyaPS from Sidoarjo', $bodyElemen->nodeValue);
            $this->testResultsManager->showTestResult($testCase);
        } catch (\Throwable $e) {
            $this->testResultsManager->showTestResult($testCase, $e);
            throw $e;
        }
    }

    public function testInvalidFormData()
    {
        try {
            $testCase = get_class($this) . '.' . __FUNCTION__;
            $html = file_get_contents('./apps/validasiForm.php');
            $dom = new DOMDocument();
            $dom->loadHTML($html);

            $titleElemen = $dom->getElementsByTagName('title')->item(0);

            $this->assertEquals('Validasi Form', $titleElemen->nodeValue, 'The text content of the title element on the page should be "Validasi Form""');

            $bodyElemen = $dom->getElementsByTagName('body')->item(0);
            $this->assertStringContainsString('Sorry, you must access this page from validasiForm.html', $bodyElemen->nodeValue);
            $this->testResultsManager->showTestResult($testCase);
        } catch (\Throwable $e) {
            $this->testResultsManager->showTestResult($testCase, $e);
            throw $e;
        }
    }
}
