Skip to content

Commit f578b35

Browse files
General improvements were made for onMockInstanceCreated and tests were written for construct parameters.
1 parent 31492f6 commit f578b35

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ function fetch_data(PDO $PDO):array
425425
$data=fetch_data($mockPdo);
426426
print_r($data);////['id' => 1, 'name' => 'Dilo Surucu']
427427
```
428-
**onInstanceCreated**
428+
**onMockInstanceCreated**
429429

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

443443
$mockFactory = new MockFactory($mockMethod);
444444
$mockFactory
445-
->onInstanceCreated(function (Date $date) {
445+
->onMockInstanceCreated(function (Date $date) {
446446
echo $date->test(); //test foo
447447

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

458458

459459
$mockFactory = new MockFactory($mockMethod);
460-
$mockFactory->onInstanceCreated(function (Date $dateInstance, string $date) {
460+
$mockFactory->onMockInstanceCreated(function (Date $dateInstance, string $date) {
461461
//$dateInstance
462462
//$date 2022-12-12
463463

src/MockFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private function generateUniqueName(string $originalClass): string
6767
str_replace('.', '', uniqid(time(), true));
6868
}
6969

70-
public function onInstanceCreated(Closure $closure):self
70+
public function onMockInstanceCreated(Closure $closure):self
7171
{
7272
$this->mockMethod->mockMethod('object.on.created', $closure);
7373
return $this;

tests/stubs/ExampleForOnInstanced.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Zeus\Mock\Tests\stubs;
4+
5+
class ExampleForOnInstanced
6+
{
7+
8+
public function __construct(string $message,bool $status)
9+
{
10+
}
11+
}

tests/unit/OnInstanceClosureTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Zeus\Mock\Tests\unit;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\TestCase;
7+
use ReflectionException;
8+
use Zeus\Mock\MockFactory;
9+
use Zeus\Mock\Tests\stubs\ExampleForOnInstanced;
10+
11+
class OnInstanceClosureTest extends TestCase
12+
{
13+
14+
#[Test]
15+
/**
16+
*
17+
* @throws ReflectionException
18+
*/
19+
public function onInstanceClosure(): void
20+
{
21+
$mockFactory = new MockFactory();
22+
$mockFactory->onMockInstanceCreated(function (ExampleForOnInstanced $example, string $message, bool $status) {
23+
24+
$this->assertEquals('hello', $message);
25+
$this->assertTrue($status);
26+
$this->assertInstanceOf(ExampleForOnInstanced::class, $example);
27+
});
28+
29+
$mockFactory->createMock(ExampleForOnInstanced::class,
30+
[
31+
'message' => 'hello',
32+
'status' => true
33+
]);
34+
}
35+
}

0 commit comments

Comments
 (0)