Git Product home page Git Product logo

phpunit-function-mocker's Introduction

PHPUnit function mocker extension

Allows mocking otherwise untestable PHP functions through the use of namespaces.

Build Status

<?php
namespace MyNamespace;

class Tool
{
    public function isString($string)
    {
        return strlen($string) > 0 && ctype_alpha($string);
    }
}
<?php

require_once 'PHPUnit/Extension/FunctionMocker.php';

class MyTestCase extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->php = PHPUnit_Extension_FunctionMocker::start($this, 'MyNamespace')
            ->mockFunction('strlen')
            ->mockFunction('ctype_alpha')
            ->getMock();
    }

    /** @runInSeparateProcess */
    public function testIsStringUsesStrlenAndCtypeAlpha()
    {
        $this->php
            ->expects($this->once())
            ->method('strlen')
            ->with('foo')
            ->will($this->returnValue(3))
        ;
        $this->php
            ->expects($this->once())
            ->method('ctype_alpha')
            ->with('foo')
            ->will($this->returnValue(false))
        ;

        $tool = new MyNamespace\Tool();
        $this->assertFalse($tool->isString('foo'));
    }
}

NOTE

Use @runInSeparateProcess annotation to make sure that the mocking is reliably working in PHP >=5.4

phpunit-function-mocker's People

Contributors

gitter-badger avatar lstrojny avatar slamdunk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

phpunit-function-mocker's Issues

Stale function mocks

When I use a class in which I overrid some internal PHP functions later on in my testsuite within an integration test, weird side effects happen, e. g. empty output in conjunction with caching etc.

New tag request.

Could we expect to have a new tag with the last phpunit v5.4.6 support.
Thank you.

Mocked functions are used instead of original in subsequent tests

If you mock original function in a namespace, then all subsequent tests in that namespace will try to use the mocked function instead of original one, even after calling PHPUnit_Extension_FunctionMocker::tearDown(). This is not correct in my opinion, because each test should be run in clean environment and only functions passed to PHPUnit_Extension_FunctionMocker::mockFunction() should be mocked.

PHP version bump

We use this library for our unit tests and support PHP >=5.5.9 in line with Symfony/Silex.

So we're wondering if you could tag 28a1354 as 0.3.0 so that we can still run our tests. ๐Ÿ˜„

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.