All Projects → php-mock → Php Mock Phpunit

php-mock / Php Mock Phpunit

Licence: wtfpl
Mock built-in PHP functions (e.g. time() or rand()) in PHPUnit.

Projects that are alternatives of or similar to Php Mock Phpunit

Guzzler
Supercharge your app or SDK with a testing library specifically for Guzzle
Stars: ✭ 272 (+124.79%)
Mutual labels:  mock, phpunit
phake
PHP Mocking Framework
Stars: ✭ 464 (+283.47%)
Mutual labels:  mock, phpunit
Mockery
Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succinct API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL).
Stars: ✭ 10,048 (+8204.13%)
Mutual labels:  mock, phpunit
Mock Webserver
Simple mock web server in PHP for unit testing.
Stars: ✭ 82 (-32.23%)
Mutual labels:  mock, phpunit
Interceptors
Low-level HTTP/HTTPS/XHR/fetch request interception library.
Stars: ✭ 100 (-17.36%)
Mutual labels:  mock
Impersonator
Ruby library to record and replay object interactions
Stars: ✭ 100 (-17.36%)
Mutual labels:  mock
Cuckoo
Boilerplate-free mocking framework for Swift!
Stars: ✭ 1,344 (+1010.74%)
Mutual labels:  mock
Sns
Fake Amazon SNS
Stars: ✭ 94 (-22.31%)
Mutual labels:  mock
Nsubstitute
A friendly substitute for .NET mocking libraries.
Stars: ✭ 1,646 (+1260.33%)
Mutual labels:  mock
Rabbitmq Mock
Mock for RabbitMQ Java amqp-client
Stars: ✭ 114 (-5.79%)
Mutual labels:  mock
Prig
Prig is a lightweight framework for test indirections in .NET Framework.
Stars: ✭ 106 (-12.4%)
Mutual labels:  mock
Unit Threaded
Advanced unit test framework for D
Stars: ✭ 100 (-17.36%)
Mutual labels:  mock
Really Need
Node require wrapper with options for cache busting, pre- and post-processing
Stars: ✭ 108 (-10.74%)
Mutual labels:  mock
Mockit
A tool to quickly mock out end points, setup delays and more...
Stars: ✭ 1,534 (+1167.77%)
Mutual labels:  mock
React Nativeish
React Native / React Native Web Boilerplate
Stars: ✭ 106 (-12.4%)
Mutual labels:  mock
Phpunit Arraysubset Asserts
Provides assertArraySubset for use in PHPunit
Stars: ✭ 95 (-21.49%)
Mutual labels:  phpunit
Parrot
✨ Scenario-based HTTP mocking
Stars: ✭ 109 (-9.92%)
Mutual labels:  mock
Paraunit
Run PHPUnit tests in parallel
Stars: ✭ 104 (-14.05%)
Mutual labels:  phpunit
Mimic
A mocking library for Elixir
Stars: ✭ 104 (-14.05%)
Mutual labels:  mock
Mockssh
Mock an SSH server and define all commands it supports (Python, Twisted)
Stars: ✭ 107 (-11.57%)
Mutual labels:  mock

Mock PHP built-in functions with PHPUnit

This package integrates the function mock library PHP-Mock with PHPUnit.

Installation

Use Composer:

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:

<?php

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.

Restrictions

This library comes with the same restrictions as the underlying php-mock:

  • 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.

  • 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).

License and authors

This project is free and under the WTFPL. Responsable for this project is Markus Malkusch [email protected].

Donations

If you like this project and feel generous donate a few Bitcoins here: 1335STSwu9hST4vcMRppEPgENMHD2r1REK

Build Status

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].