|
7 | 7 | use function Flow\Types\DSL\{type_from_array, type_html}; |
8 | 8 | use Flow\Types\Exception\{CastingException, InvalidTypeException}; |
9 | 9 | use Flow\Types\Value\HTMLDocument; |
10 | | -use PHPUnit\Framework\Attributes\DataProvider; |
| 10 | +use PHPUnit\Framework\Attributes\{DataProvider, RequiresPhp}; |
11 | 11 | use PHPUnit\Framework\TestCase; |
12 | 12 |
|
13 | 13 | final class HTMLTypeTest extends TestCase |
@@ -60,24 +60,39 @@ public static function assert_data_provider() : \Generator |
60 | 60 | ]; |
61 | 61 | } |
62 | 62 |
|
63 | | - public static function cast_data_provider() : \Generator |
| 63 | + public static function cast_data_provider_php82() : \Generator |
64 | 64 | { |
65 | | - if (PHP_VERSION_ID >= 80400) { |
66 | | - yield 'string to HTML' => [ |
67 | | - 'value' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>', |
68 | | - 'expected' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>', |
69 | | - 'exceptionClass' => null, |
70 | | - ]; |
71 | | - } else { |
72 | | - yield 'string to HTML' => [ |
73 | | - 'value' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>', |
74 | | - 'expected' => <<<'HTML' |
| 65 | + yield 'string to HTML' => [ |
| 66 | + 'value' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>', |
| 67 | + 'expected' => <<<'HTML' |
75 | 68 | <!DOCTYPE html> |
76 | 69 | <html lang="en"><body><div><span>1</span></div></body></html> |
77 | 70 | HTML, |
78 | | - 'exceptionClass' => null, |
79 | | - ]; |
80 | | - } |
| 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 cast_data_provider_php84() : \Generator |
| 90 | + { |
| 91 | + yield 'string to HTML' => [ |
| 92 | + 'value' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>', |
| 93 | + 'expected' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>', |
| 94 | + 'exceptionClass' => null, |
| 95 | + ]; |
81 | 96 |
|
82 | 97 | yield 'incomplete string to HTML' => [ |
83 | 98 | 'value' => '<div><span>1</span></div>', |
@@ -128,8 +143,22 @@ public function test_assert(mixed $value, ?string $exceptionClass = null) : void |
128 | 143 | } |
129 | 144 | } |
130 | 145 |
|
131 | | - #[DataProvider('cast_data_provider')] |
132 | | - public function test_cast(mixed $value, mixed $expected, ?string $exceptionClass) : void |
| 146 | + #[RequiresPhp('< 8.4')] |
| 147 | + #[DataProvider('cast_data_provider_php82')] |
| 148 | + public function test_cast_php82(mixed $value, mixed $expected, ?string $exceptionClass) : void |
| 149 | + { |
| 150 | + if ($exceptionClass !== null) { |
| 151 | + $this->expectException($exceptionClass); |
| 152 | + type_html()->cast($value); |
| 153 | + } else { |
| 154 | + $result = type_html()->cast($value); |
| 155 | + self::assertSame($expected, $result->toString()); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + #[RequiresPhp('>= 8.4')] |
| 160 | + #[DataProvider('cast_data_provider_php84')] |
| 161 | + public function test_cast_php84(mixed $value, mixed $expected, ?string $exceptionClass) : void |
133 | 162 | { |
134 | 163 | if ($exceptionClass !== null) { |
135 | 164 | $this->expectException($exceptionClass); |
|
0 commit comments