-
-
Notifications
You must be signed in to change notification settings - Fork 19
PHPMock::defineFunctionMock not working? #12
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
Comments
If i try an call the mocked function inside the test it works. I've tried requiring files prior to the test running and it seems to mock it as well. namespace fred;
some_function_which_executes_builtin();
include "some_include_in_namespace_fred_which overrides_same_builtin.php";
some_function_which_executes_builtin(); Seems to have a wider scope functions / class constants etc. |
Sure, use a debugger and have a look into which function it jumps.
Any code which uses the function previously in the same namespace. PHP is caching namespace resolution. If you're running a test suite and any test before is using said function unmocked, you're out of luck within the same process. You can confirm that by running your tests under HHVM. They might pass there, as HHVM doesn't have this bug.
|
I've tried the annotation and the problem still persists, this makes me think that some of the imports have side-effects and call the method in question before the tests even run :|. Unfortunately using HHVM for testing isn't optimal as production code does not run on it. Edit: I've commented out all files except the one containing the sources for the methods in question and still the issue is present. Even went further and changed the bootstrap file to a bare minimum and still nothing. |
Could you provide a SSCCE so that I could reproduce it? |
It is currently using the mockery extension but the issue is the same with the simple I ran it with php 5.6.29. |
Added some more tests that fail in some way or the other (may be related or completely new issues). Only the first test case works, the rest fail. Some even die and stop the others from executing which is even weirder. |
@krodyrobi is it still an issue? |
php 7.2.1 "phpunit/phpunit": "5.7.27",
|
@krodyrobi So... It is possible to overload only methods which are called in the namespace and these can't be defined in that namespace. So I've analized first failing test: function test_minify_page_minification() {
$true_constant = PHPMockery::mock(__NAMESPACE__, 'true_constant');
$true_constant->once()
->with('PL_MINIFY_HTML')
->andReturn(true);
$input = 'random';
$minify_html = PHPMockery::mock(__NAMESPACE__, 'minify_html');
$minify_html->once()
->with($input)->andReturn('mini');
$actual = pl_minify_page($input);
$this->assertSame('mini', $actual);
} so you try to mock Exactly the same in case So as the real issue I can consider only error in test |
@krodyrobi Ok, I think I have solution for mockery: php-mock-phpunit is fine, similar issue we have in php-mock-prophecy, but there is already PR with the solution: php-mock/php-mock-prophecy#6 |
That seems to be it, is there a composer version with which I can check? So for the first part, if I got it right, because php has caching on the defined functions I need to call the mocked function from a test namespace != than the one it was defined in, but when mocking I still need to target definitions namespace. Will check this out later and see what pops up. |
So the branch in the pr works those tests pass. But i still haven't got a clue how to mock the other functions :D. |
What other function? What you try to do? As I said, you can't overload function defined in the namespace, and you can't mock function called with context without namespace. The purpose of the library is to allow mocking built-in PHP functions via overloading them in the namespace where these are called. |
I'm trying to mock a function that is not builtin, and I guess is out of scope. |
So I've read about the namespace bug and applied the recommended fix but it doesn't seem to work in this instance.
It did in another case but not here, even tried to run solo / in a different process but to no avail.
Added all the functions to the defineFunctionMock just to be safe even though most are not needed.
I've ran out of ideas, any thoughts on why this might not work?
The text was updated successfully, but these errors were encountered: