|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\Types\Tests\Unit\Type\Logical; |
| 6 | + |
| 7 | +use function Flow\Types\DSL\{type_from_array, type_html}; |
| 8 | +use DOM\HTMLDocument; |
| 9 | +use Flow\Types\Exception\{CastingException, InvalidTypeException}; |
| 10 | +use PHPUnit\Framework\Attributes\{DataProvider, RequiresPhp}; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +#[RequiresPhp('8.4')] |
| 14 | +final class HTMLTypeTest extends TestCase |
| 15 | +{ |
| 16 | + public static function assert_data_provider() : \Generator |
| 17 | + { |
| 18 | + yield 'valid \DOM\HTMLDocument' => [ |
| 19 | + 'value' => HTMLDocument::createEmpty(), |
| 20 | + 'exceptionClass' => null, |
| 21 | + ]; |
| 22 | + |
| 23 | + yield 'invalid string' => [ |
| 24 | + 'value' => 'string', |
| 25 | + 'exceptionClass' => InvalidTypeException::class, |
| 26 | + ]; |
| 27 | + |
| 28 | + yield 'invalid UUID string' => [ |
| 29 | + 'value' => '49e952c8-80ec-4910-a1d6-a19bd46b163d', |
| 30 | + 'exceptionClass' => InvalidTypeException::class, |
| 31 | + ]; |
| 32 | + |
| 33 | + yield 'invalid boolean' => [ |
| 34 | + 'value' => false, |
| 35 | + 'exceptionClass' => InvalidTypeException::class, |
| 36 | + ]; |
| 37 | + |
| 38 | + yield 'invalid float' => [ |
| 39 | + 'value' => 124.25, |
| 40 | + 'exceptionClass' => InvalidTypeException::class, |
| 41 | + ]; |
| 42 | + |
| 43 | + yield 'invalid array' => [ |
| 44 | + 'value' => [1, 2], |
| 45 | + 'exceptionClass' => InvalidTypeException::class, |
| 46 | + ]; |
| 47 | + |
| 48 | + yield 'invalid object' => [ |
| 49 | + 'value' => new \stdClass(), |
| 50 | + 'exceptionClass' => InvalidTypeException::class, |
| 51 | + ]; |
| 52 | + |
| 53 | + yield 'invalid DateTimeZone' => [ |
| 54 | + 'value' => new \DateTimeZone('UTC'), |
| 55 | + 'exceptionClass' => InvalidTypeException::class, |
| 56 | + ]; |
| 57 | + |
| 58 | + yield 'invalid DateTimeImmutable' => [ |
| 59 | + 'value' => new \DateTimeImmutable(), |
| 60 | + 'exceptionClass' => InvalidTypeException::class, |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + public static function cast_data_provider() : \Generator |
| 65 | + { |
| 66 | + yield 'string to HTML' => [ |
| 67 | + 'value' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>', |
| 68 | + 'expected' => <<<'HTML' |
| 69 | +<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html> |
| 70 | +HTML, |
| 71 | + 'exceptionClass' => null, |
| 72 | + ]; |
| 73 | + |
| 74 | + yield 'incomplete string to HTML' => [ |
| 75 | + 'value' => '<div><span>1</span></div>', |
| 76 | + 'expected' => <<<'HTML' |
| 77 | +<div><span>1</span></div> |
| 78 | +HTML, |
| 79 | + 'exceptionClass' => null, |
| 80 | + ]; |
| 81 | + |
| 82 | + yield 'object to HTML' => [ |
| 83 | + 'value' => new \stdClass(), |
| 84 | + 'expected' => null, |
| 85 | + 'exceptionClass' => CastingException::class, |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + public static function is_valid_data_provider() : \Generator |
| 90 | + { |
| 91 | + yield 'valid \DOM\HTMLDocument' => [ |
| 92 | + 'value' => HTMLDocument::createEmpty(), |
| 93 | + 'expected' => true, |
| 94 | + ]; |
| 95 | + |
| 96 | + yield 'invalid HTML string' => [ |
| 97 | + 'value' => '<html></html>', |
| 98 | + 'expected' => false, |
| 99 | + ]; |
| 100 | + |
| 101 | + yield 'invalid date string' => [ |
| 102 | + 'value' => '2020-01-01', |
| 103 | + 'expected' => false, |
| 104 | + ]; |
| 105 | + |
| 106 | + yield 'invalid datetime string' => [ |
| 107 | + 'value' => '2020-01-01 00:00:00', |
| 108 | + 'expected' => false, |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + #[DataProvider('assert_data_provider')] |
| 113 | + public function test_assert(mixed $value, ?string $exceptionClass = null) : void |
| 114 | + { |
| 115 | + if ($exceptionClass !== null) { |
| 116 | + $this->expectException($exceptionClass); |
| 117 | + type_html()->assert($value); |
| 118 | + } else { |
| 119 | + self::assertInstanceOf(HTMLDocument::class, type_html()->assert($value)); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + #[DataProvider('cast_data_provider')] |
| 124 | + public function test_cast(mixed $value, mixed $expected, ?string $exceptionClass) : void |
| 125 | + { |
| 126 | + if ($exceptionClass !== null) { |
| 127 | + $this->expectException($exceptionClass); |
| 128 | + type_html()->cast($value); |
| 129 | + } else { |
| 130 | + $result = type_html()->cast($value); |
| 131 | + self::assertSame($expected, $result->saveHtml()); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + #[DataProvider('is_valid_data_provider')] |
| 136 | + public function test_is_valid(mixed $value, bool $expected) : void |
| 137 | + { |
| 138 | + self::assertSame($expected, type_html()->isValid($value)); |
| 139 | + } |
| 140 | + |
| 141 | + public function test_normalization() : void |
| 142 | + { |
| 143 | + $type = type_html(); |
| 144 | + $normalized = $type->normalize(); |
| 145 | + |
| 146 | + self::assertEquals($type, type_from_array($normalized)); |
| 147 | + } |
| 148 | + |
| 149 | + public function test_to_string() : void |
| 150 | + { |
| 151 | + self::assertSame( |
| 152 | + 'html', |
| 153 | + type_html()->toString() |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
0 commit comments