From 84dd33a6f066c2fb2d30fc3d306eab20873d5299 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 6 Aug 2018 15:15:01 +0200 Subject: [PATCH] WIP: Introduce attribute callback tests --- tests/php/AttributesTest.php | 105 ++++++++++++++++++++ tests/php/ElementWithCallbackAttributes.php | 32 ++++++ 2 files changed, 137 insertions(+) create mode 100644 tests/php/AttributesTest.php create mode 100644 tests/php/ElementWithCallbackAttributes.php diff --git a/tests/php/AttributesTest.php b/tests/php/AttributesTest.php new file mode 100644 index 00000000..32b35a34 --- /dev/null +++ b/tests/php/AttributesTest.php @@ -0,0 +1,105 @@ +setCallback('callback', $callback); + + $this->assertSame($attributes->get('callback')->getValue(), 'value from callback'); + } + + public function testSetterCallbackInSet() + { + $element = new ElementWithCallbackAttributes(); + + $attributes = $element->getAttributes(); + + $attributes->set('name', 'name from test'); + + $this->assertSame('name from test', $attributes->get('name')->getValue()); + $this->assertSame('name from test', $element->getName()); + } + + public function testSetterCallbackInAdd() + { + $element = new ElementWithCallbackAttributes(); + + $attributes = $element->getAttributes(); + + $attributes->add('name', 'name from test'); + + $this->assertSame('name from test', $attributes->get('name')->getValue()); + $this->assertSame('name from test', $element->getName()); + } + + public function testSetterCallbackIsProxied() + { + $element = new ElementWithCallbackAttributes(); + + $attributes = $element->getAttributes(); + + $attributes->get('name')->setValue('name from test'); + + $this->assertSame('name from test', $attributes->get('name')->getValue()); + $this->assertSame('name from test', $element->getName()); + } + + /** + * @expectedException RuntimeException + */ + public function testCantOverrideCallbacks() + { + $callback = function () { + return new Attribute('callback', 'value from callback'); + }; + + $attributes = (new Attributes()) + ->setCallback('callback', $callback); + + $attributes->set('callback', 'overridden'); + } + + /** + * @expectedException RuntimeException + */ + public function testGetterCallbackRuntimeException() + { + $callback = function () { + throw new Exception(); + }; + + $attributes = (new Attributes()) + ->setCallback('callback', $callback); + + $attributes->get('callback'); + } + + /** + * @expectedException UnexpectedValueException + */ + public function testGetterCallbackValueException() + { + $callback = function () { + return []; + }; + + $attributes = (new Attributes()) + ->setCallback('callback', $callback); + + $attributes->get('callback'); + } +} diff --git a/tests/php/ElementWithCallbackAttributes.php b/tests/php/ElementWithCallbackAttributes.php new file mode 100644 index 00000000..fb30e594 --- /dev/null +++ b/tests/php/ElementWithCallbackAttributes.php @@ -0,0 +1,32 @@ +registerCallbacks(); + } + + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + + return $this; + } + + protected function registerCallbacks() + { + $this->getAttributes()->setCallback('name', [$this, 'getName'], [$this, 'setName']); + } +}