Skip to content

Commit

Permalink
Test attribute callbacks after cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed Dec 13, 2022
1 parent 9622086 commit a5863dc
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/CloneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}
}

0 comments on commit a5863dc

Please sign in to comment.