-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test attribute callbacks after cloning
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |