https://github.com/chubbyphp/chubbyphp-mock
A helper trait simplify mocking within phpunit.
https://github.com/chubbyphp/chubbyphp-mock
helper mock phpunit trait
Last synced: 18 days ago
JSON representation
A helper trait simplify mocking within phpunit.
- Host: GitHub
- URL: https://github.com/chubbyphp/chubbyphp-mock
- Owner: chubbyphp
- License: mit
- Created: 2018-07-28T14:27:36.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-03-12T13:55:27.000Z (about 2 months ago)
- Last Synced: 2025-03-27T18:55:27.104Z (about 1 month ago)
- Topics: helper, mock, phpunit, trait
- Language: PHP
- Homepage:
- Size: 231 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# chubbyphp-mock
[](https://github.com/chubbyphp/chubbyphp-mock/actions/workflows/ci.yml)
[](https://coveralls.io/github/chubbyphp/chubbyphp-mock?branch=master)
[](https://dashboard.stryker-mutator.io/reports/github.com/chubbyphp/chubbyphp-mock/master)
[](https://packagist.org/packages/chubbyphp/chubbyphp-mock)
[](https://packagist.org/packages/chubbyphp/chubbyphp-mock)
[](https://packagist.org/packages/chubbyphp/chubbyphp-mock)[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-mock)## Description
A strict mocking solution.
## Requirements
* php: ^8.2
## Installation
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-mock][1].
```sh
composer require chubbyphp/chubbyphp-mock "^2.0" --dev
```## Usage
```php
create(ServerRequestInterface::class, []);$responseBody = $builder->create(StreamInterface::class, [
new WithCallback('write', static function (string $string): int {
$data = json_decode($string, true);
self::assertArrayHasKey('datetime', $data);return \strlen($string);
}),
]);$response = $builder->create(ResponseInterface::class, [
new WithReturnSelf('withHeader', ['Content-Type', 'application/json']),
new WithReturnSelf('withHeader', ['Cache-Control', 'no-cache, no-store, must-revalidate']),
new WithReturnSelf('withHeader', ['Pragma', 'no-cache']),
new WithReturnSelf('withHeader', ['Expires', '0']),
new WithReturn('getBody', [], $responseBody),
]);$responseFactory = $builder->create(ResponseFactoryInterface::class, [
new WithReturn('createResponse', [200, ''], $response),
]);$requestHandler = new PingRequestHandler($responseFactory);
self::assertSame($response, $requestHandler->handle($request));
}
}
```## FAQ
### Howto mock final classes/methods
Use the third party package [dg/bypass-finals](https://packagist.org/packages/dg/bypass-finals).
**This does not work to get rid of the final keyword on internal classes.**
### What Cannot Be Mocked
- **Static methods**
- **Properties**
- **__construct, __destruct methods**
- **Interfaces extending internal interfaces:**
Interfaces that extend built-in PHP interfaces like `Traversable` are used as markers rather than containing methods. They cannot be mocked.- **Internal final classes or methods:**
Even with tools like `dg/bypass-finals`, you cannot mock internal final classes or methods.- **Poorly built extension classes:**
Some older PHP extensions create classes that cannot be fully reverse-engineered using reflection. These classes are not mockable.Please report if you find other restrictions / bugs.
## Upgrade
[Upgrade from 1.x](doc/upgrade-from-1.x.md)
## Copyright
2025 Dominik Zogg
[1]: https://packagist.org/packages/chubbyphp/chubbyphp-mock