2017 © Pedro Peláez
 

library php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

image

php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  • Monday, June 4, 2018
  • by malkusch
  • Repository
  • 2 Watchers
  • 71 Stars
  • 340,713 Installations
  • PHP
  • 146 Dependents
  • 1 Suggesters
  • 9 Forks
  • 3 Open issues
  • 16 Versions
  • 12 % Grown

The README.md

.github/workflows/tests.yml, (*1)

Mock PHP built-in functions with PHPUnit

This package integrates the function mock library PHP-Mock with PHPUnit., (*2)

Installation

Use Composer:, (*3)

composer require --dev php-mock/php-mock-phpunit

Usage

PHP-Mock integrates with the trait PHPMock into your PHPUnit test case. This trait extends the framework by the method getFunctionMock(). With this method you can build a mock in the way you are used to build a PHPUnit mock:, (*4)

namespace foo;

class BuiltinTest extends \PHPUnit\Framework\TestCase
{

    use \phpmock\phpunit\PHPMock;

    public function testTime()
    {
        $time = $this->getFunctionMock(__NAMESPACE__, "time");
        $time->expects($this->once())->willReturn(3);

        $this->assertEquals(3, time());
    }

    public function testExec()
    {
        $exec = $this->getFunctionMock(__NAMESPACE__, "exec");
        $exec->expects($this->once())->willReturnCallback(
            function ($command, &$output, &$return_var) {
                $this->assertEquals("foo", $command);
                $output = ["failure"];
                $return_var = 1;
            }
        );

        exec("foo", $output, $return_var);
        $this->assertEquals(["failure"], $output);
        $this->assertEquals(1, $return_var);
    }
}

There's no need to disable the mocked function. The PHPUnit integration does that for you., (*5)

Restrictions

This library comes with the same restrictions as the underlying php-mock:, (*6)

  • Only unqualified function calls in a namespace context can be mocked. E.g. a call for time() in the namespace foo is mockable, a call for \time() is not., (*7)

  • The mock has to be defined before the first call to the unqualified function in the tested class. This is documented in Bug #68541. In most cases you can ignore this restriction. But if you happen to run into this issue you can call PHPMock::defineFunctionMock() before that first call (e.g. with @beforeClass). This would define a side effectless namespaced function. Another effective approach is running your test in an isolated process (e.g. with @runInSeparateProcess)., (*8)

License and authors

This project is free and under the WTFPL. Responsable for this project is Markus Malkusch markus@malkusch.de., (*9)

Donations

If you like this project and feel generous donate a few Bitcoins here: 1335STSwu9hST4vcMRppEPgENMHD2r1REK, (*10)

The Versions

04/06 2018

dev-master

9999999-dev https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

bdd tdd test mock stub test double function phpunit

06/04 2018

2.1.1

2.1.1.0 https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

bdd tdd test mock stub test double function phpunit

23/03 2018

2.1.0

2.1.0.0 https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

bdd tdd test mock stub test double function phpunit

02/12 2017

2.0.1

2.0.1.0 https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

bdd tdd test mock stub test double function phpunit

17/02 2017

2.0.0

2.0.0.0 https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

bdd tdd test mock stub test double function phpunit

06/12 2016

1.1.x-dev

1.1.9999999.9999999-dev https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

bdd tdd test mock stub test double function phpunit

12/08 2016

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

The Development Requires

bdd tdd test mock stub test double function phpunit

12/08 2016

0.3.x-dev

0.3.9999999.9999999-dev https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

The Development Requires

bdd tdd test mock stub test double function phpunit

16/06 2016

1.1.2

1.1.2.0 https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

bdd tdd test mock stub test double function phpunit

10/06 2016

dev-useful-message

dev-useful-message https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

bdd tdd test mock stub test double function phpunit

05/11 2015

1.1.1

1.1.1.0 https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

bdd tdd test mock stub test double function phpunit

05/11 2015

1.1.0

1.1.0.0 https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

bdd tdd test mock stub test double function phpunit

27/10 2015

1.0.0

1.0.0.0 https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

The Development Requires

bdd tdd test mock stub test double function phpunit

13/04 2015

0.3

0.3.0.0 https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

The Development Requires

bdd tdd test mock stub test double function phpunit

02/04 2015

0.2

0.2.0.0 https://github.com/php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

The Development Requires

bdd tdd test mock stub test double function phpunit

02/04 2015

0.1

0.1.0.0 https://github.com/php-mock/phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

  Sources   Download

WTFPL

The Requires

 

The Development Requires

bdd tdd test mock stub test double function phpunit