Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lib/types/src/Flow/Types/Value/HTMLDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ final class HTMLDocument implements \Stringable

public function __construct(string|object $value)
{
if (\is_string($value)) {
if ('' === $value) {
$this->value = $value;
} elseif (\is_string($value)) {
if (\class_exists('\Dom\HTMLDocument', false)) {
$options = \LIBXML_HTML_NOIMPLIED;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

use function Flow\Types\DSL\{type_from_array, type_html};
use Flow\Types\Exception\{CastingException, InvalidTypeException};
use Flow\Types\Value\HTMLDocument;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class HTMLTypeTest extends TestCase
{
public static function assert_data_provider() : \Generator
{
yield 'valid \DOMDocument' => [
'value' => new \DOMDocument,
yield 'valid HTMLDocument' => [
'value' => new HTMLDocument(''),
'exceptionClass' => null,
];

Expand Down Expand Up @@ -66,7 +67,6 @@ public static function cast_data_provider() : \Generator
'expected' => <<<'HTML'
<!DOCTYPE html>
<html lang="en"><body><div><span>1</span></div></body></html>

HTML,
'exceptionClass' => null,
];
Expand All @@ -75,7 +75,6 @@ public static function cast_data_provider() : \Generator
'value' => '<div><span>1</span></div>',
'expected' => <<<'HTML'
<div><span>1</span></div>

HTML,
'exceptionClass' => null,
];
Expand All @@ -89,8 +88,8 @@ public static function cast_data_provider() : \Generator

public static function is_valid_data_provider() : \Generator
{
yield 'valid \DOMDocument' => [
'value' => new \DOMDocument(),
yield 'valid HTMLDocument' => [
'value' => new HTMLDocument(''),
'expected' => true,
];

Expand All @@ -117,7 +116,7 @@ public function test_assert(mixed $value, ?string $exceptionClass = null) : void
$this->expectException($exceptionClass);
type_html()->assert($value);
} else {
self::assertInstanceOf(\DOMDocument::class, type_html()->assert($value));
self::assertInstanceOf(HTMLDocument::class, type_html()->assert($value));
}
}

Expand All @@ -129,7 +128,7 @@ public function test_cast(mixed $value, mixed $expected, ?string $exceptionClass
type_html()->cast($value);
} else {
$result = type_html()->cast($value);
self::assertSame($expected, $result->saveHtml());
self::assertSame($expected, $result->toString());
}
}

Expand Down