66
77use Dom \Element ;
88use Dom \HTMLElement ;
9- use PHPUnit \ Framework \ Assert as PHPUnit ;
9+ use Ziadoz \ AssertableHtml \ Concerns \ AssertsMany ;
1010use Ziadoz \AssertableHtml \Contracts \PromotableAssertableElement ;
1111use Ziadoz \AssertableHtml \Dom \AssertableElement ;
1212
1313readonly class AssertableForm extends AssertableElement implements PromotableAssertableElement
1414{
15+ use AssertsMany;
16+
1517 /*
1618 |--------------------------------------------------------------------------
1719 | Interface
@@ -30,65 +32,95 @@ public static function shouldPromote(Element|HTMLElement $element): bool
3032 |--------------------------------------------------------------------------
3133 */
3234
33- /** Assert the form has the given method attribute (GET or POST) . */
35+ /** Assert the form has the given method attribute. */
3436 public function assertMethod (string $ method , ?string $ message = null ): static
3537 {
3638 $ method = trim (mb_strtolower ($ method ));
3739
38- $ this ->isValidMethod ($ method );
39-
4040 $ this ->assertAttribute (
4141 'method ' ,
4242 fn (?string $ value ): bool => trim (mb_strtolower ((string ) $ value )) === $ method ,
43- $ message ?? sprintf (
44- "The form method doesn't equal [%s]. " ,
45- $ method ,
46- ),
43+ $ message ?? sprintf ("The form method doesn't equal [%s]. " , $ method ),
4744 );
4845
4946 return $ this ;
5047 }
5148
52- /** Assert the form has the given hidden input method (PUT, PATCH or DELETE). */
53- public function assertHiddenInputMethod (string $ selector , string $ method , ?string $ message = null ): static
49+ public function assertMethodGet (?string $ message = null ): static
5450 {
55- $ method = trim ( mb_strtolower ( $ method ) );
51+ $ this -> assertMethod ( ' get ' , $ message );
5652
57- $ this ->isValidHiddenInputMethod ($ method );
53+ return $ this ;
54+ }
5855
59- $ this -> querySelector ( $ selector )
60- -> assertMatchesSelector ( ' input[type="hidden"] ' )
61- -> assertAttribute (
62- ' value ' ,
63- fn (? string $ value ): bool => trim ( mb_strtolower (( string ) $ value )) === $ method ,
64- $ message ?? sprintf (
65- " The form hidden input method doesn't equal [%s]. " ,
66- $ method ,
67- ),
68- );
56+ public function assertMethodPost (? string $ message = null ): static
57+ {
58+ $ this -> assertMethod ( ' post ' , $ message );
59+
60+ return $ this ;
61+ }
62+
63+ public function assertMethodDialog (? string $ message = null ): static
64+ {
65+ $ this -> assertMethod ( ' dialog ' , $ message );
6966
7067 return $ this ;
7168 }
7269
7370 /*
7471 |--------------------------------------------------------------------------
75- | Internal
72+ | Assert Hidden Method
7673 |--------------------------------------------------------------------------
7774 */
7875
79- /** Fail if the method isn't a valid form method. */
80- protected function isValidMethod (string $ method ): void
76+ /** Assert the form has the given hidden input method. */
77+ public function assertHiddenInputMethod (string $ selector , string $ method , ?string $ message = null ): static
78+ {
79+ $ this ->assertMany (function () use ($ selector , $ method ): void {
80+ $ method = trim (mb_strtolower ($ method ));
81+
82+ $ this ->querySelector ($ selector )
83+ ->assertMatchesSelector ('input[type="hidden"] ' )
84+ ->assertAttribute ('value ' , fn (?string $ value ): bool => trim (mb_strtolower ((string ) $ value )) === $ method );
85+ }, $ message ?? sprintf ("The form hidden input method doesn't equal [%s]. " , $ method ));
86+
87+ return $ this ;
88+ }
89+
90+ public function assertMethodPut (?string $ message = null ): static
8191 {
82- if (! in_array ($ this ->formatMethod ($ method ), ['get ' , 'post ' , 'dialog ' ])) {
83- PHPUnit::fail (sprintf ("The method [%s] isn't a valid form method. " , $ method ));
84- }
92+ $ this ->assertHiddenInputMethod ('input[type="hidden"][name="_method"] ' , 'put ' , $ message );
93+
94+ return $ this ;
95+ }
96+
97+ public function assertMethodPatch (?string $ message = null ): static
98+ {
99+ $ this ->assertHiddenInputMethod ('input[type="hidden"][name="_method"] ' , 'patch ' , $ message );
100+
101+ return $ this ;
85102 }
86103
87- /** Fail if the method isn't a valid hidden input method. */
88- protected function isValidHiddenInputMethod (string $ method ): void
104+ public function assertMethodDelete (?string $ message = null ): static
89105 {
90- if (! in_array ($ method , ['put ' , 'patch ' , 'delete ' ])) {
91- PHPUnit::fail (sprintf ("The method [%s] isn't a valid form method. " , $ method ));
92- }
106+ $ this ->assertHiddenInputMethod ('input[type="hidden"][name="_method"] ' , 'delete ' , $ message );
107+
108+ return $ this ;
109+ }
110+
111+ /*
112+ |--------------------------------------------------------------------------
113+ | Assert Upload
114+ |--------------------------------------------------------------------------
115+ */
116+
117+ public function assertAcceptsUpload (?string $ message = null ): static
118+ {
119+ $ this ->assertMany (function (): void {
120+ $ this ->assertAttribute ('enctype ' , fn (?string $ value ): bool => trim (mb_strtolower ((string ) $ value )) === 'multipart/form-data ' )
121+ ->assertElementsCountGreaterThanOrEqual ('input[type="file"] ' , 1 );
122+ }, $ message ?? "The form doesn't accept uploads. " );
123+
124+ return $ this ;
93125 }
94126}
0 commit comments