2017 © Pedro Peláez
 

library php-mock

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

image

php-mock/php-mock

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

  • Saturday, December 2, 2017
  • by malkusch
  • Repository
  • 7 Watchers
  • 193 Stars
  • 754,997 Installations
  • PHP
  • 48 Dependents
  • 0 Suggesters
  • 9 Forks
  • 2 Open issues
  • 14 Versions
  • 12 % Grown

The README.md

PHP-Mock: mocking built-in PHP functions

PHP-Mock is a testing library which mocks non deterministic built-in PHP functions like time() or rand(). This is achieved by PHP's namespace fallback policy:, (*1)

PHP will fall back to global functions […] if a namespaced function […] does not exist., (*2)

PHP-Mock uses that feature by providing the namespaced function. I.e. you have to be in a non global namespace context and call the function unqualified:, (*3)

namespace foo;

$time = time(); // This call can be mocked, a call to \time() can't.

Requirements and restrictions

  • 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., (*4)

  • 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 Mock::define() before that first call. This would define a side effectless namespaced function which can be enabled later. Another effective approach is running your test in an isolated process., (*5)

Alternatives

If you can't rely on or just don't want to use the namespace fallback policy, there are alternative techniques to mock built-in PHP functions:, (*6)

  • PHPBuiltinMock relies on the APD extension., (*7)

  • MockFunction is a PHPUnit extension. It uses the runkit extension., (*8)

  • UOPZ is a Zend extension which allows, among others, renaming and deletion of functions., (*9)

  • vfsStream is a stream wrapper for a virtual file system. This will help you write tests which covers PHP stream functions (e.g. fread() or readdir())., (*10)

Installation

Use Composer:, (*11)

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

Usage

You don't need to learn yet another API. PHP-Mock has integrations for these testing frameworks:, (*12)

Note: If you plan to use one of the above mentioned testing frameworks you can skip reading any further and just go to the particular integration project., (*16)

PHP-Mock API

You find the API in the namespace phpmock., (*17)

Create a Mock object. You can do this with the fluent API of MockBuilder:, (*18)

After you have build your Mock object you have to call enable() to enable the mock in the given namespace. When you are finished with that mock you should disable it by calling disable() on the mock instance., (*29)

This example illustrates mocking of the unqualified function time() in the namespace foo:, (*30)

namespace foo;

use phpmock\MockBuilder;

$builder = new MockBuilder();
$builder->setNamespace(__NAMESPACE__)
        ->setName("time")
        ->setFunction(
            function () {
                return 1417011228;
            }
        );

$mock = $builder->build();

// The mock is not enabled yet.
assert (time() != 1417011228);

$mock->enable();
assert (time() == 1417011228);

// The mock is disabled and PHP's built-in time() is called.
$mock->disable();
assert (time() != 1417011228);

Instead of setting the mock function with MockBuilder::setFunction() you could also use the existing FixedValueFunction:, (*31)

namespace foo;

use phpmock\MockBuilder;
use phpmock\functions\FixedValueFunction;

$builder = new MockBuilder();
$builder->setNamespace(__NAMESPACE__)
        ->setName("time")
        ->setFunctionProvider(new FixedValueFunction(1417011228));

$mock = $builder->build();

It's important to note that setNamespace() should target the namespace where the function is called, not the namespace where it's being mocked. For example:, (*32)

<?php

namespace App\Code;

class Subject
{
  public function foo()
  {
    time();
  }
}

In a test mocking this call:, (*33)

<?php

namespace Tests\Unit;

class SubjectTest
{
  public function myTest()
  {
    $builder = new MockBuilder();
    $builder->setNamespace('\\App\\Code'); // ... etc
  }
}

Reset global state

An enabled mock changes global state. This will break subsequent tests if they run code which would call the mock unintentionally. Therefore you should always disable a mock after the test case. You will have to disable the created mock. You could do this for all mocks by calling the static method Mock::disableAll()., (*34)

Mock environments

Complex mock environments of several mocked functions can be grouped in a MockEnvironment:, (*35)

SleepEnvironmentBuilder

The SleepEnvironmentBuilder builds a mock environment where sleep() and usleep() return immediatly. Furthermore they increase the amount of time in the mocked date(), time() and microtime():, (*39)

namespace foo;

use phpmock\environment\SleepEnvironmentBuilder;

$builder = new SleepEnvironmentBuilder();
$builder->addNamespace(__NAMESPACE__)
        ->setTimestamp(1417011228);

$environment = $builder->build();
$environment->enable();

// This won't delay the test for 10 seconds, but increase time().        
sleep(10);

assert(1417011228 + 10 == time());

If the mocked functions should be in different namespaces you can add more namespaces with SleepEnvironmentBuilder::addNamespace(), (*40)

Spies

A Spy gives you access to the function invocations. Spy::getInvocations() gives you access to the arguments and return value., (*41)

As a Spy is a specialization of Mock it behaves identically. However you could ommit the third constructor parameter callable $function which would then create a spy using the existing function. E.g. a new Spy(__NAMESPACE__ , "rand") would create a spy which basically proxies PHP's built-in rand():, (*42)

namespace foo;

use phpmock\spy\Spy;

function bar($min, $max) {
    return rand($min, $max) + 3;
}

$spy = new Spy(__NAMESPACE__, "rand");
$spy->enable();

$result = bar(1, 2);

assert ([1, 2]  == $spy->getInvocations()[0]->getArguments());
assert ($result == $spy->getInvocations()[0]->getReturn() + 3);

License and authors

This project is free and under the WTFPL. Responsable for this project is Markus Malkusch markus@malkusch.de. This library was inspired by Fabian Schmengler's article PHP: “Mocking” built-in functions like time() in Unit Tests., (*43)

Donations

If you like PHP-Mock and feel generous donate a few Bitcoins here: 1335STSwu9hST4vcMRppEPgENMHD2r1REK, (*44)

The Versions

02/12 2017

dev-master

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

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock 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

17/02 2017

2.0.0

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

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock 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

11/11 2015

1.0.1

1.0.1.0 https://github.com/php-mock/php-mock

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock 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

26/10 2015

1.0.0

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

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock 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

14/04 2015

0.1.1

0.1.1.0 https://github.com/malkusch/php-mock

Mock non deterministic built-in PHP functions (e.g. time() or rand())

  Sources   Download

WTFPL-2.0

The Requires

  • php >=5.3

 

The Development Requires

test mock stub function

13/04 2015

0.8

0.8.0.0 https://github.com/php-mock/php-mock

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock 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

02/04 2015

0.7

0.7.0.0 https://github.com/php-mock/php-mock

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock 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

02/04 2015

0.6

0.6.0.0 https://github.com/php-mock/php-mock

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock 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

20/03 2015

0.5

0.5.0.0 https://github.com/malkusch/php-mock

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

  Sources   Download

WTFPL-2.0

The Requires

  • php >=5.4

 

The Development Requires

bdd tdd test mock stub test double function

26/01 2015

0.4.1

0.4.1.0 https://github.com/malkusch/php-mock

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

  Sources   Download

WTFPL-2.0

The Requires

  • php >=5.4

 

The Development Requires

bdd tdd test mock stub test double function

23/01 2015

0.4

0.4.0.0 https://github.com/malkusch/php-mock

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

  Sources   Download

WTFPL-2.0

The Requires

  • php >=5.4

 

The Development Requires

bdd tdd test mock stub test double function

02/12 2014

0.3

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

Mock non deterministic built-in PHP functions (e.g. time() or rand())

  Sources   Download

WTFPL-2.0

The Requires

  • php >=5.4

 

The Development Requires

test mock stub function

30/11 2014

0.2

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

Mock non deterministic built-in PHP functions (e.g. time() or rand())

  Sources   Download

WTFPL-2.0

The Requires

  • php >=5.4

 

The Development Requires

test mock stub function

26/11 2014

0.1

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

Mocks for php functions.

  Sources   Download

WTFPL-2.0

The Requires

  • php >=5.3

 

The Development Requires

test mock