pestphp/pest-plugin-mock
Pest plugin that integrates Mockery into Pest tests, providing convenient helpers to create, configure, and verify mocks and spies with a simple, Pest-friendly API for unit and feature testing in PHP/Laravel projects.
Install the plugin via Composer (composer require --dev pestphp/pest-plugin-mock), then enable it in your pest.php config by adding use Pest\Mixins\Mock;. The plugin provides a lightweight, expressive way to create and interact with test doubles—especially for classes where full mocking frameworks like Mockery or PHPUnit are overkill. Your first use case: stubbing a dependency in a unit test for a service class.
use App\Services\PaymentGateway;
test('process payment', function () {
$gateway = mock(PaymentGateway::class);
$gateway->shouldReceive('charge')
->once()
->with(100)
->andReturn(true);
$service = new PaymentService($gateway);
expect($service->process(100))->toBeTrue();
});
Start by reviewing the plugin’s src/Mocks/Mockable.php and pest.php setup in the repo—despite being archived, it remains compatible with Pest ≥1.0.
mock() for quick, inline stubs without creating dedicated mock classes.shouldReceive(), andReturn(), andThrow(), and with() in fluent chains for clarity.mock(ClassName::class, ['expensiveMethod'])) to isolate behavior.mock(), spy(), and stub() as global helpers—avoid naming conflicts by not importing them explicitly.For teams migrating from PHPUnit, replace getMockBuilder() patterns with mock(...)—simpler syntax and same expressive power for 80% of use cases.
__invoke()), pass them as strings: ->shouldReceive('__invoke').with() accepts matchers (any(), isType('int'), etc.)—avoid raw values if type coercion matters; e.g., with(isType('string')).mock() does not resolve dependencies—pass collaborators explicitly.$mock->shouldHaveReceived() before assertions fail, or expectations are lost. Consider spy() for post-hoc verification. Pest\Mixins\Mock::setVerbose(true); in pest.php to log expectations to console (useful for flaky tests).How can I help you explore Laravel packages today?