Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jasonmccreary/test-double
A simple helper method to make using Mockery easier.
https://github.com/jasonmccreary/test-double
mockery phpunit
Last synced: 2 months ago
JSON representation
A simple helper method to make using Mockery easier.
- Host: GitHub
- URL: https://github.com/jasonmccreary/test-double
- Owner: jasonmccreary
- License: other
- Created: 2017-11-07T20:41:00.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-14T16:00:32.000Z (about 7 years ago)
- Last Synced: 2024-04-20T18:15:29.562Z (8 months ago)
- Topics: mockery, phpunit
- Language: PHP
- Homepage:
- Size: 5.86 KB
- Stars: 12
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Test Double
Tired of remembering the difference between mocks, partials, and spies in Mockery? I am, which is why I created `double()` - a simple helper method to make using Mockery easier.When writing tests I don't want to think about the differences between _fakes_, _mocks_, and _spies_. I just to create a generic _test double_ and focus on writing my test. This generalization is common in other testing frameworks such as [RSpec](https://relishapp.com/rspec/rspec-mocks/docs/basics/test-doubles), [td.js](https://github.com/testdouble/testdouble.js), and more.
## Installation
To install the latest version of the `double()` helper, run the command:```sh
composer require --dev jasonmccreary/test-double
```## Usage
Anytime you need to create a _test double_ simply call `double()`By default, `double()` returns an object that will allow you to stub methods as well as verify method calls.
```php
shouldReceive('someMethod')->andReturn(5);$td->someMethod(); // returns 5
$td->unstubbedMethod(); // returns null, does not throw an exception$td->anotherMethod();
$td->shouldHaveReceived('anotherMethod');
```In Mockery, this _test double_ is equivalent to `Mockery::mock()->shouldIgnoreMissing()` or, in recent versions, `Mockery::spy()`.
You can also pass `double()` a reference to a class or interface. This will create a test object that extends the class or implements the interface. This allows the double to pass any type hints or type checking in your implementation.
```php
shouldReceive('length')->andReturn(5);$td->length(); // 5
$td->substr(1, 3); // null$td instanceof Str; // true
$td->shouldHaveReceived('substr')->with(1, 3);
```Finally, `double()` accepts a second argument of _passthru_. By default, _passthru_ is `false`. When set to `true`, the test object will pass any method calls through to the underlying object.
In Mockery, this is equivalent to `Mockery::mock(Number::class)->shouldDeferMissing()`.
```php
shouldReceive('random')->andReturn(21);$td->random(); // 21
$td->one(); // 1$td instanceof Number; // true
$td->shouldHaveReceived('one');
```Note: _passthru_ can only be used when creating a test double with a class reference as that is the only time an underlying implementation exists.
In the end, `double()` is an opinionated way to create test objects for your underlying code. If it does not meet your needs, you can always create a `Mockery::mock()` directly. However, doing so is likely a smell you're testing your implementation in a way that does not reflect real world behavior. Remember, `double()` returns an object which implements the `MockeryInterface`. So it can be treated as any other `Mockery::mock()` object.