From 0a5f563fecf5fc98268040e1056fe48eb3412788 Mon Sep 17 00:00:00 2001 From: Dilo surucu Date: Tue, 25 Feb 2025 15:08:52 -0500 Subject: [PATCH] bind created the instance object to onInstanceCreated parameter closure --- src/MockClassGenerator.php | 2 +- src/MockInterfaceGenerator.php | 2 +- src/MockMethod.php | 9 +++++++++ src/MockMethodInterface.php | 6 ++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/MockClassGenerator.php b/src/MockClassGenerator.php index be1a3a5..ab661c2 100644 --- a/src/MockClassGenerator.php +++ b/src/MockClassGenerator.php @@ -37,7 +37,7 @@ public function generate(string $mockClassName, string $class, bool $overrideCon $mockCode .= " public function __construct(\$mockFactory) {\n"; $mockCode .= $defineMockFactory; } - $mockCode.="\$mockFactory->invokeMockedMethod('object.on.created',[\$this]);\n"; + $mockCode.="\$mockFactory->getMockMethod('object.on.created')(\$this,\$params=[]);\n"; $mockCode .= " }\n"; diff --git a/src/MockInterfaceGenerator.php b/src/MockInterfaceGenerator.php index a3f0fcd..c2022e9 100644 --- a/src/MockInterfaceGenerator.php +++ b/src/MockInterfaceGenerator.php @@ -24,7 +24,7 @@ public function generate(string $mockClassName, string $interface): string $mockCode .= " public function __construct({$this->mockMethodInterface} \$mockFactory) { \$this->mockFactory = \$mockFactory; - \$mockFactory->invokeMockedMethod('object.on.created',[\$this]); + \$mockFactory->getMockMethod('object.on.created')(\$this); }\n"; foreach ($reflection->getMethods() as $method) { diff --git a/src/MockMethod.php b/src/MockMethod.php index 81463fd..b8bf049 100644 --- a/src/MockMethod.php +++ b/src/MockMethod.php @@ -49,4 +49,13 @@ public function mockMethod(string $methodName, Closure $closure): self $this->methods[$methodName] = $closure; return $this; } + + /** + * @param string $methodName + * @return Closure + */ + public function getMockMethod(string $methodName): Closure + { + return $this->methods[$methodName]; + } } diff --git a/src/MockMethodInterface.php b/src/MockMethodInterface.php index a9ade2c..3a39f98 100644 --- a/src/MockMethodInterface.php +++ b/src/MockMethodInterface.php @@ -31,4 +31,10 @@ public function invokeMockedMethod(string $methodName, array $arguments): mixed; * @return $this */ public function mockMethod(string $methodName, Closure $closure): MockMethod; + + /** + * @param string $methodName + * @return Closure + */ + public function getMockMethod(string $methodName): Closure; }