mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-17 20:54:52 +08:00
add macroable test (#115)
This commit is contained in:
parent
bbfb40f973
commit
b496c3136e
65
tests/ZM/Utils/MacroableTest.php
Normal file
65
tests/ZM/Utils/MacroableTest.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\ZM\Utils;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use ZM\Exception\MethodNotFoundException;
|
||||||
|
use ZM\Utils\Macroable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
class MacroableTest extends TestCase
|
||||||
|
{
|
||||||
|
private $macroable;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->macroable = new class() {
|
||||||
|
use Macroable;
|
||||||
|
|
||||||
|
private $secret = 'secret';
|
||||||
|
|
||||||
|
private static function anotherSecret()
|
||||||
|
{
|
||||||
|
return 'another secret';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMacroCanBeDefined(): void
|
||||||
|
{
|
||||||
|
$this->macroable::macro('getSecret', function () {
|
||||||
|
return $this->secret;
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertEquals('secret', $this->macroable->getSecret());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMacroCanBeDefinedStatically(): void
|
||||||
|
{
|
||||||
|
$this->macroable::macro('getSecret', static function () {
|
||||||
|
return 'static secret';
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertEquals('static secret', $this->macroable::getSecret());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMacroCanBeDefinedWithParameters(): void
|
||||||
|
{
|
||||||
|
$this->macroable::macro('getParam', function ($param) {
|
||||||
|
return $param;
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertEquals('param', $this->macroable->getParam('param'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExceptionIsThrownWhenMacroIsNotDefined(): void
|
||||||
|
{
|
||||||
|
$this->expectException(MethodNotFoundException::class);
|
||||||
|
|
||||||
|
$this->macroable->unknownMacro();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user