Skip to content

Commit 61b348d

Browse files
committed
Add docs
1 parent d57e273 commit 61b348d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ functions as: `time()`, `str_contains()`, `rand`, etc.
2020
- [Tracking calls](#tracking-calls)
2121
- [Global namespaced functions](#global-namespaced-functions)
2222
- [Internal functions](#internal-functions)
23-
- [Workaround](#workaround)
2423
- [Internal function implementation](#internal-function-implementation)
2524
- [Restrictions](#restrictions)
2625
- [Data Providers](#data-providers)
@@ -76,6 +75,14 @@ The main idea is pretty simple: register a Listener for PHPUnit and call the Moc
7675

7776
Here you have registered extension that will be called every time when you run `./vendor/bin/phpunit`.
7877

78+
By default, all functions will be generated and saved into `/vendor/bin/xepozz/internal-mocker/data/mocks.php` file.
79+
80+
Override the first argument of the `Mocker` constructor to change the path:
81+
82+
```php
83+
$mocker = new Mocker('/path/to/your/mocks.php');
84+
```
85+
7986
### Register mocks
8087

8188
The package supports a few ways to mock functions:
@@ -216,16 +223,22 @@ $traces = MockerState::getTraces('App\Service', 'time');
216223
]
217224
```
218225

219-
## Global namespaced functions
226+
### Function signature stubs
220227

221-
### Internal functions
228+
All internal functions are stubbed to be compatible with the original ones.
229+
It makes the functions use referenced arguments (`&$file`) as the originals do.
222230

223-
Without any additional configuration you can mock only functions that are defined under any not global
224-
namespaces: `App\`, `App\Service\`, etc.
225-
But you cannot mock functions that are defined under global namespace or defined in a `use` statement, e.g. `use time;`
226-
or `\time();`.
231+
They are located in the [`src/stubs.php`](src/stubs.php) file.
227232

228-
#### Workaround
233+
If you need to add a new function signature, override the second argument of the `Mocker` constructor:
234+
235+
```php
236+
$mocker = new Mocker(stubPath: '/path/to/your/stubs.php');
237+
```
238+
239+
## Global namespaced functions
240+
241+
### Internal functions
229242

230243
The way you can mock global functions is to disable them
231244
in `php.ini`: https://www.php.net/manual/en/ini.core.php#ini.disable-functions
@@ -269,6 +282,9 @@ $mocks[] = [
269282
];
270283
```
271284

285+
> Keep in mind that leaving a global function without implementation will cause a recourse call of the function,
286+
> that will lead to a fatal error.
287+
272288
## Restrictions
273289

274290
### Data Providers

src/Mocker.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
final class Mocker
1010
{
11-
public function __construct(private string $path = __DIR__ . '/../data/mocks.php')
12-
{
11+
public function __construct(
12+
private string $path = __DIR__ . '/../data/mocks.php',
13+
private string $stubPath = __DIR__ . '/stubs.php',
14+
) {
1315
}
1416

1517
public function load(array $mocks): void
@@ -52,7 +54,8 @@ public function generate(array $mocks): string
5254
}
5355
}
5456
}
55-
$stubs = require __DIR__ . '/stubs.php';
57+
58+
$stubs = require $this->stubPath;
5659

5760
$outputs = [];
5861
$mockerConfigClassName = MockerState::class;

0 commit comments

Comments
 (0)