2017 © Pedro Peláez
 

library global-functions-mocker

A simple class that allow you mock global functions.

image

yguedidi/global-functions-mocker

A simple class that allow you mock global functions.

  • Wednesday, September 17, 2014
  • by yguedidi
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Global Functions Mocker

A simple class that allow you mock global functions for your unit tests., (*1)

Note: This works only if you don't use absolute function calls (like \phpversion()) in your classes., (*2)

Installation

Add yguedidi/global-functions-mocker to the require-dev section of your composer.json, (*3)

{
    "require-dev": {
        "yguedidi/global-functions-mocker": "*"
    }
}

Basic usage

0 - Prerequisites

1 - Setup

First of all, call the GlobalFunctionsMocker::setUp() method in your test setUp() method.
This is needed to clear mocked global functions between tests., (*4)

2 - Namespaced function

Create a function named exactly like the global function in the namespace of your tested class, but define it in your test file.
The body of that function should be:, (*5)

return GlobalFunctionsMocker::execute('global_function_name', func_get_args());

3 - Mock the function

Then just mock the global function you need in your tests with GlobalFunctionsMocker::mock('global_function_name').
It returns a subset of Mockery\Expectation so you can define some expectations: MockedFunction, (*6)

Full exemple

This exemple assume your tested class and your unit test class are in different namespace., (*7)

// src/FooClass.php
namespace Acme\FooClass;

class FooClass {
    public function foo()
    {
        return phpversion();
    }
}
// tests/FooClassTest.php

// Note the different namespace
namespace Acme\Tests\FooClass
{
    use Acme\FooClass;
    use YassineGuedidi\GlobalFunctionsMocker\GlobalFunctionsMocker as GFM;

    class FooClassTest extends \PHPUnit_Framework_TestCase
    {
        protected function setUp()
        {
            GFM::setUp()
        }

        public function testFoo()
        {
            GFM::mock('phpversion')->once()->withNoArgs()->andReturn('foo');
            $object = new FooClass();
            $this->assertEquals('foo', $object->foo());
        }
    }
}

// IMPORTANT: Define the function in the tested class namespace
namespace Acme\FooClass
{
    use YassineGuedidi\GlobalFunctionsMocker\GlobalFunctionsMocker as GFM;

    function phpversion()
    {
        // Don't forget 'func_get_args()' for fallback to the real global function work
        return GFM::execute('phpversion', func_get_args());
    }
}

Reference

GlobalFunctionsMocker::mock($functionName)

Mock a global function. Should be called in your tests., (*8)

  • $functionName
    Name of the global function to mock

GlobalFunctionsMocker::execute($functionName, $args)

Execute a mocked global function. Call this in your namespaced global function definition., (*9)

  • $functionName
    Name of the global function to execute, (*10)

  • $args (optional)
    Arguments passed to the global function (empty array by default).
    Important: This should be func_get_args() in the namespaced function. It allow fallback to the real global if it's not mocked., (*11)

It will fallback to the real global function if the function isn't mocked by a test, or if you mock it with some expectations but without a return value., (*12)

And if the real global function is not define (missing PHP extension for exemple), it will gracefully throw a PHPUnit_Framework_SkippedTestError exception that will skip the current test with a useful message., (*13)

MockedFunction

Available functions: with(), withArgs(), withNoArgs(), withAnyArgs(), andReturn(), andReturnValues(), andReturnUsing() andReturnUndefined(), andReturnNull(), andThrow(),andThrowExceptions(), zeroOrMoreTimes(), times(), never(), once(), twice(), atLeast(), atMost(), between()., (*14)

Look at Mockery's documentation about Expectation Declarations and Argument Validation for more detail., (*15)

License

See LICENSE file, (*16)

The Versions

17/09 2014

dev-master

9999999-dev https://github.com/yguedidi/global-functions-mocker

A simple class that allow you mock global functions.

  Sources   Download

MIT

The Requires

 

mock function global