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 Nov 29, 2022
1 parent b29a7e5 commit be97fd2
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/CloneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace ipl\Tests\Html;

use InvalidArgumentException;
use ipl\Html\Attribute;
use ipl\Html\Attributes;
use ipl\Html\FormElement\InputElement;
use ReflectionProperty;

class CloneTest extends TestCase
{
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 be97fd2

Please sign in to comment.