Skip to content

Commit

Permalink
General improvements were made for onMockInstanceCreated and tests we…
Browse files Browse the repository at this point in the history
…re written for construct parameters.
  • Loading branch information
diloabininyeri committed Feb 25, 2025
1 parent 31492f6 commit f578b35
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ function fetch_data(PDO $PDO):array
$data=fetch_data($mockPdo);
print_r($data);////['id' => 1, 'name' => 'Dilo Surucu']
```
**onInstanceCreated**
**onMockInstanceCreated**

This method is triggered when the object is instantiated.
```php
Expand All @@ -442,7 +442,7 @@ $mockMethod->mockMethod('now', function (int $a) {

$mockFactory = new MockFactory($mockMethod);
$mockFactory
->onInstanceCreated(function (Date $date) {
->onMockInstanceCreated(function (Date $date) {
echo $date->test(); //test foo

//$date->__construct(); for custom constructor
Expand All @@ -457,7 +457,7 @@ $mockMethod = new MockMethod();


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

Expand Down
2 changes: 1 addition & 1 deletion src/MockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function generateUniqueName(string $originalClass): string
str_replace('.', '', uniqid(time(), true));
}

public function onInstanceCreated(Closure $closure):self
public function onMockInstanceCreated(Closure $closure):self
{
$this->mockMethod->mockMethod('object.on.created', $closure);
return $this;
Expand Down
11 changes: 11 additions & 0 deletions tests/stubs/ExampleForOnInstanced.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Zeus\Mock\Tests\stubs;

class ExampleForOnInstanced
{

public function __construct(string $message,bool $status)
{
}
}
35 changes: 35 additions & 0 deletions tests/unit/OnInstanceClosureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Zeus\Mock\Tests\unit;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use Zeus\Mock\MockFactory;
use Zeus\Mock\Tests\stubs\ExampleForOnInstanced;

class OnInstanceClosureTest extends TestCase
{

#[Test]
/**
*
* @throws ReflectionException
*/
public function onInstanceClosure(): void
{
$mockFactory = new MockFactory();
$mockFactory->onMockInstanceCreated(function (ExampleForOnInstanced $example, string $message, bool $status) {

$this->assertEquals('hello', $message);
$this->assertTrue($status);
$this->assertInstanceOf(ExampleForOnInstanced::class, $example);
});

$mockFactory->createMock(ExampleForOnInstanced::class,
[
'message' => 'hello',
'status' => true
]);
}
}

0 comments on commit f578b35

Please sign in to comment.