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

use PHPUnit\Framework\TestCase;

class ValidasiFormHTMLTest extends TestCase
{
    private $testResultsManager;

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

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

            $html = file_get_contents('./apps/validasiForm.html');
            $dom = new DOMDocument();
            $dom->loadHTML($html);

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

            $this->assertEquals('Validasi Form', $titleElemen->nodeValue);

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

            $this->assertEquals('validasiForm.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, 'The value of the method attribute on the form should be "GET"');

            // label assertion
            $labels = $dom->getElementsByTagName('label');
            $labelName = $labels->item(0);
            $labelAddress = $labels->item(1);

            $this->assertEquals('inputName', $labelName->attributes->getNamedItem('for')->nodeValue, 'The value of the for attribute in the first label tag should be "inputName"');
            $this->assertEquals('inputAddress', $labelAddress->attributes->getNamedItem('for')->nodeValue,  'The value of the name attribute in the second label tag should be "inputAddress"');

            $this->assertEquals('Your Name: ', $labelName->nodeValue, 'Text content of label element with attributes for == "inputName" should be "Your Name: "');
            $this->assertEquals('Your Address: ', $labelAddress->nodeValue, 'Text content of label element with attributes for == "inputAddress" should be "Your Address: "');

            // input assertion
            $inputs = $dom->getElementsByTagName('input');
            $inputName = $inputs->item(0);
            $inputAddress = $inputs->item(1);

            $this->assertNotNull($inputName, 'The first input tag is not found');
            $this->assertNotNull($inputAddress, 'The second input tag is not found');

            $this->assertEquals('yourName', $inputName->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the first input tag should be "yourName"');
            $this->assertEquals('yourAddress', $inputAddress->attributes->getNamedItem('name')->nodeValue,  'The value of the name attribute in the second input tag should be "yourAddress"');

            $this->assertEquals('text', $inputName->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the input element with name == "yourName" should be "text"');
            $this->assertEquals('inputName', $inputName->attributes->getNamedItem('id')->nodeValue, 'The value of the id attribute on the input element with name == "yourName" should be "inputName"');
            $this->assertEquals('Input Your Name', $inputName->attributes->getNamedItem('placeholder')->nodeValue, 'The value of the placeholder attribute on the input element with name == "yourName" should be "Input Your Name"');

            $this->assertEquals('text', $inputAddress->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the input element with name == "yourAddress" should be "text"');
            $this->assertEquals('inputAddress', $inputAddress->attributes->getNamedItem('id')->nodeValue, 'The value of the id attribute on the input element with name == "yourAddress" should be "inputAddress"');
            $this->assertEquals('Input Your Address', $inputAddress->attributes->getNamedItem('placeholder')->nodeValue, 'The value of the placeholder attribute on the input element with name == "yourAddress" should be "Input Your Address"');

            $inputSubmit = $inputs->item(2);
            $this->assertNotNull($inputName, 'The third input tag is not found');
            $this->assertEquals('submit', $inputSubmit->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute in the third input element in form != submit');
            $this->testResultsManager->showTestResult($testCase);
        } catch (\Throwable $e) {
            $this->testResultsManager->showTestResult($testCase, $e);
            throw $e;
        }
    }
}
