Skip to content
Allow mocking otherwise unmockable functions with PHPUnit
PHP
Find file
Latest commit 28a1354 Jul 30, 2015 @lstrojny Migrate to new infra
Failed to load latest commit information.
build Build infra Apr 1, 2013
src/PHPUnit/Extension
tests/PHPUnitTests/Extension Making method static Feb 11, 2013
.gitignore
.travis.yml Migrate to new infra Jul 30, 2015
README.md Update README.md Jan 12, 2015
build.xml Build infra Apr 1, 2013
composer.json Adding PHP 5.3 as a dep Nov 8, 2012
phpunit.xml.dist

README.md

PHPUnit function mocker extension

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

Gitter Build Status Dependency Status Average time to resolve an issue Percentage of issues still open

<?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

Something went wrong with that request. Please try again.