|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Ziadoz\AssertableHtml\Dom\Elements; |
| 6 | + |
| 7 | +use Dom\Element; |
| 8 | +use Dom\HTMLElement; |
| 9 | +use Ziadoz\AssertableHtml\Concerns\AssertsMany; |
| 10 | +use Ziadoz\AssertableHtml\Contracts\PromotableAssertableElement; |
| 11 | +use Ziadoz\AssertableHtml\Dom\AssertableElement; |
| 12 | + |
| 13 | +readonly class AssertableForm extends AssertableElement implements PromotableAssertableElement |
| 14 | +{ |
| 15 | + use AssertsMany; |
| 16 | + |
| 17 | + /* |
| 18 | + |-------------------------------------------------------------------------- |
| 19 | + | Interface |
| 20 | + |-------------------------------------------------------------------------- |
| 21 | + */ |
| 22 | + |
| 23 | + /** {@inheritDoc} */ |
| 24 | + public static function shouldPromote(Element|HTMLElement $element): bool |
| 25 | + { |
| 26 | + return $element->tagName === 'FORM'; |
| 27 | + } |
| 28 | + |
| 29 | + /* |
| 30 | + |-------------------------------------------------------------------------- |
| 31 | + | Assert Method |
| 32 | + |-------------------------------------------------------------------------- |
| 33 | + */ |
| 34 | + |
| 35 | + /** Assert the form has the given method attribute. */ |
| 36 | + public function assertMethod(string $method, ?string $message = null): static |
| 37 | + { |
| 38 | + $method = trim(mb_strtolower($method)); |
| 39 | + |
| 40 | + $this->assertAttribute( |
| 41 | + 'method', |
| 42 | + fn (?string $value): bool => trim(mb_strtolower((string) $value)) === $method, |
| 43 | + $message ?? sprintf("The form method doesn't equal [%s].", $method), |
| 44 | + ); |
| 45 | + |
| 46 | + return $this; |
| 47 | + } |
| 48 | + |
| 49 | + /** Assert the form has the GET method attribute. */ |
| 50 | + public function assertMethodGet(?string $message = null): static |
| 51 | + { |
| 52 | + $this->assertMethod('get', $message); |
| 53 | + |
| 54 | + return $this; |
| 55 | + } |
| 56 | + |
| 57 | + /** Assert the form has the POST method attribute. */ |
| 58 | + public function assertMethodPost(?string $message = null): static |
| 59 | + { |
| 60 | + $this->assertMethod('post', $message); |
| 61 | + |
| 62 | + return $this; |
| 63 | + } |
| 64 | + |
| 65 | + /** Assert the form has the DIALOG method attribute. */ |
| 66 | + public function assertMethodDialog(?string $message = null): static |
| 67 | + { |
| 68 | + $this->assertMethod('dialog', $message); |
| 69 | + |
| 70 | + return $this; |
| 71 | + } |
| 72 | + |
| 73 | + /* |
| 74 | + |-------------------------------------------------------------------------- |
| 75 | + | Assert Hidden Method |
| 76 | + |-------------------------------------------------------------------------- |
| 77 | + */ |
| 78 | + |
| 79 | + /** Assert the form has the given hidden input method. */ |
| 80 | + public function assertHiddenInputMethod(string $selector, string $method, ?string $message = null): static |
| 81 | + { |
| 82 | + $this->assertMany(function () use ($selector, $method): void { |
| 83 | + $method = trim(mb_strtolower($method)); |
| 84 | + |
| 85 | + $this->querySelector($selector) |
| 86 | + ->assertMatchesSelector('input[type="hidden"]') |
| 87 | + ->assertAttribute('value', fn (?string $value): bool => trim(mb_strtolower((string) $value)) === $method); |
| 88 | + }, $message ?? sprintf("The form hidden input method doesn't equal [%s].", $method)); |
| 89 | + |
| 90 | + return $this; |
| 91 | + } |
| 92 | + |
| 93 | + /** Assert the form has the PUT hidden input method. */ |
| 94 | + public function assertMethodPut(?string $message = null): static |
| 95 | + { |
| 96 | + $this->assertHiddenInputMethod('input[type="hidden"][name="_method"]', 'put', $message); |
| 97 | + |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + /** Assert the form has the PATCH hidden input method. */ |
| 102 | + public function assertMethodPatch(?string $message = null): static |
| 103 | + { |
| 104 | + $this->assertHiddenInputMethod('input[type="hidden"][name="_method"]', 'patch', $message); |
| 105 | + |
| 106 | + return $this; |
| 107 | + } |
| 108 | + |
| 109 | + /** Assert the form has the DELETE hidden input method. */ |
| 110 | + public function assertMethodDelete(?string $message = null): static |
| 111 | + { |
| 112 | + $this->assertHiddenInputMethod('input[type="hidden"][name="_method"]', 'delete', $message); |
| 113 | + |
| 114 | + return $this; |
| 115 | + } |
| 116 | + |
| 117 | + /* |
| 118 | + |-------------------------------------------------------------------------- |
| 119 | + | Assert Upload |
| 120 | + |-------------------------------------------------------------------------- |
| 121 | + */ |
| 122 | + |
| 123 | + /** Assert the form accepts uploads (has correct enctype and at least one file input. */ |
| 124 | + public function assertAcceptsUpload(?string $message = null): static |
| 125 | + { |
| 126 | + $this->assertMany(function (): void { |
| 127 | + $this->assertAttribute('enctype', fn (?string $value): bool => trim(mb_strtolower((string) $value)) === 'multipart/form-data') |
| 128 | + ->assertElementsCountGreaterThanOrEqual('input[type="file"]', 1); |
| 129 | + }, $message ?? "The form doesn't accept uploads."); |
| 130 | + |
| 131 | + return $this; |
| 132 | + } |
| 133 | +} |
0 commit comments