Skip to content

Commit

Permalink
the onInstanceCreated method has been created to management object cr…
Browse files Browse the repository at this point in the history
…eation
  • Loading branch information
diloabininyeri committed Feb 25, 2025
1 parent 8f5ddcb commit e04efe9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/MockClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +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 .= " }\n";


Expand Down
17 changes: 16 additions & 1 deletion src/MockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@
namespace Zeus\Mock;

use ReflectionException;
use Closure;

/**
* @mixin MockMethodInterface
*/
class MockFactory
{


/**
* @param MockMethodInterface $mockMethod
*/
public function __construct(private MockMethodInterface $mockMethod = new MockMethod())
{}
{
$this->mockMethod->mockMethod('object.on.created', fn() => null);
}

public static function from(MockMethod $mockMethod):self
{
return new self($mockMethod);
}

/**
* @template T
Expand Down Expand Up @@ -58,6 +67,12 @@ private function generateUniqueName(string $originalClass): string
str_replace('.', '', uniqid(time(), true));
}

public function onInstanceCreated(Closure $closure):self
{
$this->mockMethod->mockMethod('object.on.created', $closure);
return $this;
}

/**
* @param string $method
* @param array $arguments
Expand Down
6 changes: 5 additions & 1 deletion src/MockInterfaceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public function generate(string $mockClassName, string $interface): string
$reflection = new ReflectionClass($interface);
$mockCode = "class $mockClassName implements $interface {\n";
$mockCode .= " private {$this->mockMethodInterface} \$mockFactory;\n";
$mockCode .= " public function __construct({$this->mockMethodInterface} \$mockFactory) { \$this->mockFactory = \$mockFactory; }\n";
$mockCode .= " public function __construct({$this->mockMethodInterface} \$mockFactory)
{
\$this->mockFactory = \$mockFactory;
\$mockFactory->invokeMockedMethod('object.on.created',[\$this]);
}\n";

foreach ($reflection->getMethods() as $method) {
$mockCode .= $this->generateMethodOverride($method, false);
Expand Down

0 comments on commit e04efe9

Please sign in to comment.