2017 © Pedro Peláez
 

library mocker

Mocking helper for PHPUnit

image

mvkasatkin/mocker

Mocking helper for PHPUnit

  • Wednesday, February 7, 2018
  • by mvkasatkin
  • Repository
  • 1 Watchers
  • 0 Stars
  • 607 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 1 % Grown

The README.md

Mocker

Build Status Coverage Status Latest Stable Version License, (*1)

Helper for convenient work with PHPUnit mocks.
It uses the library phpunit/phpunit-mock-objects supplied with PHPUnit., (*2)

Install

composer require mvkasatkin/mocker

Initialize

    public function setUp()
    {
        parent::setUp();
        Mocker::init($this);
    }

Simple test-double

Works with regular classes, abstract classes and interfaces., (*3)

        $mock = Mocker::create(SomeClass::class);
        $this->assertInstanceOf(SomeClass::class, $mock);

Test-double with methods

        $mock = Mocker::create(SomeClass::class, [
            Mocker::method('firstMethod')->returns(true),
            Mocker::method('secondMethod', 1)->returns(true),
            Mocker::method('thirdMethod', 1, [$param1, $param2])->returns($value),
            Mocker::method('fourthMethod', 1)->with([$param1, $param2])->returns($value),
        ]);

In this example:, (*4)

$mock->firstMethod - can be called any number of times with any parameters and returns true
$mock->secondMethod - must be called once and returns true
$mock->thirdMethod - must be called once with the parameters $param1, $param2 and returns $value
$mock->fourthMethod - must be called once with the parameters $param1, $param2 and returns $value
, (*5)

Test-double with/return map:

        /** @var SomeClass $mock */
        $mock = Mocker::create(SomeClass::class, [
            Mocker::method('checkMap', 5)->returnsMap([
                ['arg1', 'arg2', 'arg3', 'A'],
                ['arg1', 'arg2', null, 'B'],
                ['arg1', null, null, 'C'],
                [null, null, null, 'D'],
            ]),
        ]);
        $this->assertEquals('A', $mock->checkMap('arg1', 'arg2', 'arg3'));
        $this->assertEquals('B', $mock->checkMap('arg1', 'arg2'));
        $this->assertEquals('C', $mock->checkMap('arg1'));
        $this->assertEquals('D', $mock->checkMap());
        $this->assertEquals(null, $mock->checkMap('a', 'b', 'c'));

Mocking of private methods is impossible.
Mocking of protected methods is possible, but it's not a «best practice».
, (*6)

Test-double with constructor call

        $mock = Mocker::create(SomeClass::class, [...], [$arg1, $arg2]);
        $this->assertInstanceOf(SomeClass::class, $mock);

Constructor of the SomeClass will be invoked with args: $arg1, $arg2, (*7)

Protected properties and methods

Despite the fact that testing internal implementation of classes is not the best practice, sometimes it is still necessary to set or verify a protected property, or to call a protected method., (*8)

It works both with protected properties and methods, and with private:, (*9)

        $o = new SomeClass();
        Mocker::setProperty($o, 'protectedProperty', 'a');
        Mocker::setProperty($o, 'privateProperty', 'b');
        $this->assertEquals('a', Mocker::getProperty($o, 'protectedProperty'));
        $this->assertEquals('b', Mocker::getProperty($o, 'privateProperty'));
        $this->assertEquals('aZ', Mocker::invoke($o, 'privateMethod', ['a']));

The Versions

07/02 2018

dev-master

9999999-dev

Mocking helper for PHPUnit

  Sources   Download

MIT

The Requires

 

by Maxim Kasatkin

mock stub phpunit mocking

07/02 2018

1.0.9

1.0.9.0

Mocking helper for PHPUnit

  Sources   Download

MIT

The Requires

 

by Maxim Kasatkin

mock stub phpunit mocking

07/02 2018

1.0.8

1.0.8.0

Mocking helper for PHPUnit

  Sources   Download

MIT

The Requires

 

by Maxim Kasatkin

mock stub phpunit mocking

02/12 2017

1.0.7

1.0.7.0

Mocking helper for PHPUnit

  Sources   Download

MIT

The Requires

 

by Maxim Kasatkin

mock stub phpunit mocking

02/10 2017

1.0.6

1.0.6.0

Mocking helper for PHPUnit

  Sources   Download

MIT

The Requires

 

by Maxim Kasatkin

mock stub phpunit mocking

21/09 2017

1.0.5

1.0.5.0

Mocking helper for PHPUnit

  Sources   Download

MIT

The Requires

 

by Maxim Kasatkin

mock stub phpunit mocking

20/09 2017

1.0.4

1.0.4.0

Mocking helper for PHPUnit

  Sources   Download

MIT

The Requires

 

by Maxim Kasatkin

mock stub phpunit mocking

20/09 2017

1.0.2

1.0.2.0

Mocking helper for PHPUnit

  Sources   Download

MIT

The Requires

 

by Maxim Kasatkin

mock stub phpunit mocking

20/09 2017

1.0.1

1.0.1.0

Mocking helper for PHPUnit

  Sources   Download

MIT

The Requires

 

by Maxim Kasatkin

mock stub phpunit mocking

20/09 2017

1.0.0

1.0.0.0

Mocking helper for PHPUnit

  Sources   Download

MIT

The Requires

 

by Maxim Kasatkin

mock stub phpunit mocking