|
2 | 2 |
|
3 | 3 | namespace ipl\Tests\Html;
|
4 | 4 |
|
| 5 | +use InvalidArgumentException; |
5 | 6 | use ipl\Html\Attribute;
|
6 | 7 | use ipl\Html\Attributes;
|
7 | 8 | use ipl\Html\Form;
|
| 9 | +use ipl\Html\FormElement\InputElement; |
8 | 10 | use ipl\Html\FormElement\SelectElement;
|
9 | 11 | use ipl\Html\FormElement\SubmitElement;
|
| 12 | +use ReflectionProperty; |
10 | 13 |
|
11 | 14 | class CloneTest extends TestCase
|
12 | 15 | {
|
@@ -65,4 +68,34 @@ public function testCloningAttributes(): void
|
65 | 68 |
|
66 | 69 | $this->assertNotSame($original, $clone);
|
67 | 70 | }
|
| 71 | + |
| 72 | + public function testCallbacks() |
| 73 | + { |
| 74 | + $input = (new InputElement('text', 'text')) |
| 75 | + ->setValue('original'); |
| 76 | + $clone = (clone $input) |
| 77 | + ->setValue('clone'); |
| 78 | + |
| 79 | + $this->assertEquals('original', $this->getAttributeCallbackValue($input->getAttributes(), 'value')); |
| 80 | + $this->assertEquals('clone', $this->getAttributeCallbackValue($clone->getAttributes(), 'value')); |
| 81 | + } |
| 82 | + |
| 83 | + protected function getAttributeCallbackValue(Attributes $attributes, string $name) |
| 84 | + { |
| 85 | + $callbacksProperty = new ReflectionProperty(get_class($attributes), 'callbacks'); |
| 86 | + $callbacksProperty->setAccessible(true); |
| 87 | + $callbacks = $callbacksProperty->getValue($attributes); |
| 88 | + |
| 89 | + if (isset($callbacks[$name])) { |
| 90 | + $attribute = $callbacks[$name](); |
| 91 | + |
| 92 | + if ($attribute instanceof Attribute) { |
| 93 | + return $attribute->getValue(); |
| 94 | + } |
| 95 | + |
| 96 | + return $attribute; |
| 97 | + } |
| 98 | + |
| 99 | + throw new InvalidArgumentException(); |
| 100 | + } |
68 | 101 | }
|
0 commit comments