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 committed Aug 6, 2018
1 parent ea374a1 commit 84dd33a
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
105 changes: 105 additions & 0 deletions tests/php/AttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace ipl\Tests\Html;

use Exception;
use RuntimeException;
use UnexpectedValueException;
use ipl\Html\Attribute;
use ipl\Html\Attributes;

class AttributesTest extends TestCase
{
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());
}

/**
* @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');
}
}
32 changes: 32 additions & 0 deletions tests/php/ElementWithCallbackAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace ipl\Tests\Html;

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 84dd33a

Please sign in to comment.