-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Introduce attribute callback tests
- Loading branch information
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |