dev-master
9999999-dev https://github.com/yguedidi/global-functions-mockerA simple class that allow you mock global functions.
MIT
The Requires
mock function global
Wallogit.com
2017 © Pedro Peláez
A simple class that allow you mock global functions.
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)
Add yguedidi/global-functions-mocker to the require-dev section of your composer.json, (*3)
{
"require-dev": {
"yguedidi/global-functions-mocker": "*"
}
}
First of all, call the GlobalFunctionsMocker::setUp() method in your test setUp() method.
This is needed to clear mocked global functions between tests., (*4)
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());
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)
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());
}
}
Mock a global function. Should be called in your tests., (*8)
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)
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)
See LICENSE file, (*16)
A simple class that allow you mock global functions.
MIT
mock function global