Aliyya_Putri_Setiyomadani/form-testing/tests/GetFormHTMLTest.php
2024-12-31 11:09:29 +07:00

88 lines
4.8 KiB
PHP

<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class GetFormHTMLTest 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/getFormHandling.html');
$dom = new DOMDocument();
$dom->loadHTML($html);
$titleElemen = $dom->getElementsByTagName('title')->item(0);
$this->assertEquals('Form Handling', $titleElemen->nodeValue);
$form = $dom->getElementsByTagName('form')->item(0);
$this->assertEquals('getFormHandling.php', $form->attributes->getNamedItem('action')->nodeValue, "The value of the action attribute on the form doesn't 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 = null;
$labelAddress = null;
foreach ($labels as $label) {
$for = $label->getAttributeNode('for');
switch ($for->nodeValue) {
case 'inputName':
$labelName = $label;
break;
case 'inputAddress':
$labelAddress = $label;
break;
default:
$this->fail('There are unnecessary labels, label tags with attribute for == ' . $for->nodeValue);
break;
}
}
$this->assertNotNull($labelName, 'Label tag with for == "inputName" attribute not found');
$this->assertNotNull($labelAddress, 'Label tag with for == "inputAddress" attribute not found');
$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;
}
}
}