diff --git a/tests/AttributesTest.php b/tests/AttributesTest.php index 50f922d3..eba3c346 100644 --- a/tests/AttributesTest.php +++ b/tests/AttributesTest.php @@ -2,7 +2,12 @@ namespace ipl\Tests\Html; +use Exception; +use ipl\Html\Attribute; use ipl\Html\Attributes; +use ipl\Tests\Html\TestDummy\ElementWithCallbackAttributes; +use RuntimeException; +use UnexpectedValueException; class AttributesTest extends TestCase { @@ -38,4 +43,91 @@ public function testForeach() next($attrs); } } + + public function testGetterCallbackInGet() + { + $callback = function () { + return new Attribute('callback', 'value from callback'); + }; + + $attributes = (new Attributes()) + ->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()); + } + + public function testCantOverrideCallbacks() + { + $callback = function () { + return new Attribute('callback', 'value from callback'); + }; + + $attributes = (new Attributes()) + ->setCallback('callback', $callback); + + $this->expectException(RuntimeException::class); + $attributes->set('callback', 'overridden'); + } + + public function testGetterCallbackRuntimeException() + { + $callback = function () { + throw new Exception(); + }; + + $attributes = (new Attributes()) + ->setCallback('callback', $callback); + + $this->expectException(RuntimeException::class); + $attributes->get('callback'); + } + + public function testGetterCallbackValueException() + { + $callback = function () { + return []; + }; + + $attributes = (new Attributes()) + ->setCallback('callback', $callback); + + $this->expectException(UnexpectedValueException::class); + $attributes->get('callback'); + } } diff --git a/tests/TestDummy/ElementWithCallbackAttributes.php b/tests/TestDummy/ElementWithCallbackAttributes.php new file mode 100644 index 00000000..fb18e6ba --- /dev/null +++ b/tests/TestDummy/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']); + } +}