Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Allow mocking otherwise unmockable functions with PHPUnit

branch: master

Dep status

latest commit c331c93cbd
Lars Strojny authored April 11, 2014
Octocat-spinner-32 build Build infra April 01, 2013
Octocat-spinner-32 src Dep status April 11, 2014
Octocat-spinner-32 tests Making method static February 11, 2013
Octocat-spinner-32 .gitignore Build infra April 01, 2013
Octocat-spinner-32 .travis.yml Travis config January 20, 2014
Octocat-spinner-32 README.md Dep status April 11, 2014
Octocat-spinner-32 build.xml Build infra April 01, 2013
Octocat-spinner-32 composer.json Adding PHP 5.3 as a dep November 08, 2012
Octocat-spinner-32 phpunit.xml.dist Completing blacklisting April 01, 2013
README.md

PHPUnit function mocker extension

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

Build Status Dependency 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();
    }

    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'));
    }
}
Something went wrong with that request. Please try again.