Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/etcinit/mockingbird
A DSL for mocking dependencies on PHP unit tests
https://github.com/etcinit/mockingbird
dependency-injection dsl ioc mocking
Last synced: 3 months ago
JSON representation
A DSL for mocking dependencies on PHP unit tests
- Host: GitHub
- URL: https://github.com/etcinit/mockingbird
- Owner: etcinit
- Created: 2017-01-12T00:45:12.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-08T17:46:26.000Z (almost 8 years ago)
- Last Synced: 2023-08-21T08:51:42.129Z (over 1 year ago)
- Topics: dependency-injection, dsl, ioc, mocking
- Language: PHP
- Size: 26.4 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mockingbird
A DSL for mocking dependencies on PHP unit tests
## Features:
- Acts like a mini IoC container for your unit tests.
- Automatically creates mocks for your class' dependencies.
- Supports injecting dependencies through the constructor and methods.
- Provides a DSL on top of Mockery for quickly mocking dependencies.
- You can provide real/non-mock instances and scalar arguments.
- Works with Laravel!## Requirements
- PHP 7 or higher.
- Composer## Installation
Run `composer require chromabits/mockingbird` on a Composer project.
## Quick Example:
```php
prefix = $prefix;
}public function getPrefix(): string { return $this->prefix; }
};
class DependencyB {};
class DependencyC {
public function sayWorld(string $postfix): string {
return 'world' . $postfix;
}
}// We also define our service class which will consume these dependencies
// through constructor-based and method-based dependency injection.
class Service {
/**
* @var DependencyA
*/
private $a;public function __construct(DependencyA $a, DependencyB $b) {
$this->a = $a;
}public function targetMethod(DependencyC $c): string
{
return $this->a->getPrefix() . 'hello ' . $c->sayWorld('!');
}
};// Our Service class has three dependencies, two services injected through the
// constructor and one passed on the called method. We will build a stage that
// provides them for us:
//
// - DependencyA: We will pass down a real instance (not a mock).
// - DependencyB: We will let Stage auto-mock it for us.
// - DependencyC: We will manually create our own mock.
//
$result = stage()
->provide(new DependencyA('>>> '))
->mock(DependencyC::class, [
on('sayWorld', ['!'], 'worlds!!!'),
])
->makeAndCall(Service::class, 'targetMethod');// Should output ">>> hello worlds!!!"
echo $result;
```