Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mnapoli/phpunit-easymock
Build PHPUnit mocks easily
https://github.com/mnapoli/phpunit-easymock
php phpunit phpunit-extension phpunit-mock tests
Last synced: 5 days ago
JSON representation
Build PHPUnit mocks easily
- Host: GitHub
- URL: https://github.com/mnapoli/phpunit-easymock
- Owner: mnapoli
- License: mit
- Created: 2014-11-11T10:05:48.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2024-12-11T09:40:42.000Z (about 2 months ago)
- Last Synced: 2024-12-22T21:41:55.833Z (about 2 months ago)
- Topics: php, phpunit, phpunit-extension, phpunit-mock, tests
- Language: PHP
- Homepage:
- Size: 30.3 KB
- Stars: 38
- Watchers: 5
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# PHPUnit EasyMock
Helpers to build PHPUnit mock objects easily.
[![Total Downloads](https://poser.pugx.org/mnapoli/phpunit-easymock/downloads)](https://packagist.org/packages/mnapoli/phpunit-easymock)
## Why?
This library is **not** a mocking library. It's just a few helpers to write the most common mocks more easily.
It doesn't reinvent anything and is not intended to cover every use case: only the most common ones.
## Installation
```bash
$ composer require --dev mnapoli/phpunit-easymock
```To be able to use EasyMock in your tests **you must include the trait in your class**:
```php
class MyTest extends \PHPUnit\Framework\TestCase
{
use \EasyMock\EasyMock;// ...
}
```## Usage
Here is what a very common PHPUnit mock looks like:
```php
$mock = $this->createMock('My\Class');$mock->expect($this->any())
->method('sayHello')
->willReturn('Hello');
```Yuck!
Here is how to write it with EasyMock:
```php
$mock = $this->easyMock('My\Class', [
'sayHello' => 'Hello',
]);
```What if you want to assert that the method is called once (i.e. `$mock->expect($this->once())`)? Use `spy()` instead:
```php
$mock = $this->easySpy('My\Class', [
'sayHello' => 'Hello',
]);
```### Features
You can mock methods so that they return values:
```php
$mock = $this->easyMock('My\Class', [
'sayHello' => 'Hello',
]);
```Or so that they use a callback:
```php
$mock = $this->easyMock('My\Class', [
'sayHello' => function ($name) {
return 'Hello ' . $name;
},
]);
```You can also have methods throw exceptions by providing an `Exception` instance:
```php
$mock = $this->easyMock('My\Class', [
'sayHello' => new \RuntimeException('Whoops'),
]);
```It is possible to call the `mock()` method again on an existing mock:
```php
$mock = $this->easyMock('My\Class');$mock = $this->easyMock($mock, [
'sayHello' => 'Hello',
]);
```### What if?
If you want to use assertions or other PHPUnit features, just do it:
```php
$mock = $this->easyMock('My\Class', [
'sayHello' => 'hello',
]);$mock->expects($this->once())
->method('sayGoodbye')
->willReturn('Goodbye');
```Mocks are plain PHPUnit mocks, nothing special here.
## Contributing
See the [CONTRIBUTING](CONTRIBUTING.md) file.
## License
Released under the [MIT license](LICENSE).