2017 © Pedro Peláez
 

library phpunit-easymock

Helpers to build PHPUnit mocks

image

mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  • Friday, July 27, 2018
  • by mnapoli
  • Repository
  • 1 Watchers
  • 23 Stars
  • 7,152 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 12 Versions
  • 2 % Grown

The README.md

PHPUnit EasyMock

Helpers to build PHPUnit mock objects easily., (*1)

Total Downloads, (*2)

Why?

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

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

Installation

$ composer require --dev mnapoli/phpunit-easymock

To be able to use EasyMock in your tests you must include the trait in your class:, (*5)

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

    // ...
}

Usage

Here is what a very common PHPUnit mock looks like:, (*6)

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

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

Yuck!, (*7)

Here is how to write it with EasyMock:, (*8)

$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:, (*9)

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

Features

You can mock methods so that they return values:, (*10)

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

Or so that they use a callback:, (*11)

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

You can also have methods throw exceptions by providing an Exception instance:, (*12)

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

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

$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:, (*14)

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

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

Mocks are plain PHPUnit mocks, nothing special here., (*15)

Contributing

See the CONTRIBUTING file., (*16)

License

Released under the MIT license., (*17)

The Versions

27/07 2018

dev-master

9999999-dev https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

11/11 2017

1.0.0

1.0.0.0 https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

13/06 2016

0.2.3

0.2.3.0 https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

04/06 2016

0.2.2

0.2.2.0 https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

04/01 2016

0.2.1

0.2.1.0 https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

18/10 2015

0.2.0

0.2.0.0 https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

08/03 2015

0.1.x-dev

0.1.9999999.9999999-dev https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

08/03 2015

0.1.4

0.1.4.0 https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

07/03 2015

0.1.2

0.1.2.0 https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

07/03 2015

0.1.3

0.1.3.0 https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

16/01 2015

0.1.1

0.1.1.0 https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit

11/11 2014

0.1.0

0.1.0.0 https://github.com/mnapoli/phpunit-easymock

Helpers to build PHPUnit mocks

  Sources   Download

MIT

The Requires

 

The Development Requires

mock phpunit