diff --git a/tests/CloneTest.php b/tests/CloneTest.php index 689406ef..3c630c32 100644 --- a/tests/CloneTest.php +++ b/tests/CloneTest.php @@ -2,11 +2,14 @@ namespace ipl\Tests\Html; +use InvalidArgumentException; use ipl\Html\Attribute; use ipl\Html\Attributes; use ipl\Html\Form; +use ipl\Html\FormElement\InputElement; use ipl\Html\FormElement\SelectElement; use ipl\Html\FormElement\SubmitElement; +use ReflectionProperty; class CloneTest extends TestCase { @@ -65,4 +68,34 @@ public function testCloningAttributes(): void $this->assertNotSame($original, $clone); } + + public function testCallbacks() + { + $input = (new InputElement('text', 'text')) + ->setValue('original'); + $clone = (clone $input) + ->setValue('clone'); + + $this->assertEquals('original', $this->getAttributeCallbackValue($input->getAttributes(), 'value')); + $this->assertEquals('clone', $this->getAttributeCallbackValue($clone->getAttributes(), 'value')); + } + + protected function getAttributeCallbackValue(Attributes $attributes, string $name) + { + $callbacksProperty = new ReflectionProperty(get_class($attributes), 'callbacks'); + $callbacksProperty->setAccessible(true); + $callbacks = $callbacksProperty->getValue($attributes); + + if (isset($callbacks[$name])) { + $attribute = $callbacks[$name](); + + if ($attribute instanceof Attribute) { + return $attribute->getValue(); + } + + return $attribute; + } + + throw new InvalidArgumentException(); + } }