Skip to content

Commit a5863dc

Browse files
committed
Test attribute callbacks after cloning
1 parent 9622086 commit a5863dc

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

tests/CloneTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace ipl\Tests\Html;
44

5+
use InvalidArgumentException;
56
use ipl\Html\Attribute;
67
use ipl\Html\Attributes;
78
use ipl\Html\Form;
9+
use ipl\Html\FormElement\InputElement;
810
use ipl\Html\FormElement\SelectElement;
911
use ipl\Html\FormElement\SubmitElement;
12+
use ReflectionProperty;
1013

1114
class CloneTest extends TestCase
1215
{
@@ -65,4 +68,34 @@ public function testCloningAttributes(): void
6568

6669
$this->assertNotSame($original, $clone);
6770
}
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+
}
68101
}

0 commit comments

Comments
 (0)