-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement BindGetter attribute (#266)
* build: upgrade to stable dom release * test: bind objects in arrays with bindList * test: ensure second check uses cache * feature: implement BindGetter Attribute closes #258 * test: bind callable value
- Loading branch information
Showing
6 changed files
with
127 additions
and
67 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,4 @@ | ||
<?php | ||
namespace Gt\DomTemplate; | ||
|
||
class BindGetterMethodDoesNotStartWithGetException extends DomTemplateException {} |
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
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
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,9 @@ | ||
<?php | ||
namespace Gt\DomTemplate\Test; | ||
|
||
use Attribute; | ||
|
||
#[Attribute] | ||
class BindGetter{ | ||
public function __construct() {} | ||
} |
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,76 @@ | ||
<?php | ||
namespace Gt\DomTemplate\Test; | ||
|
||
use Gt\DomTemplate\Bind; | ||
use Gt\DomTemplate\BindableCache; | ||
use Gt\DomTemplate\BindGetterMethodDoesNotStartWithGetException; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
class BindableCacheTest extends TestCase { | ||
public function testIsBindable_nonBindableCached():void { | ||
$obj = new StdClass(); | ||
$sut = new BindableCache(); | ||
self::assertFalse($sut->isBindable($obj)); | ||
self::assertFalse($sut->isBindable($obj)); | ||
} | ||
|
||
public function testIsBindable_bindableCached():void { | ||
$obj1 = new class extends StdClass { | ||
#[Bind("name")] | ||
public function getName():string { | ||
return "Test 1"; | ||
} | ||
}; | ||
|
||
$obj2 = new class extends StdClass { | ||
#[Bind("name")] | ||
public function getName():string { | ||
return "Test 2"; | ||
} | ||
}; | ||
|
||
$sut = new BindableCache(); | ||
self::assertTrue($sut->isBindable($obj1)); | ||
self::assertTrue($sut->isBindable($obj1)); | ||
self::assertTrue($sut->isBindable($obj2)); | ||
} | ||
|
||
public function testConvertToKvp_getter():void { | ||
$obj = new class { | ||
#[BindGetter] | ||
public function getName():string { | ||
return "Test Name"; | ||
} | ||
}; | ||
|
||
$sut = new BindableCache(); | ||
$kvp = $sut->convertToKvp($obj); | ||
self::assertEquals("Test Name", $kvp["name"]); | ||
} | ||
|
||
public function testConvertToKvp_getterDoesNotStartWithGet():void { | ||
$obj = new class { | ||
#[BindGetter] | ||
public function retrieveName():string { | ||
return "Test Name"; | ||
} | ||
}; | ||
|
||
$sut = new BindableCache(); | ||
self::expectException(BindGetterMethodDoesNotStartWithGetException::class); | ||
self::expectExceptionMessage("Method retrieveName has the BindGetter Attribute, but its name doesn't start with \"get\"."); | ||
$sut->convertToKvp($obj); | ||
} | ||
|
||
public function testConvertToKvp_notBindable():void { | ||
$obj = new class { | ||
public function getName():string { | ||
return "Test"; | ||
} | ||
}; | ||
|
||
$sut = new BindableCache(); | ||
self::assertSame([], $sut->convertToKvp($obj)); | ||
} | ||
} |
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