Skip to content

Commit

Permalink
WIP: Introduce attribute callback tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd authored and nilmerg committed Apr 28, 2021
1 parent 35b852d commit 9c91ad8
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
92 changes: 92 additions & 0 deletions tests/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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');
}
}
32 changes: 32 additions & 0 deletions tests/TestDummy/ElementWithCallbackAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace ipl\Tests\Html\TestDummy;

use ipl\Html\BaseHtmlElement;

class ElementWithCallbackAttributes extends BaseHtmlElement
{
protected $name;

public function __construct()
{
$this->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']);
}
}

0 comments on commit 9c91ad8

Please sign in to comment.