Description
Q | A |
---|---|
PHPUnit version | 9.6.20 / 10.5.29 / 11.3.0 |
PHP version | 8.3.4 |
Installation Method | Composer |
Summary
Expectation of the number of calls for mocked methods is not correct
Current behavior
When setting up expectations on mocked methods on mocks, the call number is always 0.
How to reproduce
I've created a public repo that can be installed using composer: https://github.com/dingo-d/phpunit-expectation-count-issue.
On versions 9 and 10 (I know 9 is out of bugfix support, but that was the one I tested on first) I got the same error:
Expectation failed for method name is "count" when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
On version 11 I got a different error:
Error: Typed property MockObject_C_c176faab::$__phpunit_state must not be accessed before initialization
/Users/dzoljom/Projects/Personal/phpunit-test/src/A.php:15
/Users/dzoljom/Projects/Personal/phpunit-test/tests/ATest.php:36
So I'm not sure what is the problem here.
Expected behavior
When I used xDebug to inspect the code when I ran the test, I could see that when I passed the mocked method as a dependency to the class under test, the mocked services were provided ($cMock
and $bMock
) so their mocked methods are used and called instead of the actual classes.
So I expect (no pun intended) that the call count to these methods I mocked will increase, and I'll get the info that the methods were called once for each service class.
This is just a POC from another project that has a similar structure, but the call count is different in each service, and this happens there as well (the reported count is 0 when it shouldn't be). As to why I'm doing these mocks, this is a simplified version, in my version the mocked method on the services does an API call, so I need to avoid doing that in my tests ofc (hence mocking this).
I'm not sure if I'm doing something wrong with the expectations, or if this is some edge-case bug 🤷🏼♂️
Activity