Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Mar 1, 2024
1 parent d57e273 commit 61b348d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ functions as: `time()`, `str_contains()`, `rand`, etc.
- [Tracking calls](#tracking-calls)
- [Global namespaced functions](#global-namespaced-functions)
- [Internal functions](#internal-functions)
- [Workaround](#workaround)
- [Internal function implementation](#internal-function-implementation)
- [Restrictions](#restrictions)
- [Data Providers](#data-providers)
Expand Down Expand Up @@ -76,6 +75,14 @@ The main idea is pretty simple: register a Listener for PHPUnit and call the Moc

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

By default, all functions will be generated and saved into `/vendor/bin/xepozz/internal-mocker/data/mocks.php` file.

Override the first argument of the `Mocker` constructor to change the path:

```php
$mocker = new Mocker('/path/to/your/mocks.php');
```

### Register mocks

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

## Global namespaced functions
### Function signature stubs

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

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

#### Workaround
If you need to add a new function signature, override the second argument of the `Mocker` constructor:

```php
$mocker = new Mocker(stubPath: '/path/to/your/stubs.php');
```

## Global namespaced functions

### Internal functions

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

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

### Data Providers
Expand Down
9 changes: 6 additions & 3 deletions src/Mocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

final class Mocker
{
public function __construct(private string $path = __DIR__ . '/../data/mocks.php')
{
public function __construct(
private string $path = __DIR__ . '/../data/mocks.php',
private string $stubPath = __DIR__ . '/stubs.php',
) {
}

public function load(array $mocks): void
Expand Down Expand Up @@ -52,7 +54,8 @@ public function generate(array $mocks): string
}
}
}
$stubs = require __DIR__ . '/stubs.php';

$stubs = require $this->stubPath;

$outputs = [];
$mockerConfigClassName = MockerState::class;
Expand Down

0 comments on commit 61b348d

Please sign in to comment.