Get support for mnapoli/phpunit-easymock

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

mnapoli/phpunit-easymock

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.

Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2