Skip to content

Commit

Permalink
you can read the parameters sent to the constructor as soon as it is …
Browse files Browse the repository at this point in the history
…instanced.
  • Loading branch information
diloabininyeri committed Feb 25, 2025
1 parent e4a2264 commit 31492f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,18 @@ $mockFactory
})
->createMock(Date::class, ['a' => 1]);

```
You can get the parameters sent to the constructor as soon as it is instanced.
```php

$mockMethod = new MockMethod();


$mockFactory = new MockFactory($mockMethod);
$mockFactory->onInstanceCreated(function (Date $dateInstance, string $date) {
//$dateInstance
//$date 2022-12-12

});
$dateInstance = $mockFactory->createMock(Date::class, ['date' => '2022-12-12'], true);
```
5 changes: 4 additions & 1 deletion src/MockClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ public function generate(string $mockClassName, string $class, bool $overrideCon
if ($overrideConstruct) {
$mockCode .= " public function __construct({$this->mockMethodInterface} \$mockFactory, array \$params = []) {\n";
$mockCode .= $defineMockFactory;
$mockCode.=" \$mockFactory->getMockMethod('object.on.created')(\$this,...\$params);\n";
} elseif ($reflection->hasMethod('__construct')) {
$mockCode .= " public function __construct(\$mockFactory, array \$params = []) {\n";
$mockCode .= $defineMockFactory;
$mockCode .= " parent::__construct(...\$params);\n";
$mockCode.=" \$mockFactory->getMockMethod('object.on.created')(\$this,...\$params);\n";
} else {
$mockCode .= " public function __construct(\$mockFactory) {\n";
$mockCode .= $defineMockFactory;
$mockCode.="\$mockFactory->getMockMethod('object.on.created')(\$this,\$params=[]);\n";
}
$mockCode.="\$mockFactory->getMockMethod('object.on.created')(\$this,\$params=[]);\n";

$mockCode .= " }\n";


Expand Down
2 changes: 1 addition & 1 deletion src/MockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MockFactory
*/
public function __construct(private MockMethodInterface $mockMethod = new MockMethod())
{
$this->mockMethod->mockMethod('object.on.created', fn() => null);
$this->mockMethod->mockMethod('object.on.created', fn(...$args) => null);
}

public static function from(MockMethod $mockMethod):self
Expand Down

0 comments on commit 31492f6

Please sign in to comment.