From 834bd7a0fe59c2c80a7db444d42c86ad7f211f9c Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 29 Nov 2022 10:27:41 +0100 Subject: [PATCH] Test attribute callbacks after cloning --- tests/CloneTest.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/CloneTest.php diff --git a/tests/CloneTest.php b/tests/CloneTest.php new file mode 100644 index 00000000..35b35fd0 --- /dev/null +++ b/tests/CloneTest.php @@ -0,0 +1,42 @@ +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(); + } +}