|
| 1 | +<?php |
| 2 | + |
| 3 | +use Html\Trait\GlobalAttribute\AutofocusTrait; |
| 4 | + |
| 5 | +class TestAutofocus |
| 6 | +{ |
| 7 | + use AutofocusTrait; |
| 8 | + |
| 9 | + public array $attributes = []; |
| 10 | + public $delegated; |
| 11 | + |
| 12 | + public function __construct() |
| 13 | + { |
| 14 | + $this->delegated = $this; |
| 15 | + } |
| 16 | + |
| 17 | + public function setAttribute(string $name, string $value) |
| 18 | + { |
| 19 | + $this->attributes[$name] = $value; |
| 20 | + return $this; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +test('setAutofocus true sets delegated attribute and getAutofocus returns true', function () { |
| 25 | + $obj = new TestAutofocus(); |
| 26 | + |
| 27 | + $obj->setAutofocus(true); |
| 28 | + |
| 29 | + expect($obj->getAutofocus())->toBeTrue() |
| 30 | + ->and($obj->attributes['autofocus'])->toBe('true'); |
| 31 | +}); |
| 32 | + |
| 33 | +test('setAutofocus with "true" string also sets attribute', function () { |
| 34 | + $obj = new TestAutofocus(); |
| 35 | + |
| 36 | + $obj->setAutofocus('true'); |
| 37 | + |
| 38 | + expect($obj->getAutofocus())->toBeTrue() |
| 39 | + ->and($obj->attributes['autofocus'])->toBe('true'); |
| 40 | +}); |
| 41 | + |
| 42 | +test('setAutofocus false does not set attribute', function () { |
| 43 | + $obj = new TestAutofocus(); |
| 44 | + |
| 45 | + $obj->setAutofocus(false); |
| 46 | + |
| 47 | + expect(array_key_exists('autofocus', $obj->attributes))->toBeFalse(); |
| 48 | +}); |
| 49 | + |
| 50 | +test('setAutofocus throws on invalid string', function () { |
| 51 | + $obj = new TestAutofocus(); |
| 52 | + |
| 53 | + expect(fn () => $obj->setAutofocus('maybe')) |
| 54 | + ->toThrow(InvalidArgumentException::class); |
| 55 | +}); |
0 commit comments