Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to mock global functions #180

Open
aenglander opened this issue May 13, 2015 · 1 comment
Open

Add ability to mock global functions #180

aenglander opened this issue May 13, 2015 · 1 comment

Comments

@aenglander
Copy link

When writing plugins/modules for legacy platforms like WordPress, Drupal < 8, or OS Commerce, it would be nice to be able to test the code. Unfortunately, they all use global functions and specify that if you want to test your code, write some sort of facade in front of the globals to mock. It would be much nicer to be able just mock the functions themselves as long as they do not exist. An example might be:

$get_user_metadata_mock = Phake::mockFunction('get_user_metadata');
Phake::when($get_user_metadata_mock)->call(Phake::anything())->thenReturn(array());

get_user_metadata(12345);

Phake::verify($get_user_metadata_mock)->call(12345);
@mollierobbert
Copy link

If your code is namespaced, mocking out function calls is quite easy with standard PHPUnit by redefining the functions inside the namespace.

See also: http://marcelog.github.io/articles/php_mock_global_functions_for_unit_tests_with_phpunit.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants