https://github.com/qratorlabs/smocky
Mocker helper based on runkit7
https://github.com/qratorlabs/smocky
helper mock mocking-methods php phpunit runkit7 testing
Last synced: 6 months ago
JSON representation
Mocker helper based on runkit7
- Host: GitHub
- URL: https://github.com/qratorlabs/smocky
- Owner: QratorLabs
- License: mit
- Created: 2021-02-19T12:56:22.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-03-20T18:08:45.000Z (7 months ago)
- Last Synced: 2025-04-19T08:14:12.975Z (6 months ago)
- Topics: helper, mock, mocking-methods, php, phpunit, runkit7, testing
- Language: PHP
- Homepage:
- Size: 92.8 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mock static methods
...and a bit more ;)
# Goals
- easy mocking
- make any changes revertible
- revert changes automatically
# Targets
There are several classes that will do the work:
- For class methods:
- `MockedClassMethod` to mock any class with closure
- `UndefinedClassMethod` to make method disappear
- For class constants:
- `MockedClassConstant`
- `UndefinedClassConstant`
- For global constants:
- `MockedGlobalConstant`
- `UndefinedGlobalConstant`
- For functions (global or Namespaced):
- `MockedFunction`
- `UndefinedFunction`
# Install
```shell
composer require --dev qratorlabs/smocky
```
### Note
There is a workaround that ensures that any (defined) children of class, which method is mocking, have its own method,
defined by user or mocked (by Smocky - closure that calls parent).
Example for code that will fail without this workaround following code will end up with `Segmentation fault: 11`
```php
class Base
{
public static function methodName()
{
return 'something';
}
}
class Child extends Base
{
}
// simulating PHPUnit test case
(new class('test') extends \PHPUnit\Framework\TestCase {
public function test(): void
{
// child should instanced (or loaded any other way)
$child = new Child();
// mocking method of base class
$method = new MockedClassMethod(Base::class, 'methodName');
// at least one call should be made
Base::methodName();
}
})->run();
```
# Trivia
## Revertible changes
All changes are made revertible by using internal storage and `__destruct` methods.
## Drawbacks
Thing to keep in mind before using:
- Mocking anything will hit memory consumption (ex. to preserve changes)
- Mocking methods will hit performance (a bit)
- To mock static class we must check (and mock) children of mocking class
# Powered by
- [runkit7](https://github.com/runkit7/runkit7)
- [phpunit](https://github.com/sebastianbergmann/phpunit)
# Tested with
- [phpunit](https://github.com/sebastianbergmann/phpunit)
- [phpstan](https://github.com/phpstan/phpstan)
- [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)