2017 © Pedro Peláez
 

library xpmock

PHPUnit: simple syntax to create mock-objects

image

michalv8/xpmock

PHPUnit: simple syntax to create mock-objects

  • Wednesday, January 17, 2018
  • by michalv8
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3,147 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 16 Versions
  • 21 % Grown

The README.md

xpmock

Build Status Latest Stable Version Total Downloads, (*1)

Introduction [in English] [на русском], (*2)

!!! New version 1.1.5: multiple improvements, (*3)

PHPUnit is standard testing framework in PHP world. No wonder - it is nifty. But speaking about way of mocking objects in PHPUnit many people complain on a bit redundant syntax. They suggest many addons for PHPUnit to create mocks like Mockery (I know this is not just addon)., (*4)

But I am sure PHPUnit has well-developed mocking system. And this project XPMock is a way to simplify this syntax a bit. I need to underline that XPMock doesn't create some own mock objects. XPMock just calls the same PHPUnit methods creating the same native mocks but much simpler., (*5)

Write this

$this->mock('MyClass')
    ->getBool(true)
    ->getNumber(1)
    ->getString('string')
    ->new();

instead of that

$mock = $this->getMockBuilder('MyClass')
    ->setMethods(['getBool', 'getNumber', 'getString'])
    ->disableOriginalConstructor()
    ->getMock();
$mock->expects($this->any())
    ->method('getBool')
    ->will($this->returnValue(true));
$mock->expects($this->any())
    ->method('getNumber')
    ->will($this->returnValue(1));
$mock->expects($this->any())
    ->method('getString')
    ->will($this->returnValue('string'));

Tool generates full-functional native PHPUnit mocks., (*6)

Syntax short description


// init mock writer $this->mock('MyClass') // init mock (all methods are real by default) // mock methods // $mock->expects($this->any())->method('getNumber')->will($this->returnValue(null)) ->getNumber() // $mock->expects($this->any())->method('getNumber')->will($this->returnValue(1)) ->getNumber(1) // $mock->expects($this->any())->method('getNumber')->will($this->returnValue(1)) ->getNumber($this->returnValue(1)) // $mock->expects($this->once())->method('getNumber')->will($this->returnValue(null)) ->getNumber($this->once()) // $mock->expects($this->once())->method('getNumber')->will($this->returnValue(1)) ->getNumber(1, $this->once()) // $mock->expects($this->at(0))->method('getNumber')->will($this->returnValue(1)) // $mock->expects($this->at(1))->method('getNumber')->will($this->returnValue(2)) ->getNumber(1, $this->at(0)) ->getNumber(2, $this->at(1)) // $mock->expects($this->once())->method('getNumber')->with(1,2,3)->will($this->returnValue(null)) ->getNumber([1,2,3], $this->once()) // $mock->expects($this->any())->method('getNumber')->with(1,2,3)->will($this->returnValue(1)) ->getNumber([1,2,3], 1) // $mock->expects($this->once())->method('getNumber')->with(1,2,3)->will($this->returnValue(1)) ->getNumber([1,2,3], 1, $this->once()) // $mock->expects($this->any())->method('getNumber')->will($this->returnCallback(function(){})) ->getNumber(function(){}) // $mock->expects($this->any())->method('getNumber')->will($this->throwException(new \Exception(''))) ->getNumber(new \Exception('')) // create mock // $this->getMockBuilder('MyClass')->disableOriginalConstructor()->getMock() ->new() // $this->getMockBuilder('MyClass')->setConstructorArgs([1,2,3])->getMock() ->new(1, 2, 3)

Handy reflection methods


// get value of any property: static/non-static, public/protected/private $value = $this->reflect('MyClass')->property; // class name (only static) $value = $this->reflect(new MyClass())->property; // object // set value of any property: static/non-static, public/protected/private property $this->reflect('MyClass')->property = $value; // class name (only static) $this->reflect(new MyClass())->property = $value; // object $this->reflect(new MyClass()) ->__set('property1', $value1) ->__set('property2', $value2); // chain // call any method: static/non-static, public/protected/private $this->reflect('MyClass')->method($arg); // class name (only static) $this->reflect(new MyClass())->method($arg); // object

Installation

  1. If you don't have composer, install it
  2. Add xpmock to your project
composer require ptrofimov/xpmock:dev-master

Usage

Option 1. Add trait to existing test case:, (*7)

class MyTestCase extends \PHPUnit_Framework_TestCase
{
    use \Xpmock\TestCaseTrait;
}

OR Option 2. Extend your test case from xpmock's one:, (*8)

class MyTestCase extends \Xpmock\TestCase
{

}

NEW

  1. If you need to create object with some methods and classname doesn't matter, you could easily do it like this:
$mock = $this->mock()
    ->getString('string')
    ->getNumber(function () {
        return 2 + 2;
    })
    ->new();
  1. Mocking static methods
  2. Mocking abstract methods
  3. Brief syntax to create mocks
$mock = $this->mock('MyClass', ['getNumber' => 1]);
  1. Special method this() inside each mock gives you possibility to change non-public properties and call non-public methods of mock via Xpmock\Reflection
$mock = $this->mock('MyClass')->new();
$mock->this()->protectedProperty = 'value';
$mock->this()->protectedMethod();
  1. You can use $this pointer inside your fake methods
$mock = $this->mock('MyClass',
    [
        'property' => 1,
        'getProperty' => function () {
            return $this->property;
        },
    ]
);
  1. It is easy now to create mocks all methods of which return some value, for example null (stub)
$mock = $this->mock('MyClass', null);
  1. Very expected: now it is possible to adjust mocks after creation with magic method mock()
$myClass = $this->mock('MyClass', null);
$myClass->getNumber(); // null
$myClass->mock()
    ->getNumber(1);
$myClass->getNumber(); // 1

The Versions

17/01 2018

dev-phpunit-update

dev-phpunit-update

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

27/02 2017

dev-master

9999999-dev

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

27/02 2017

1.2.1

1.2.1.0

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

27/02 2017

1.2.0

1.2.0.0

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

06/02 2017

dev-phpunit-6

dev-phpunit-6

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

02/01 2014

1.1.5

1.1.5.0

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

02/01 2014

1.1.4

1.1.4.0

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

28/10 2013

1.1.3

1.1.3.0

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

18/10 2013

1.1.2

1.1.2.0

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

18/10 2013

1.1.1

1.1.1.0

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

16/10 2013

1.1.0

1.1.0.0

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

11/10 2013

1.0.4

1.0.4.0

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

21/08 2013

1.0.3

1.0.3.0

PHPUnit: simple syntax to create mock-objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

18/06 2013

1.0.2

1.0.2.0

PHPUnit mocks: simple syntax

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

17/06 2013

1.0.1

1.0.1.0

PHPUnit mocks: simple syntax

  Sources   Download

The Requires

  • php >=5.3.0

 

11/06 2013

1.0.0

1.0.0.0

PHPUnit mocks: simple syntax

  Sources   Download

The Requires

  • php >=5.3.0