66
77use function Flow \Types \DSL \{type_from_array , type_html };
88use Flow \Types \Exception \{CastingException , InvalidTypeException };
9- use PHPUnit \Framework \Attributes \DataProvider ;
9+ use Flow \Types \Value \HTMLDocument ;
10+ use PHPUnit \Framework \Attributes \{DataProvider , RequiresPhp };
1011use PHPUnit \Framework \TestCase ;
1112
1213final class HTMLTypeTest extends TestCase
1314{
1415 public static function assert_data_provider () : \Generator
1516 {
16- yield 'valid \DOMDocument ' => [
17- 'value ' => new \ DOMDocument ,
17+ yield 'valid HTMLDocument ' => [
18+ 'value ' => new HTMLDocument ( '' ) ,
1819 'exceptionClass ' => null ,
1920 ];
2021
@@ -59,14 +60,13 @@ public static function assert_data_provider() : \Generator
5960 ];
6061 }
6162
62- public static function cast_data_provider () : \Generator
63+ public static function cast_data_provider_php82 () : \Generator
6364 {
6465 yield 'string to HTML ' => [
6566 'value ' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html> ' ,
6667 'expected ' => <<<'HTML'
6768<!DOCTYPE html>
6869<html lang="en"><body><div><span>1</span></div></body></html>
69-
7070HTML,
7171 'exceptionClass ' => null ,
7272 ];
@@ -75,7 +75,29 @@ public static function cast_data_provider() : \Generator
7575 'value ' => '<div><span>1</span></div> ' ,
7676 'expected ' => <<<'HTML'
7777<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+ ];
7896
97+ yield 'incomplete string to HTML ' => [
98+ 'value ' => '<div><span>1</span></div> ' ,
99+ 'expected ' => <<<'HTML'
100+ <div><span>1</span></div>
79101HTML,
80102 'exceptionClass ' => null ,
81103 ];
@@ -89,8 +111,8 @@ public static function cast_data_provider() : \Generator
89111
90112 public static function is_valid_data_provider () : \Generator
91113 {
92- yield 'valid \DOMDocument ' => [
93- 'value ' => new \ DOMDocument ( ),
114+ yield 'valid HTMLDocument ' => [
115+ 'value ' => new HTMLDocument ( '' ),
94116 'expected ' => true ,
95117 ];
96118
@@ -117,19 +139,33 @@ public function test_assert(mixed $value, ?string $exceptionClass = null) : void
117139 $ this ->expectException ($ exceptionClass );
118140 type_html ()->assert ($ value );
119141 } else {
120- self ::assertInstanceOf (\DOMDocument::class, type_html ()->assert ($ value ));
142+ self ::assertInstanceOf (HTMLDocument::class, type_html ()->assert ($ value ));
143+ }
144+ }
145+
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 ());
121156 }
122157 }
123158
124- #[DataProvider('cast_data_provider ' )]
125- public function test_cast (mixed $ value , mixed $ expected , ?string $ exceptionClass ) : void
159+ #[RequiresPhp('>= 8.4 ' )]
160+ #[DataProvider('cast_data_provider_php84 ' )]
161+ public function test_cast_php84 (mixed $ value , mixed $ expected , ?string $ exceptionClass ) : void
126162 {
127163 if ($ exceptionClass !== null ) {
128164 $ this ->expectException ($ exceptionClass );
129165 type_html ()->cast ($ value );
130166 } else {
131167 $ result = type_html ()->cast ($ value );
132- self ::assertSame ($ expected , $ result ->saveHtml ());
168+ self ::assertSame ($ expected , $ result ->toString ());
133169 }
134170 }
135171
0 commit comments