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

use PHPUnit\Framework\TestCase;

class PostFormPHPTest extends TestCase
{
    private $testResultsManager;

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

    public function testPostFormHandlingResult()
    {
        try {
            $testCase = get_class($this) . '.' . __FUNCTION__;

            $_POST['yourName'] = 'AliyyaPS';
            $_POST['yourAddress'] = 'Sidoarjo';

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

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

            $titleElemen = $dom->getElementsByTagName('title')->item(0);
            $this->assertEquals('Post Form Handling', $titleElemen->nodeValue, 'The text content of the title element on the page should be "Post Form Handling"');

            $h3Elemen = $dom->getElementsByTagName('h3')->item(0);
            $this->assertEquals('Welcome!!', $h3Elemen->nodeValue, 'Text content of the h3 element should be "Welcome!!"');

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