Skip to content

Commit

Permalink
Add Url::class widget. (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 16, 2024
1 parent 159576d commit 024f53c
Show file tree
Hide file tree
Showing 4 changed files with 556 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Input/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Input;

use PHPForge\Html\Attribute;

/**
* The input element with a type attribute whose value is "url" represents a control for editing an absolute URL given
* in the element’s value.
*
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.url.html
*/
final class Url extends Base\AbstractInput implements
Contract\LengthInterface,
Contract\PatternInterface,
Contract\PlaceholderInterface,
Contract\RequiredInterface
{
use Attribute\Custom\HasWidgetValidation;
use Attribute\Input\CanBeRequired;
use Attribute\Input\HasMaxLength;
use Attribute\Input\HasMinLength;
use Attribute\Input\HasPattern;
use Attribute\Input\HasPlaceholder;
use Attribute\Input\HasSize;

protected function run(): string
{
$this->validateString($this->getValue());

return $this->buildInputTag($this->attributes, 'url');
}
}
315 changes: 315 additions & 0 deletions tests/Input/Url/AttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Input\Url;

use PHPForge\Html\Input\Url;
use PHPForge\Support\Assert;
use PHPUnit\Framework\TestCase;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class AttributeTest extends TestCase
{
public function testAriaDescribedBy(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" aria-describedby="value">
HTML,
Url::widget()->ariaDescribedBy('value')->id('url-6582f2d099e8b')->render()
);
}

public function testAriaLabel(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" aria-label="value">
HTML,
Url::widget()->ariaLabel('value')->id('url-6582f2d099e8b')->render()
);
}

public function testAttributes(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input class="value" id="url-6582f2d099e8b" type="url">
HTML,
Url::widget()->attributes(['class' => 'value'])->id('url-6582f2d099e8b')->render()
);
}

public function testAutofocus(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" autofocus>
HTML,
Url::widget()->autofocus()->id('url-6582f2d099e8b')->render()
);
}

public function testClass(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input class="value" id="url-6582f2d099e8b" type="url">
HTML,
Url::widget()->class('value')->id('url-6582f2d099e8b')->render()
);
}

public function testDataAttributes(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" data-value="value">
HTML,
Url::widget()->dataAttributes(['value' => 'value'])->id('url-6582f2d099e8b')->render()
);
}

public function testDisabled(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" disabled>
HTML,
Url::widget()->disabled()->id('url-6582f2d099e8b')->render()
);
}

public function testForm(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" form="value">
HTML,
Url::widget()->form('value')->id('url-6582f2d099e8b')->render()
);
}

public function testGenerateAriaDescribeBy(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" aria-describedby="url-6582f2d099e8b-help">
HTML,
Url::widget()->ariaDescribedBy()->id('url-6582f2d099e8b')->render()
);
}

public function testGenerateAriaDescribeByWithFalse(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url">
HTML,
Url::widget()->ariaDescribedBy(false)->id('url-6582f2d099e8b')->render()
);
}

public function testGenerateId(): void
{
$this->assertStringContainsString('id="url-', Url::widget()->render());
}

public function testGetValue(): void
{
$this->assertSame('value', Url::widget()->value('value')->getValue());
}

public function testHidden(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" hidden>
HTML,
Url::widget()->hidden()->id('url-6582f2d099e8b')->render()
);
}

public function testId(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="value" type="url">
HTML,
Url::widget()->id('value')->render()
);
}

public function testLang(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" lang="value">
HTML,
Url::widget()->id('url-6582f2d099e8b')->lang('value')->render()
);
}

public function testMaxLength(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" maxlength="1">
HTML,
Url::widget()->id('url-6582f2d099e8b')->maxlength(1)->render()
);
}

public function testMinLength(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" minlength="1">
HTML,
Url::widget()->id('url-6582f2d099e8b')->minlength(1)->render()
);
}

public function testName(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" name="value" type="url">
HTML,
Url::widget()->id('url-6582f2d099e8b')->name('value')->render()
);
}

public function testPattern(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" pattern="value">
HTML,
Url::widget()->id('url-6582f2d099e8b')->pattern('value')->render()
);
}

public function testPlaceholder(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" placeholder="value">
HTML,
Url::widget()->id('url-6582f2d099e8b')->placeholder('value')->render()
);
}

public function testReadonly(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" readonly>
HTML,
Url::widget()->id('url-6582f2d099e8b')->readonly()->render()
);
}

public function testRender(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url">
HTML,
Url::widget()->id('url-6582f2d099e8b')->render()
);
}

public function testRequired(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" required>
HTML,
Url::widget()->id('url-6582f2d099e8b')->required()->render()
);
}

public function testSize(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" size="1">
HTML,
Url::widget()->id('url-6582f2d099e8b')->size(1)->render()
);
}

public function testStyle(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" style="value">
HTML,
Url::widget()->id('url-6582f2d099e8b')->style('value')->render()
);
}

public function testTabIndex(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" tabindex="1">
HTML,
Url::widget()->id('url-6582f2d099e8b')->tabIndex(1)->render()
);
}

public function testTitle(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" title="value">
HTML,
Url::widget()->id('url-6582f2d099e8b')->title('value')->render()
);
}

public function testValue(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url" value="value">
HTML,
Url::widget()->id('url-6582f2d099e8b')->value('value')->render()
);
}

public function testValueWithEmpty(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url">
HTML,
Url::widget()->id('url-6582f2d099e8b')->value(null)->render()
);
}

public function testWithoutId(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input type="url">
HTML,
Url::widget()->id(null)->render()
);
}

public function testWithoutName(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="url-6582f2d099e8b" type="url">
HTML,
Url::widget()->id('url-6582f2d099e8b')->name(null)->render()
);
}
}

0 comments on commit 024f53c

Please sign in to comment.