https://github.com/gealex/doublit
Double and test PHP classes easily in PhpUnit
https://github.com/gealex/doublit
mock mocking php phpunit test test-doubles testing testing-tools unit-test unit-testing
Last synced: about 2 months ago
JSON representation
Double and test PHP classes easily in PhpUnit
- Host: GitHub
- URL: https://github.com/gealex/doublit
- Owner: gealex
- License: mit
- Created: 2018-07-21T10:03:06.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-04T10:57:24.000Z (almost 6 years ago)
- Last Synced: 2025-01-18T09:30:10.293Z (3 months ago)
- Topics: mock, mocking, php, phpunit, test, test-doubles, testing, testing-tools, unit-test, unit-testing
- Language: PHP
- Homepage: https://getdoublit.com
- Size: 170 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Doublit - Double and test PHP classes easily in PhpUnit
[](https://travis-ci.org/gealex/doublit)
Doublit can help you to test your PHP classes by generating doubles that look like the original classes but can be manipulated and tested (sort of a copy of a class). These doubles then can then be used instead of the original classes for your test. Doublit can create doubles of any kind of class, interface or trait.
See full documentation at [https://getdoublit.com](https://getdoublit.com)
## Installation
Add the line `"gealex/doublit": "~2.1.0"` in the `"require-dev"` section of your composer.json file :
{
"require-dev": {
"gealex/doublit": "~2.1.0"
}
}And run the following command :
$ composer update
This will install the latest version of Doublit with the required PhpUnit package.## Creating a double
A double is called a "dummy" when all the methods of the original class are overwritten to return `null`. To get a "dummy" double instance, use the `dummy` method :
```php
// Get a double instance of type "dummy" for class "MyClass"
$my_double = Doublit::dummy(MyClass::class)->getInstance();
```A double is called a "mock" when all the methods of the original class are overwritten to behave the same as in the original class. To get a "mock" double instance, use the `mock` method :
```php
// Get a double instance of type "mock" for class "MyClass"
$my_double = Doublit::mock(MyClass::class)->getInstance();
```
For more details : [Read the doc on creating doubles](doc/creating_doubles.md)## Testing a double
To test how many times a double method is called, use the `count` method :
```php
// Test that the method "myMethod" is called a least one time
$double::_method('myMethod')->count('>=1');
```To test the values of the arguments passed to a double method, use the `args` method :
```php
// Test that the arguments passed to method "myMethod" are "value1" and "value2"
$double::_method('myMethod')->args(['value1', 'value2']);
```To change the return value of a method, use the `stub` method. :
```php
// Make method "myMethod" return "hello"
$my_double::_method('myMethod')->stub('hello');
```For more details : [Read the doc on testing doubles](doc/testing_doubles.md)
## Configuration
You define the configuration for a specific double using the 2nd argument of the `dummy` and `mock` methods :
```php
// Get double instance with config
$my_double = Doublit::dummy(MyClass::class, [
'allow_final_doubles' => true,
'allow_non_existent_classes' => true
])->getInstance();
```
Here is a list of all available config parameters :- `allow_final_doubles` : Set this parameter to `false` to stop Doublit from trying to make doubles of final classes/methods.
- `allow_protected_methods` : Set this parameter to `false` to disallow testing protected methods.
- `allow_non_existent_classes` : Set this parameter to `false` to disallow alias doubles of non existent classes.
- `test_unexpected_methods` : Set this parameter to `true` to automatically receive an assertion error whenever an unexpected method is called.
For more details : [Read the doc on configuration](doc/configuration.md)## About
### License
Doublit is licensed under the [MIT license](https://opensource.org/licenses/MIT).### Author
Alexandre Geiswiller - [[email protected]](mailto:[email protected]).For more details : [Read the doc on about](doc/about.md)