Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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;
```