, (*1)
About
ExtraMocks are a tools that give extra functionality for Mocks., (*2)
Main features
- Allow to redefine global function in namespaces.
Usage
Redefine global function., (*3)
Method Pameters
- string $fullName - name with namespace of function for redefine
- mixed|callable $result - new function or result
- int|null $count, default = null. Count of mocked calls
Get count of mocked calls, (*4)
Method Pameters
- string $fullName - fullname of redefined function
Examples
namespace A;
class A
{
public static function string_length($str)
{
return strlen($str);
}
}
namespace B;
class B {
public static function string_length($str)
{
return strlen($str);
}
}
namespace Example;
require (__DIR__ . '/../src/autoloader.php');
use ExtraMocks\Mocks;
// 1. Redefine Global Function by Function
\ExtraMocks\Mocks::mockGlobalFunction(
'\A\strlen',
function($s) {
return strlen($s) * 5;
}
);
echo \A\A::string_length('foo') . PHP_EOL; // 15
echo \B\B::string_length('foo') . PHP_EOL; // 3;
// 2. Redefine Global Function by Result
\ExtraMocks\Mocks::mockGlobalFunction('\A\strlen', 42);
echo \A\A::string_length('foo') . PHP_EOL; // 42;
echo \B\B::string_length('foo') . PHP_EOL; // 3;
// 3. Redefine Global Function by Result once
\ExtraMocks\Mocks::mockGlobalFunction('\A\strlen', 42, 1);
echo \A\A::string_length('foo') . PHP_EOL; // 42;
echo \A\A::string_length('foo') . PHP_EOL; // 3;
echo \B\B::string_length('foo') . PHP_EOL; // 3;
// 3. Get count of calls mocked function
\ExtraMocks\Mocks::mockGlobalFunction('\A\strlen', 42);
echo Mocks::getCountCalls('\A\strlen') . PHP_EOL; // 0
echo \A\A::string_length('foo') . PHP_EOL; // 42;
echo Mocks::getCountCalls('\A\strlen') . PHP_EOL; // 1
echo \A\A::string_length('foo') . PHP_EOL; // 42;
echo Mocks::getCountCalls('\A\strlen') . PHP_EOL; // 2
Installation
Composer
Download composer:, (*5)
wget -nc http://getcomposer.org/composer.phar
and add dependency to your project:, (*6)
php composer.phar require cheprasov/php-extra-mocks
Running tests
-
To run tests type in console:, (*7)
./vendor/bin/phpunit, (*8)
Something doesn't work
Feel free to fork project, fix bugs and finally request for pull, (*9)