From b496c3136e987a5f0500674be9827f813de3c391 Mon Sep 17 00:00:00 2001 From: sunxyw <31698606+sunxyw@users.noreply.github.com> Date: Thu, 5 May 2022 14:37:26 +0800 Subject: [PATCH] add macroable test (#115) --- tests/ZM/Utils/MacroableTest.php | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/ZM/Utils/MacroableTest.php diff --git a/tests/ZM/Utils/MacroableTest.php b/tests/ZM/Utils/MacroableTest.php new file mode 100644 index 00000000..2cd4a4c1 --- /dev/null +++ b/tests/ZM/Utils/MacroableTest.php @@ -0,0 +1,65 @@ +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(); + } +}