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

use PHPUnit\Framework\TestCase;

class FormRequiredTest extends TestCase
{

    private $testResultsManager;

    protected function setUp(): void
    {
        $this->testResultsManager = new TestResultsManager();
    }
    public function testVariableUndefined()
    {
        try {
            $testCase = get_class($this) . '.' . __FUNCTION__;
            $_GET['error'] = 'variable_undefined';

            ob_start();
            include './apps/formRequired.php';

            $output = ob_get_clean();
            $dom = new DOMDocument();
            $dom->loadHTML($output);
            $errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
            $expectedMessage = "Sorry, you must access this page from formRequired.php";
            $this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "variable_undefined" is out of specification');

            $this->verifyFormAttributes($dom);
            $this->testResultsManager->showTestResult($testCase);
        } catch (\Throwable $e) {
            $this->testResultsManager->showTestResult($testCase, $e);
            throw $e;
        }
    }

    public function testEmptyName()
    {
        try {
            $testCase = get_class($this) . '.' . __FUNCTION__;
            $_GET['error'] = 'empty_name';

            ob_start();
            include './apps/formRequired.php';

            $output = ob_get_clean();
            $dom = new DOMDocument();
            $dom->loadHTML($output);
            $errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
            $expectedMessage = "Sorry, name field required";
            $this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "empty_name" is out of specification');

            $this->verifyFormAttributes($dom);
            $this->testResultsManager->showTestResult($testCase);
        } catch (\Throwable $e) {
            $this->testResultsManager->showTestResult($testCase, $e);
            throw $e;
        }
    }

    public function testEmptyAddress()
    {
        try {
            $testCase = get_class($this) . '.' . __FUNCTION__;
            $_GET['error'] = 'empty_address';

            ob_start();
            include './apps/formRequired.php';

            $output = ob_get_clean();
            $dom = new DOMDocument();
            $dom->loadHTML($output);
            $errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
            $expectedMessage = "Sorry, address field required";
            $this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "empty_name" is out of specification');

            $this->verifyFormAttributes($dom);
            $this->testResultsManager->showTestResult($testCase);
        } catch (\Throwable $e) {
            $this->testResultsManager->showTestResult($testCase, $e);
            throw $e;
        }
    }

    private function verifyFormAttributes(DOMDocument $dom)
    {
        try {
            $testCase = get_class($this) . '.' . __FUNCTION__;
            $form = $dom->getElementsByTagName('form')->item(0);
            $this->assertEquals('prosesFormRequired.php', $form->attributes->getNamedItem('action')->nodeValue, 'The value of the action attribute on the form does not match the specifications');
            $this->assertEqualsIgnoringCase('GET', $form->attributes->getNamedItem('method')->nodeValue, 'Method attribute values on the form != GET');

            $labelName = $dom->getElementsByTagName('label')->item(0);
            $this->assertEquals('Your Name:', $labelName->textContent, 'The text content of the first label element in the form should be "Your Name"');

            $inputName = $dom->getElementsByTagName('input')->item(0);
            $this->assertEquals('text', $inputName->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the first input element in the form should be "text"');
            $this->assertEquals('yourname', $inputName->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute on the first input element in the form should be "yourname"');
            $this->assertEquals('', $inputName->attributes->getNamedItem('value')->nodeValue);

            $labelName = $dom->getElementsByTagName('label')->item(1);
            $this->assertEquals('Your Address:', $labelName->textContent, 'The text content of the third label element in the form should be "Your Address"');

            $inputAddress = $dom->getElementsByTagName('input')->item(1);
            $this->assertEquals('text', $inputAddress->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the second input element in the form should be "text"');
            $this->assertEquals('youraddress', $inputAddress->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the second input element in the form should be "youraddress"');
            $this->assertEquals('', $inputAddress->attributes->getNamedItem('value')->nodeValue);

            $inputSubmit = $dom->getElementsByTagName('input')->item(2);
            $this->assertEquals('submit', $inputSubmit->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the third input element in the form != submit');
            $this->assertEquals('submit', $inputSubmit->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the third input element in the form != submit');
            $this->assertEquals('Submit', $inputSubmit->attributes->getNamedItem('value')->nodeValue, 'The value attribute value in the third input element in the form != Submit');
            $this->testResultsManager->showTestResult($testCase);
        } catch (\Throwable $e) {
            $this->testResultsManager->showTestResult($testCase, $e);
            throw $e;
        }
    }
}
