Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Phpunit Easymock Laravel Package

mnapoli/phpunit-easymock

A lightweight bridge between PHPUnit and EasyMock for PHP. Quickly create, replay, and verify mocks from your test cases with minimal boilerplate, helping you write clear unit tests using the familiar EasyMock-style workflow.

View on GitHub
Deep Wiki
Context7

PHPUnit EasyMock

Helpers to build PHPUnit mock objects easily.

Total Downloads

Why?

This library is not a mocking library. It's just a few helpers to write the most common mocks more easily.

It doesn't reinvent anything and is not intended to cover every use case: only the most common ones.

Installation

$ composer require --dev mnapoli/phpunit-easymock

To be able to use EasyMock in your tests you must include the trait in your class:

class MyTest extends \PHPUnit\Framework\TestCase
{
    use \EasyMock\EasyMock;

    // ...
}

Usage

Here is what a very common PHPUnit mock looks like:

$mock = $this->createMock('My\Class');

$mock->expect($this->any())
    ->method('sayHello')
    ->willReturn('Hello');

Yuck!

Here is how to write it with EasyMock:

$mock = $this->easyMock('My\Class', [
    'sayHello' => 'Hello',
]);

What if you want to assert that the method is called once (i.e. $mock->expect($this->once()))? Use spy() instead:

$mock = $this->easySpy('My\Class', [
    'sayHello' => 'Hello',
]);

Features

You can mock methods so that they return values:

$mock = $this->easyMock('My\Class', [
    'sayHello' => 'Hello',
]);

Or so that they use a callback:

$mock = $this->easyMock('My\Class', [
    'sayHello' => function ($name) {
        return 'Hello ' . $name;
    },
]);

You can also have methods throw exceptions by providing an Exception instance:

$mock = $this->easyMock('My\Class', [
    'sayHello' => new \RuntimeException('Whoops'),
]);

It is possible to call the mock() method again on an existing mock:

$mock = $this->easyMock('My\Class');

$mock = $this->easyMock($mock, [
    'sayHello' => 'Hello',
]);

What if?

If you want to use assertions or other PHPUnit features, just do it:

$mock = $this->easyMock('My\Class', [
    'sayHello' => 'hello',
]);

$mock->expects($this->once())
    ->method('sayGoodbye')
    ->willReturn('Goodbye');

Mocks are plain PHPUnit mocks, nothing special here.

Contributing

See the CONTRIBUTING file.

License

Released under the MIT license.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport