mockery/mockery
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
hamcrest/hamcrest-php | v2.0.1 | 116 kB | BSD-3-Clause | prod |
Mockery/mockery is a flexible and simple PHP mock object framework that is used in unit testing with PHPUnit, PHPSpec or any other testing framework. The primary goal of Mockery is to offer a test double framework that has a succinct API capable of clearly defining all possible operations and interactions using a human-readable Domain Specific Language (DSL). It's designed to serve as an alternative to PHPUnit's phpunit-mock-objects library, and can operate alongside phpunit-mock-objects without causing any issues. It's useful to provide test isolation, to stand in for objects which do not yet exist, or to allow for the exploratory design of class APIs without requiring implementation upfront.
To use Mockery, first, you need to install it using composer by running composer require --dev mockery/mockery
. Once installed, you can start creating your test double, also known as mocks. Here is a basic example of how to create a test double with Mockery:
$double = Mockery::mock();
If you need Mockery to create a test double that satisfies a particular type hint, you can pass the type to the mock
method.
class Book {}
interface BookRepository {
function find($id): Book;
function findAll(): array;
function add(Book $book): void;
}
$double = Mockery::mock(BookRepository::class);
You can also create and set up method stubs, setup expectations on method calls, apply spyOn, and much more with Mockery. You need to call Mockery::close()
at the end of every test.
public function tearDown()
{
Mockery::close();
}
The Mockery/mockery's documentation can be found at this location. The documentation provides extended guidance on how to use Mockery, including creating test doubles, setting up method stubs and expectations, using spies, and includes a variety of other utilities for testing.