php-mock / php-mock-mockery
Mock built-in PHP functions (e.g. time()) with Mockery. This package relies on PHP's namespace fallback policy. No further extension is needed.
Fund package maintenance!
michalbundyra
Installs: 1 326 792
Dependents: 98
Suggesters: 0
Security: 0
Stars: 38
Watchers: 3
Forks: 4
Open Issues: 1
Requires
- php: >=5.6
- mockery/mockery: ^1
- php-mock/php-mock-integration: ^2.2.1
Requires (Dev)
- phpunit/phpunit: ^4|^5|^8
README
Mock PHP built-in functions with Mockery
This package integrates the function mock library PHP-Mock with Mockery.
Installation
Use Composer:
composer require --dev php-mock/php-mock-mockery
Usage
PHPMockery::mock()
let's you build a function mock which can be equiped
with Mockery's expectations. After your test you'll have to disable all created
function mocks by calling Mockery::close()
.
Example
namespace foo; use phpmock\mockery\PHPMockery; $mock = PHPMockery::mock(__NAMESPACE__, "time")->andReturn(3); assert (3 == time()); \Mockery::close();
Restrictions
This library comes with the same restrictions as the underlying
php-mock
:
-
Only unqualified function calls in a namespace context can be mocked. E.g. a call for
time()
in the namespacefoo
is mockable, a call for\time()
is not. -
The mock has to be defined before the first call to the unqualified function in the tested class. This is documented in Bug #68541. In most cases you can ignore this restriction. But if you happen to run into this issue you can call
PHPMockery::define()
before that first call. This would define a side effectless namespaced function.
License and authors
This project is free and under the WTFPL. Responsable for this project is Markus Malkusch markus@malkusch.de.
Donations
If you like this project and feel generous donate a few Bitcoins here: 1335STSwu9hST4vcMRppEPgENMHD2r1REK